Help - Adding LED "ON" "OFF" and "Blink" to Servo Sweep Program

A little help for a "newbie" please. I'm assisting a local artist on one of his installation pieces. I'm using an Arduino UNO board with a servo to rotate the needle of a pressure gauge (for show not real) 180 degrees and successfully accomplished this from the Arduino library "Arduino Sweep" and even figured out how to add a 5 second delay to stop the sweep at one end.
My question: Is there anyway to add 2 LEDS to my UNO to switch ON and OFF and Blink when the servo is at specific points of it sweep (by degrees)? I can do this mechanically with cam and a micro switch but thought there must be a more elegant solution in programming the UNO.
In my current Servo Sweep sketch (see below), I would like a Green LED to remain ON from 0 - 90 degrees then Turn OFF and then from 90 - 180 degrees a second Red LED to Switch ON and Blink and then reverse the ON/OFF/Blink when sweeping back during the reverse sweep. The effect is that a Green LED that is ON shows a "safe" reading between 0 - 90 degrees of the needle's sweep and then from 90 - 180 degrees the "danger" Red LED Blinks.

Is it possible to add LEDs to function like this?

My current setup is:
// Sweep
// by BARRAGAN http://barraganstudio.com
// This example code is in the public domain.

#include <Servo.h>

Servo myservo; // create servo object to control a servo
// a maximum of eight servo objects can be created

int pos = 0; // variable to store the servo position

void setup()
{
myservo.attach(9); // attaches the servo on pin 9 to the servo object
}

void loop()
{
for(pos = 0; pos < 180; pos += 1) // goes from 0 degrees to 180 degrees
{ // in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(120); // waits 15ms for the servo to reach the position
}
for(pos = 180; pos>=1; pos-=1) // goes from 180 degrees to 0 degrees
{
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(120); // waits 15ms for the servo to reach the position
}
delay(5000); // waits for a second
}

Split the sweep into 2 sections of code, with the break at 90- basically double up that coding, one part going 0-90, other part from 90-180.

Turn led1 on, start the first "half-sweep", off led1 and on led2, then second "half-sweep"

Hi jimboZA! Thanks for the quick reply.
Bearing in mind that I'm very new to this (the UNO was my first Arduino purchase) would you be so kind as to give me an example of how I integrate the LED functions (with pin designations) and break up the sweep into 2 sections in code?
You would most likely save me hours of frustration in trial and error (lots of error) and make a new friend in the process.
Thanks again!

Something like this.... not tested, YMMV, E&OE.... usual disclaimer crap 8)

See lines marked //<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

#include <Servo.h> 
  
 Servo myservo;  // create servo object to control a servo 
                 // a maximum of eight servo objects can be created 
  
int pos = 0;    // variable to store the servo position 
int redLed= 8;  //<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
int greenLed=7; //<<<<<<<<<<<<<<<<<<<<<<<<<<

  
void setup() 
{ 
   myservo.attach(9);  // attaches the servo on pin 9 to the servo object 
  pinMode(redLed, OUTPUT); //<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<,
 pinMode(greenLed, OUTPUT); //<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

} 
    
void loop() 
{ 
   pinMode(redLed, HIGH);  //<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
   for(pos = 0; pos < 90; pos += 1)  // goes from 0 degrees to 90 degrees   <<<<<<<<<<<<<<<<<<<<
   {                                  // in steps of 1 degree 
     myservo.write(pos);              // tell servo to go to position in variable 'pos' 
     delay(120);                       // waits 15ms for the servo to reach the position 
   } 

  pinMode(redLed, LOW);  //<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
pinMode(greenLed, HIGH);  //<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
{ 
   for(pos >=90; pos < 180; pos += 1)  // goes from 90 degrees to 180 degrees  <<<<<<<<<<<<<<<<<<<<<
   {                                  // in steps of 1 degree 
     myservo.write(pos);              // tell servo to go to position in variable 'pos' 
     delay(120);                       // waits 15ms for the servo to reach the position 
   }


// <<<<<<<<<<<<<<<<<<<<<<<, and add similar for the return journey


  
}

Edit.... no blinking, just on or not

Edit.... changed a pin number, you were using 9 for servo, oops

Thanks again JimboZA!
This gives me a good start. I've bought a blinking Red LED allowing easier programming for just ON and OFF functions.
I plan to play around with this on New Year's Day and I'll post my success or failure.

Happy New Year!

Hi JimboZA! I'm almost there and need just a bit more help.

I'm using Non-Blinking Red and Green LED's to make for easier debugging and the LED's do turn ON but I have 3 bugs that are driving me nuts.

Here are the 3 bugs with the current code
#1) Green LED stays ON all the time when it should be OFF between 90 - 180 degrees (Red LED works fine and is ON only between 90 - 180 degrees but there is an issue with a blink see #2). The 2 LED's should not be ON at the same time. Only one LED should be ON during half the servo sweep.
#2) The Red LED briefly blinks at the 0 position and I can't figure out why (this is a non-blink LED).
#3) There is a 2 second (2000ms) delay at the beginning of the sweep (0 degrees) that is supposed to be at the end of the sweep (180 degrees) and I can't resolve this.

#include <Servo.h>

Servo myservo; // create servo object to control a servo
// a maximum of eight servo objects can be created

int pos = 0; // variable to store the servo position
int redLed= 8; //<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
int greenLed=7; //<<<<<<<<<<<<<<<<<<<<<<<<<<

void setup()
{
myservo.attach(9); // attaches the servo on pin 9 to the servo object
pinMode(redLed, OUTPUT); //<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<,
pinMode(greenLed, OUTPUT); //<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

}

void loop()
{
pinMode(redLed, HIGH); //<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
for(pos = 0; pos < 90; pos += 1) // goes from 0 degrees to 90 degrees <<<<<<<<<<<<<<<<<<<<
{ // in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(30
); // waits 15ms for the servo to reach the position
}

pinMode(redLed, LOW); //<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
pinMode(greenLed, HIGH); //<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

for(pos >=90; pos < 180; pos += 1) // goes from 90 degrees to 180 degrees <<<<<<<<<<<<<<<<<<<<<
{ // in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(30); // waits 15ms for the servo to reach the position
}
pinMode(redLed, HIGH); //<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
for(pos = 180; pos < 90; pos += 1) // goes from 0 degrees to 90 degrees <<<<<<<<<<<<<<<<<<<<
// in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(30); // waits 15ms for the servo to reach the position

pinMode(redLed, LOW); //<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
pinMode(greenLed, HIGH); //<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

for(pos >=90; pos < 1; pos += 1) // goes from 90 degrees to 180 degrees <<<<<<<<<<<<<<<<<<<<<
{ // in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(30); // waits 15ms for the servo to reach the position
}
delay(2000);
}

You aren't using pinMode properly in your void loop()
In those instances, you should be using digitalWrite

#include <Servo.h> 
  
Servo myservo;  // create servo object to control a servo 
                 // a maximum of eight servo objects can be created 
  
int pos = 0;    // variable to store the servo position 
int redLed= 8;  //<<
int greenLed=7; //<<<
  
void setup() 
{ 
   myservo.attach(9);  // attaches the servo on pin 9 to the servo object 
   pinMode(redLed, OUTPUT); //<<<<<<<<
   pinMode(greenLed, OUTPUT); //<<<<<<<
} 
    
void loop() 
{ 
  digitalWrite(redLed, HIGH);  //<<<<<<
  for(pos = 0; pos < 90; pos += 1)  // goes 0 deg to 90 deg<<<
  {                                  // in steps of 1 deg 
    myservo.write(pos);              // tell servo to go to position in var 'pos' 
    delay(30);                       // waits 15ms for the servo to reach the pos 
  } 
  digitalWrite(redLed, LOW);  //<<<<<<<
  digitalWrite(greenLed, HIGH);  //<<<<<
  for(pos >=90; pos < 180; pos += 1)  // goes from 90 deg to 180 deg  <<<<<
  {                                  // in steps of 1 degree 
     myservo.write(pos);              // tell servo to go to pos in var 'pos' 
     delay(30);                       // waits 15ms for the servo to reach the pos 
  }
  digitalWrite(redLed, HIGH);  //<<<<<
  for(pos = 180; pos < 90; pos += 1)  // goes from 0 deg to 90 deg   <<<
  {                                   // in steps of 1 degree 
     myservo.write(pos);              // tell servo to go to pos in variable 'pos' 
     delay(30);                       // waits 15ms for the servo to reach the pos 
  } 
  digitalWrite(redLed, LOW);  //<<<<
  digitalWrite(greenLed, HIGH);  //<<<<
  for(pos >=90; pos < 1; pos += 1)  // goes from 90 deg to 180 deg  <<<<<
  {                                  // in steps of 1 degree 
    myservo.write(pos);              // tell servo to go to pos in variable 'pos' 
    delay(30);                       // waits 15ms for the servo to reach the pos
  }
  delay(2000);
}

