Blinking LED while continuous servo moves

Hello someone help me with this code please.
So, i already have my servo to move and my IR sensor done
I click in the button, the servo moves clockwise, click again, the servo stops, if i do it again, the servo moves antclockwise and again, the servo stops. After this its a loop.
My IR sensor is working, when the servo is moving and the IR sensor gets a sinal the servo stops.
This is all i have, now i want to add a LED and it blinks while the servo rotates to both sides. When i try to add the LED it does not work or the servo not move, so i need help
The working code is down below

#include <Servo.h>
Servo motor1;
int cont = 1; // a variable that counts the button clicks
int IR = 13;
void setup() {
  pinMode(4, INPUT); // button in pin 4
  pinMode(5, INPUT); //limit switch 1
  pinMode(6, INPUT); //limit switch 2
 pinMode(13, INPUT); // sensor IR
  pinMode(11, OUTPUT); // LED 
  motor1.attach(9); // motor on pin 9
  motor1.write(96); // this number, 96 is where my servo is stopped, remember this is continuous 
}

void loop() {
  if (digitalRead(4) == 1) { // if button is presses
    cont++; //  +1 variable cont
    delay(300); // 
  }
  if (cont == 5) {
    cont = 1; // 
  }

  if (cont == 1) {
    motor1.write(96); //servo starts stopped
    delay(50);
  }
  else if (cont == 2) {
    motor1.write(180);//servo rotates clockwise when i click the button
    delay(50);
  }
  else if (cont == 3) { 
    motor1.write(96); //servo stops again when i click the button
    delay(50);
  }
  else if (cont == 4) {
    motor1.write(0); //servo rotates anticlockwise when i click the button
    delay(50);
}

while (digitalRead(13) == LOW){
  motor1.write(96);
}
}

Maybe something like this?

#include <Servo.h>

Servo motor1;
int cont = 1; // a variable that counts the button clicks
int IR = 13;
int LED = 11;

void setup() {
  pinMode(4, INPUT); // button in pin 4
  pinMode(5, INPUT); // limit switch 1
  pinMode(6, INPUT); // limit switch 2
  pinMode(13, INPUT); // sensor IR
  pinMode(LED, OUTPUT); // LED
  motor1.attach(9); // motor on pin 9
  motor1.write(96); // servo initially stopped at 96 degrees
}

void loop() {
  if (digitalRead(4) == 1) { // if button is pressed
    cont++; // increment cont variable by 1
    delay(300); 
  }
  if (cont == 5) {
    cont = 1; // reset cont to 1 when it reaches 5
  }

  if (cont == 1) {
    motor1.write(96); // servo stops
    digitalWrite(LED, LOW); // turn off LED
    delay(50);
  }
  else if (cont == 2) {
    motor1.write(180); // servo rotates clockwise
    blinkLED(); // call function to blink LED
    delay(50);
  }
  else if (cont == 3) { 
    motor1.write(96); // servo stops
    digitalWrite(LED, LOW); // turn off LED
    delay(50);
  }
  else if (cont == 4) {
    motor1.write(0); // servo rotates anticlockwise
    blinkLED(); // call function to blink LED
    delay(50);
  }

  while (digitalRead(13) == LOW) {
    motor1.write(96); // servo stops if IR sensor detects no signal
    digitalWrite(LED, LOW); // turn off LED
  }
}

void blinkLED() {
  digitalWrite(LED, HIGH); // turn on LED
  delay(500); // blink duration
  digitalWrite(LED, LOW); // turn off LED
  delay(500); // blink duration
}

Thanks for reply, but did not work as i want, now when i first click it works and the led blinks but to stop sometimes i need to click few times and other times i need to click many times.
Also the IR sensor if it gets sinal when the led is on, the servo continuos moving until the led turns off.

I told you elsewhere that you have some workable code you can have fun with, and I may have gone on to say that soon enough this approach will hit the wall.

Basing the functionality and timing on the use of delay() will lead to unresponsive systems that cannot maintain even the illusion of doing three things at once.

  • Watch the button(s)

  • Run the motor, respecting limit switches

  • Blink an LED

You need to drop everything and learn some more basic programming. Especially at this low end, you need to learn and exploit all the tricks that have been long ago developed for wringing the maximum out of a minimal machine.

Google…

Arduino blink without delay

and

Arduino two things at once

and

Arduino finite state machine

and

Arduino traffic lights finite state machine

You will not regret a single minute that you spend poking around amongst the 1000s of hits those searches will provide.

a7

2 Likes

Ok, i will search for some.

here's something i had posted on another thread where the OP was asking to do 2 timed things at the same time

in your case, you need to also monitor for button presses

// multiple things

const byte PotPin = 34;
const byte LedPin = 18;         // Capitalize Constants

unsigned long msec;

// -----------------------------------------------------------------------------
void taskPot ()
{
    static unsigned long msecLast;
    if (msec - msecLast >= 2000)  {
        msecLast = msec;

        Serial.print   ("Analog value: ");
        Serial.println (analogRead (PotPin));
    }
}

// -----------------------------------------------------------------------------
void taskLed ()
{
    static unsigned long msecLast;
    static int           state;
    if (msec - msecLast >= 500)  {
        msecLast = msec;

        if (HIGH == state)
            state = LOW;
        else
            state = HIGH;
        digitalWrite (LedPin, state);
    }
}

// -----------------------------------------------------------------------------
void loop ()
{
    msec = millis ();
    taskPot ();
    taskLed ();
}

// -----------------------------------------------------------------------------
void setup () {
    Serial.begin (115200);
    pinMode (LedPin, OUTPUT);
}

Here's one that has been in the news lately, give it a shot:

HTH

a7

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.