Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

error with echoCheck visual studio code #1

Open
Sorin-Jayaweera opened this issue Jul 7, 2022 · 1 comment
Open

error with echoCheck visual studio code #1

Sorin-Jayaweera opened this issue Jul 7, 2022 · 1 comment

Comments

@Sorin-Jayaweera
Copy link

*I don't know where to go for a forum, but this should be a super quick and simple question.

image

What is going wrong?

all code pertaining to the ultrasounds(basically a copy of what you had in the examples):


#include <NewPing.h>

#define SONAR_NUM     2 // Number of sensors.
#define MAX_DISTANCE 200 // Maximum distance (in cm) to ping.
#define PING_INTERVAL 33 // Milliseconds between sensor pings (29ms is about the min to avoid cross-sensor echo).

const byte SONARECHOPIN1 = 23; 
const byte SONARTRIGPIN1 = 22;

const byte SONARECHOPIN2 = 21;
const byte SONARTRIGPIN2 = 20;

/*sonar 1 pins:
unsigned long pingTimer[SONAR_NUM]; // Holds the times when the next ping should happen for each sensor.
unsigned int cm[SONAR_NUM];         // Where the ping distances are stored.
uint8_t currentSensor = 0;          // Keeps track of which sensor is active.

char *sonarLabel[] = {"ultra1", "ultra2", "ultra3"};

const int ultrasoundSamplingTimer = 200; // check very often
double lastUltrasoundSample;

void readUltrasound();
void echoCheck();
void oneSensorCycle();


void setup() {
   pingTimer[0] = millis() + 75;           // First ping starts at 75ms, gives time for the Arduino to chill before starting.
   for (uint8_t i = 1; i < SONAR_NUM; i++){ // Set the starting time for each sensor.
    pingTimer[i] = pingTimer[i - 1] + PING_INTERVAL;
   }
   
}

void loop() {  
  if(currentTime - lastUltrasoundSample >= ultrasoundSamplingTimer){
    Serial.println("readUltraSound");
    lastUltrasoundSample = millis();    
    readUltrasound();    
  }
}

void readUltrasound(){
 for (uint8_t i = 0; i < SONAR_NUM; i++) { // Loop through all the sensors.
    if (millis() >= pingTimer[i]) {         // Is it this sensor's time to ping?
      pingTimer[i] += PING_INTERVAL * SONAR_NUM;  // Set next time this sensor will be pinged.
      if (i == 0 && currentSensor == SONAR_NUM - 1) oneSensorCycle(); // Sensor ping cycle complete, do something with the results.
      sonar[currentSensor].timer_stop();          // Make sure previous timer is canceled before starting a new ping (insurance).
      currentSensor = i;                          // Sensor being accessed.
      cm[currentSensor] = 999;                      // Make distance zero in case there's no ping echo for this sensor.
      sonar[currentSensor].ping_timer(echoCheck); // Do the ping (processing continues, interrupt will call echoCheck to look for echo).
    }
  }
}


void echoCheck() { // If ping received, set the sensor distance to array.
  if (sonar[currentSensor].check_timer()){
    cm[currentSensor] = sonar[currentSensor].ping_result / US_ROUNDTRIP_CM;
  }
}

void oneSensorCycle() { // Sensor ping cycle complete, do something with the results.
 /* display.setCursor(0,cursorLine2);
  display.print("                    ");//clear the line only
  display.display();
*/
//ultrasound has pinged, now check distance and do object avoidence............................................................................
}

image

This should be a relatively quick and easy fix, just an issue with Visual Studio Code or something. Thank you! I love you library, it's a major component of my project, and without it I wouldn't be able to do this!

@jonnor
Copy link
Member

jonnor commented Aug 3, 2022

This is a fork of NewPing - not the original library. If you just want the, then the NewPing original is probably better maintained.

In this fork, the user specified function that is passed to ping_timer, receives an additional argument, a pointer to the NewPing instance that called the function.

So to work with this version, the function signature needs to be

void echoCheck(NewPing *ping) {
...
}

Since I only intended to use it in https://github.com/microflo/microflo, the examples have not been updated.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants