Pages: [1]   Go Down
Author Topic: Ping sensor, servo and LED working :)  (Read 1286 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 10
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
#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.
« Last Edit: November 08, 2012, 10:19:32 pm by Coding Badly » Logged

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

I put up a vid



Kevin
Logged

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

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 smiley-wink
Demo at :
 
Kevin B

Code:
#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); 
    }
}


Logged

Toledo, OH
Offline Offline
Sr. Member
****
Karma: 19
Posts: 455
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Arduino Uno - Teensy 2.0 - Teensy 3.0 - Raspberry Pi Model B w/512MB RAM
My libraries: NewPing library - LCDBitmap library - toneAC library - NewTone library

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

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

San Jose, Ca
Offline Offline
Newbie
*
Karma: 0
Posts: 3
In way over my head... lol
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

YOU'RE new @ coding.....??? Man I'm feeling pretty stupid after reading that.... smiley-money 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!
Logged

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

Pages: [1]   Go Up
Jump to: