0

my project involves: 1 ultrasonic sensor. 1 servo motor. 3 leds. when a person comes in range of ultrasonic sensor (7 to 20),servo turns by 150 degree. then ultrasonic sensor check again if the person is in range, if yes then led1 turns on for 10sec n turns off, still if the person in in range then led2 turns on for 10sec and turns off, still if the person is in range then led3 turns on for 10sec and this keep on repeating for the leds(led1, led2, led3 but servo is still in 150) once detected and the person is not detected the leds should not glow and servo should return to 0

code:

if ((distance1>7)&&(distance1 <= 20))
 {
  myservo1.write(150);
  delay(1000);
 }
 {
  if ((distance2>7)&&(distance2 <= 20))
   {
   digitalWrite(led1,HIGH); 
   delay(1000); 
   digitalWrite(led1,LOW);
   }
  if ((distance1>7)&&(distance1 <= 20))
   {
   digitalWrite(led2,HIGH); 
   delay(1000); 
   digitalWrite(led2,LOW);
   }
   if ((distance1>7)&&(distance1 <= 20))
   {
   digitalWrite(led3,HIGH); 
   delay(1000); 
   digitalWrite(led3,LOW);
   }
else
{
 myservo1.write(0);
  }

but this code does not work the way i want. it blink the leds even when there is no person in range. need help in programing so that the leds blink only when ultrasonic detects the person in range and stops when person is not detected. thank u in advance

  • The code indentation looks odd, check if the curly braces are balanced the way you intend. especially line 6 opens a new block that is unconditional. The else in line 25 belongs to the if in line 19. – Kwasmich Aug 8 '16 at 10:21
1

Your indenting doesn't match your code.

Right after the very first if (without an else), you have a new open brace ({). This looks wrong:

if ((distance1>7)&&(distance1 <= 20))
 {
  myservo1.write(150);
  delay(1000);
 }
 { // <<<< What is this?

The very very final else matches the very very final if - certainly not the first if!

Also, what is the difference between distance1 and distance2? You only have one ultrasonic sensor - there is only one distance!

EDIT:

I think what you need to do is use loop() more effectively. At the moment, you're pausing in the middle of your code, rather than letting the rest of the code work and using millis(). That function returns the number of milliseconds since the program started - which you can use for your problem (with a little mathematics):

EDIT 2:

I've added a little extra code to play different tunes depending on the current state. It uses the library at http://www.rhydolabz.com/other-widgets-c-205_124/mini-mp3-player-module-p-2159.html

static unsigned lastMilli = 0;
static byte lastLED = 0;
static const byte LEDs[] = { led1, led2, led3 };

distance1 = // Whatever you call to get the distance...
if (distance1>7) && (distance1<=20)) { // Too close?
   if (lastMilli==0) {       // Is this new???
       lastMilli = millis(); // Yes! Record that...
       mysero.write(150);    // ...and set servo.
   } // if
   unsigned delay = millis() - lastMilli; // Get how long it's been
   byte which = delay / 10000 % 3;        // Which LED / tune?
   byte LED = LEDs[which];                // Which LED?
   if (LED != lastLED) {                  // Has LED changed?
       if (lastLED!=0) {
           digitalWrite(lastLED, LOW);    // Yes! Stop that one
       } // if
       mp3_single_loop(true);             // Play forever!
       mp3_single_play(which+1);          // Play "mp3/000#.mp3"
       lastLED = LED;
       digitalWrite(LED, HIGH);           // And start the new one
   } // if
} // if
else if (lastMillis!=0) { // They're gone!
    digitalWrite(lastLED, LOW); // No LED
    myservo.write(0);           // No servo
    lastMilli = 0;              // So start again
    lastLED = 0;
    mp3_stop();
} // else

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.