You aren't using pinMode properly in your void loop()
In those instances, you should be using digitalWrite

Crap... that's my fault, sorry- those come from my code a few posts earlier.... late at night and full of flu and sinus, not concentrating. I'm surprised in compiled though.....

//test

void setup(){

pinMode(8, OUTPUT);
pinMode(8, HIGH);  // should be digitalWrite but this compiles
}

void loop() {}

tobar8:
This gives me a good start. I've bought a blinking Red LED allowing easier programming for just ON and OFF functions.

No, no, that is cheating - or more accurately, wimping out!

JimboZA:
I'm surprised in compiled though.....

You write valid (C++) code, it will compile.

The compiler, clever though it might (or might not) be, can not check whether what you instruct makes any sense.

A bit like the stories about the spelling checker ...

The compiler, clever though it might (or might not) be, can not check whether what you instruct makes any sense.

A bit like the stories about the spelling checker ...

Not really a valid analogy...it's not that it can not check, it's that it does not.... it could easily check to see if the value after the comma is either INPUT or OUTPUT. It's not the same as checking logic, where it couldn't know that I meant INPUT and typed O UTPUT. It should (ok, could) reject anything other than INPUT or OUTPUT.

Anyhoo, irrelevant for now and taking the thread OT.

No, no, that is cheating - or more accurately, wimping out!

Not really: it's a perfectly valid use of a component that has the required characteristics. OTOH, a hardware solution ties the project to one that blinks, and if the requirements change to a non-blinking solution then it's back to the drawing board. That's not the same as cheating or wimping out though.

Runaway Pancake, I appreciate your efforts but even with the change only the Red LED comes on and I still have a the servo sweep fast in its return rather than just a slow return. jimboZA had me almost there and I'm running in one sweep direction.

BTW, since I'm new at this I eliminated the Blinking LED until I can get this buttoned down (too many variables to deal with).

Here is what I have now and the servo sweep works fine in one direction and the LED's work fine in both direction. I just need the return sweep 180 - 90 and 90 - 0 to run the same speed as the 0 - 90 and 90 - 180 sweep.

Any thoughts?

Here is my current code (cut and pasted)

#include <Servo.h>

Servo myservo; // create servo object to control a servo
// a maximum of eight servo objects can be created

int pos = 0; // variable to store the servo position
int redLed= 8; //<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
int greenLed=7; //<<<<<<<<<<<<<<<<<<<<<<<<<<

void setup()
{
myservo.attach(9); // attaches the servo on pin 9 to the servo object
pinMode(redLed, OUTPUT); //<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<,
pinMode(greenLed, OUTPUT); //<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

}

void loop()
{
pinMode(redLed, HIGH); //<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
pinMode(greenLed, LOW); //<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
for(pos = 0; pos < 90; pos += 1) // goes from 0 degrees to 90 degrees <<<<<<<<<<<<<<<<<<<<
{ // in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(30); // waits 15ms for the servo to reach the position
}

pinMode(redLed, LOW); //<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
pinMode(greenLed, HIGH); //<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

for(pos >=90; pos < 180; pos += 1) // goes from 90 degrees to 180 degrees <<<<<<<<<<<<<<<<<<<<<
{ // in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(30); // waits 15ms for the servo to reach the position
}
pinMode(redLed, HIGH); //<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
pinMode(greenLed, LOW); //<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
for(pos = 0; pos < 90; pos += 1) // goes from 0 degrees to 90 degrees <<<<<<<<<<<<<<<<<<<<
{ // in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(30); // waits 15ms for the servo to reach the position
}

pinMode(redLed, LOW); //<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
pinMode(greenLed, HIGH); //<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

for(pos >=90; pos < 180; pos += 1) // goes from 90 degrees to 180 degrees <<<<<<<<<<<<<<<<<<<<<
{ // in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(30); // waits 15ms for the servo to reach the position
}

// <<<<<<<<<<<<<<<<<<<<<<<, and add similar for the return journey

}

