Pages: 1 ... 32 33 [34]   Go Down
Author Topic: NewPing Library: HC-SR04, SRF05, SRF06, DYP-ME007, Parallax PING))) - v1.5  (Read 113891 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 2
mOuchadino Lumajang jatim Indonesia
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I have try upload the sketch with arduino1.05R2 but i get 1 of sensor sornar aktive  1 more not working please... help me...it.s make me crazy :p

the sketch as bellow smiley-razz
Code:
/* Code modified for analog output by: mOuchadino
 
 Available PWM pin : 3, 5, 6, 9, 10, 11
 Available Digital I/O pin : 2 to 13
*/

#include <NewPing.h>

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

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.

NewPing sonar[SONAR_NUM] = { // Sensor object array.
  NewPing(5, 6, MAX_DISTANCE), // Each sensor's echo pin, trigger pin, and max distance to ping.
  NewPing(7, 8, MAX_DISTANCE),
};

int pwmPins[SONAR_NUM] = {
  9, 10 }; // array Nomor pin utk keluaran PWM Analog out 0-5v
int led = 13;

void setup() {
  Serial.begin(115200);
  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;
  }

  for (uint8_t i = 0; i < SONAR_NUM; i++) { // pwm output pin counter
    pinMode(pwmPins[i], OUTPUT);
  }
 
  {              
  // initialize the digital pin as an output.
  pinMode(led, OUTPUT);    
}
 
  Serial.println();
}

void loop()
{
  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] = 0; // 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).
    }
  
  }

{
  digitalWrite(led, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(1000);               // wait for a second
  digitalWrite(led, LOW);    // turn the LED off by making the voltage LOW
  delay(2000);               // wait for a second
}
  // Other code that *DOESN'T* analyze ping results can go here.
}

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

// Sensor ping cycle complete, do something with the results. Location option 1 for code processing

void oneSensorCycle()
{
  for (uint8_t i = 0; i < SONAR_NUM; i++) {
    // turn the pin on:
    // http://arduino.cc/en/Reference/analogWrite
    analogWrite(pwmPins[i], cm[i]);

    //        Serial.print(i); // print sensor number accessed
    //        Serial.print("="); // = märk
    //        Serial.print(cm[i]); // sensor distance value
    //        Serial.println("cm "); // cm
    //        Print format i = xxx cm
  }
}

===========================================================================================

#include <NewPing.h>

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

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.

NewPing sonar[SONAR_NUM] = { // Sensor object array.
 // Each sensor's trigger pin, echo pin, and max distance to ping.
  NewPing(5, 6, MAX_DISTANCE),
  NewPing(7, 8, MAX_DISTANCE),
  
};

int pwmPins[SONAR_NUM] = {9, 10 };  // an array of pin numbers to which pwm output is written
int led = 13;

void setup()
{
  Serial.begin(115200);
  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;
  }

  for (uint8_t i = 0; i < SONAR_NUM; i++) { // pwm output pin counter
    pinMode(pwmPins[i], OUTPUT);
   }
 {              
  
   // initialize the digital pin as an output.
  
  pinMode(led, OUTPUT);    
}
 
  Serial.println();
}

void loop()
{
  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] = 0; // 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).
    }
  }
}

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

// Sensor ping cycle complete, do something with the results. Location option 1 for code processing

void oneSensorCycle()
{
  for (uint8_t i = 0; i < SONAR_NUM; i++) {
    // turn the pin on:
    
    analogWrite(pwmPins[i], cm[i]);

            Serial.print(i); // print sensor number accessed
            Serial.print("="); // = märk
            Serial.print(cm[i]); // sensor distance value
            Serial.println("cm "); // cm
    //        Print format i = xxx cm
  }
}

moderator update: added code tags  -->  # button above smileys
« Last Edit: July 07, 2014, 12:13:29 pm by robtillaart » Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 2
mOuchadino Lumajang jatim Indonesia
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

==============================================================================
i modifed the sketch like as bellow  smiley-cry still not working with 2 sonar


Code:
#include <NewPing.h>

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

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.

NewPing sonar[SONAR_NUM] = { // Sensor object array.
  NewPing(5, 6, MAX_DISTANCE), // Each sensor's echo pin, trigger pin, and max distance to ping.
  NewPing(7, 8, MAX_DISTANCE),
};

int pwmPins[SONAR_NUM] = {9, 10 }; // array Nomor pin utk keluaran PWM Analog out 0-5v
int ledPins[SONAR_NUM] = {12, 13};

void setup() {
  Serial.begin(115200);
  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;
  }

for (uint8_t i = 0; i < SONAR_NUM; i++) { // pwm output pin counter
    pinMode(pwmPins[i], OUTPUT);
  }
                
  // initialize the digital pin as an output.
  
for (uint8_t i = 0; i < SONAR_NUM; i++) { // pwm output pin counter
    pinMode(ledPins[i], OUTPUT);
  }  
 
  Serial.println();
}

void loop()
{
  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] = 0; // 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).
    }
  
  }

{
{
  digitalWrite(ledPins[12,13], HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(500);               // wait for a second
  digitalWrite(ledPins[13,12], LOW);    // turn the LED off by making the voltage LOW
  delay(1000);               // wait for a second
}
}
  // Other code that *DOESN'T* analyze ping results can go here.
}

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

// Sensor ping cycle complete, do something with the results. Location option 1 for code processing

void oneSensorCycle()
{
  for (uint8_t i = 0; i < SONAR_NUM; i++) {
    // turn the pin on:

    analogWrite(pwmPins[i], cm[i]);

            Serial.print(i); // print sensor number accessed
            Serial.print("="); // = märk
            Serial.print(cm[i]); // sensor distance value
            Serial.println(" -cm "); // cm
    //        Print format i = xxx cm
  }
}

===========================================================================

but if i.m delete the as bellow the 2 sensor work fine :p

Code:
{
  digitalWrite(ledPins[12,13], HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(500);               // wait for a second
  digitalWrite(ledPins[13,12], LOW);    // turn the LED off by making the voltage LOW
  delay(1000);               // wait for a second
}
every body please help me ..... smiley-cry   smiley-cry

moderator update: added code tags  -->  # button above smileys
« Last Edit: July 07, 2014, 12:15:45 pm by robtillaart » Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 9
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi Tim,

Any Plans for releasing the version 1.6 soon?

I am using this with ATtiny  as well as arduino and trying to avoid keeping the timers commented/uncommented for each use  smiley

Thanks for the great library BTW

Shomar
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 15
Applied mechanics and automatic control engineering student
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I have problem with this library. If I upload example sketch, everything works, but if I use this library for my project, Arduino loose USB communication, does anybody knows what should I do?
Thanks
Logged

Pages: 1 ... 32 33 [34]   Go Up
Jump to: