Go Down

Topic: Ping sensor, servo and LED working :) (Read 2071 times) previous topic - next topic

kevin1ab

Nov 09, 2012, 03:05 am Last Edit: Nov 09, 2012, 04:19 am by Coding Badly Reason: 1
Hi
I wanted to be able to read input from a HC-SR04 sonar sensor and then move a servo based on the
distance from the sensor. I was able to take 3 example sketches and merge them and am happy with the result
so I'm just sharing the code.
The code uses the newping library.  

My code:

Turns ON a LED for one second. [this helps me visualize/be ready when the distance will be read]
When the LED goes OFF, the sensor reads the distance and puts it in the 'pos' variable.
The code writes the 'pos' to the servo.
There is a 4 second delay so the servo isn't jumping around.  


I hope this helps anyone working with this concept.
Kevin

Code: [Select]
#include <NewPing.h>
#include <Servo.h>
#define TRIGGER_PIN  12
#define ECHO_PIN     11
#define MAX_DISTANCE 200
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE);
Servo myservo;  // create servo object to control a servo            
int pos = 0;    // variable to store the servo position
int led = 13;

void setup() {
 Serial.begin(115200);
 myservo.attach(9);  // attaches the servo on pin 9 to the servo object
 pinMode(led, OUTPUT);
}

void loop() {
 int uS = sonar.ping();
 pos = uS / US_ROUNDTRIP_CM * 2;  // goes from 0 degrees to 180 degrees
 Serial.println(uS / US_ROUNDTRIP_CM);      
 digitalWrite(led, HIGH);   // turn the LED on (HIGH is the voltage level)
 delay(1000);               // wait for a second
 myservo.write(pos);              // tell servo to go to position in variable 'pos'
 digitalWrite(led, LOW);    // turn the LED off by making the voltage LOW
 delay(4000);               // wait for 4 seconds
}



Moderator edit: [code] [/code] tags added.

kevin1ab

I put up a vid

http://www.youtube.com/watch?v=hMh1Me6hlyQ&feature=plcp

Kevin

kevin1ab

I added a row of 5 LEDs to relate to the distance from the sensor.
The further away, the more LEDs, the closer , the less. here's my code.
I'm new so there is probably a cleaner way ;)
Demo at :
http://www.youtube.com/watch?v=YZdHFabamj8
Kevin B

Code: [Select]

#include <NewPing.h>
#include <Servo.h>
#define TRIGGER_PIN  12
#define ECHO_PIN     11
#define MAX_DISTANCE 200
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE);
Servo myservo;  // create servo object to control a servo           
int pos = 0;    // variable to store the servo position
int led = 13;
int led2 = 8;
int led3 = 7;
int led4 = 4;
int led5 = 2;

void setup() {
  Serial.begin(115200);
  myservo.attach(9);  // attaches the servo on pin 9 to the servo object
  pinMode(led, OUTPUT);
  pinMode(led2, OUTPUT);
  pinMode(led3, OUTPUT);
  pinMode(led4, OUTPUT);
  pinMode(led5, OUTPUT);
}

void loop() {
  int uS = sonar.ping();
  pos = uS / US_ROUNDTRIP_CM * 2;  // goes from 0 degrees to 180 degrees
  Serial.println(uS / US_ROUNDTRIP_CM);       
  digitalWrite(led, HIGH);   // turn the LED on (HIGH is the voltage level)

  if (pos > 90) {
    digitalWrite(led, HIGH);   
    digitalWrite(led2,HIGH);
    digitalWrite(led3,HIGH);
    digitalWrite(led4,HIGH);
    digitalWrite(led5,HIGH);
    myservo.write(pos * 2);             
    delay(2000);
    digitalWrite(led, LOW);   
    digitalWrite(led2,LOW);
    digitalWrite(led3,LOW);
    digitalWrite(led4,LOW);
    digitalWrite(led5,LOW); 
  }
    else if ((pos > 70 ) && (pos < 90)) {
    digitalWrite(led, HIGH);   
    digitalWrite(led2,HIGH);
    digitalWrite(led3,HIGH);
    digitalWrite(led4,HIGH);
    myservo.write(pos * 2);             
    delay(3500);
    digitalWrite(led, LOW);   
    digitalWrite(led2,LOW);
    digitalWrite(led3,LOW);
    digitalWrite(led4,LOW); 
  }
    else if ((pos > 50 ) && (pos < 70)) {
    digitalWrite(led, HIGH);   
    digitalWrite(led2,HIGH);
    digitalWrite(led3,HIGH);
    myservo.write(pos * 2);             
    delay(3500);
    digitalWrite(led, LOW);   
    digitalWrite(led2,LOW);
    digitalWrite(led3,LOW);
  }
    else if ((pos > 25 ) && (pos < 50)) {
    digitalWrite(led, HIGH);   
    digitalWrite(led2,HIGH);
    myservo.write(pos * 2);             
    delay(3500);
    digitalWrite(led, LOW);   
    digitalWrite(led2,LOW);
  }
    else if ((pos > 10 ) && (pos < 25)) {
    digitalWrite(led, HIGH);   
    myservo.write(pos * 2);             
    delay(3500);
    digitalWrite(led, LOW);   
  }
    else if ((pos > 10 ) && (pos < 0)) {
    digitalWrite(led, LOW);   
    digitalWrite(led2,LOW);
    digitalWrite(led3,LOW);
    digitalWrite(led4,LOW);
    digitalWrite(led4,LOW);
    delay(3500);
  }
    else { pos = 5 ;
    digitalWrite(led, LOW);   
    digitalWrite(led2,LOW);
    digitalWrite(led3,LOW);
    digitalWrite(led4,LOW);
    digitalWrite(led4,LOW); 
    }
}



Now before you get too far into this, rewrite the sketch using NewPing's timer interrupt method and the rest of the code in a totally event driven way so there's no delay() statements.  The sooner you embrace writing code using an event driven paradigm, the easier it will be to write much more complex sketches and circuits.  As long as you have delay statements, you'll have clunky sketches that won't be able to do complex tasks.

Tim
Arduino - Teensy - Raspberry Pi
My libraries: NewPing - LCDBitmap - toneAC - NewTone - TimerFreeTone

kevin1ab

Thanks, I'm new at coding but I will look and study it.
Kevin

Jasondt2001

YOU'RE new @ coding.....??? Man I'm feeling pretty stupid after reading that.... $) LOL
Thanks for sharing, I really appreciate learning from others. I'm trying to put bits and pieces together and it's not going as well as yours LOL. But I'm learning from people like you and others that are willing to share their hard work, so thank you!
After reading weeks getting excited about all the things you can do with this thing I finally bought one... and realized I was in WAAAY over

Go Up