One Last Thing.....
The Red and Green LED's only work properly in the 180 - 90 (Red) and 90 - 0 (Green) return sweep direction in the code last posted.
The 0 - 90 and 90 - 180 sweep is fast (resetting?) and the Red LED stays on during the entire 0 - 90 and 90 - 180 sweep.
Any assistance would be greatly appreciated.[/color]
Thanks Again and Happy New Year!

Not really answering but ...

Can you go back and for each of your posts, highlight the code sections and click the "Insert Code" tagging button above the edit window - it has a "#" logo?

It makes working with the code much easier.

tobar8:
Runaway Pancake, I appreciate your efforts but...

But nothing.
You didn't change the pinModes in your void loop () into digitalWrites.

When you want to go the other way then you need to try decrementing (viz pos--:), not incrementing (pos++;)

You didn't change the pinModes in your void loop () into digitalWrites.

Yeah you need to fix those errors I forced on you... The pinModes in setup() are correct; the ones in loop() need to be digitalWrites.

EUREKA! No Further Assistance is Necessary
Thank you all for your help.
Runaway Pancake you were right! Too bad I came to that conclusion on my own after reviewing each character in the code against the Arduino Sweep example in the library. I had "enough" code for a 2-way sweep but there were a few syntax errors. Also 3 lines of code were missing that was the key to proper function in the return sweep.
jimboAZ, I couldn't have gotten the LED's to work without your help. Thanks!

I have posed the working code below for those of you who may have a similar need. The only thing different is that the sketch I will use will employ greater values for each "delay" allowing for more realism in the installation project.

#include <Servo.h>

Servo myservo; // create servo object to control a servo
// a maximum of eight servo objects can be created

int pos = 0; // variable to store the servo position
int redLed= 8; //<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
int greenLed= 7; //<<<<<<<<<<<<<<<<<<<<<<<<<<

void setup()
{
myservo.attach(9); // attaches the servo on pin 9 to the servo object
pinMode(redLed, OUTPUT); //<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<,
pinMode(greenLed, OUTPUT); //<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

}

void loop()
{
pinMode(redLed, HIGH); //<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
pinMode(greenLed, LOW); //<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
for(pos = 0; pos < 90; pos += 1) // goes from 0 degrees to 90 degrees <<<<<<<<<<<<<<<<<<<<
{ // in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(30); // waits 15ms for the servo to reach the position
}

pinMode(redLed, LOW); //<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
pinMode(greenLed, HIGH); //<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
for(pos =90; pos < 180; pos += 1) // goes from 90 degrees to 180 degrees <<<<<<<<<<<<<<<<<<<<<
{ // in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(30); // waits 15ms for the servo to reach the position
}
pinMode(redLed, LOW); //<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
pinMode(greenLed, HIGH ); //<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
for(pos <=180; pos >= 90; pos -= 1) // goes from 180 degrees to 90 degrees <<<<<<<<<<<<<<<<<<<<
// in steps of 1 degree
{ // in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(30); // waits 15ms for the servo to reach the position
}
pinMode(redLed, HIGH); //<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
pinMode(greenLed, LOW); //<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
for(pos <=90; pos >= 0; pos -= 1) // goes from 90 degrees to 180 degrees <<<<<<<<<<<<<<<<<<<<<
{ // in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(30); // waits 15ms for the servo to reach the position
}
delay(2000);
};

You still have the faulty pinModes in loop()

Runaway Pancake, I appreciate your input but remember I'm new to this and I don't understand how to change the pinModes to digitalWrites. Could you please provide an example (i.e. "remove this code" replace with "this code) so I can make the proper changes? The servo and LED's are currently running in an endless loop on my desk as I type this and working as planned.
Please advise.
Thanks!

Just over-type them in the ide... backspace them out and re-type. I'm surprised it's working with those faults in there.

And please, when you post code here, select it all and hit the # icon above the :wink: and :sweat_smile: smilies so that code

appears like this