Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I'm using an Arduino Uno and trying to code together 2 parts of a school project. Both parts work fine separately but when combined, only part 1 works. Part 1: Involves 1 button switch, 2 LEDs where one stays on and one stays off, the button changes which is on or off. Part 2: Servo controlled by potentiometer

I've determined that the servo only ends up working when the button is pressed down, which shouldn't be happening, the potentiometer/servo code shouldn't be dependent on the code for the LEDs. I'm definitely a beginner at this and don't understand a lot of what the code is doing, so forgive me for any redundancies. Any help is much appreciated.

#include <Servo.h>
Servo myServo;
int const potPin = A0;
int potVal;
int angle;
int switchState;
int lastSwitchState = 0;
const int bluePin = 3;
const int yellowPin = 4;
const int button = 2;
int bluelight = LOW;
int yellowlight = HIGH;

void setup(){
  pinMode(bluePin, OUTPUT); //blue LED
  pinMode(yellowPin, OUTPUT); //yellow LED
  pinMode(button, INPUT); //switch
  myServo.attach(9);
  Serial.begin(9600);
}
void loop(){
  // PART 1 - this is the only part that seems to be working now?
  switchState = digitalRead(button);
  while (digitalRead(button)==LOW); 
  if (digitalRead(button)==LOW){ 
  bluelight=!bluelight;
    digitalWrite(bluePin, bluelight);
    digitalWrite(yellowPin, yellowlight);
  }
  else{
  if (switchState=!lastSwitchState) {
    yellowlight=!yellowlight;
    bluelight=!bluelight;
  digitalWrite(yellowPin, yellowlight);
  digitalWrite(bluePin, bluelight);
  }
  }
  // PART 2 - Only works when button is pressed? 
    potVal = analogRead(potPin);
  Serial.print("potVal: ");
  Serial.print(potVal);
  angle = map(potVal, 0, 1023, 0, 179);
  Serial.print(", angle: ");
  Serial.println(angle);
  myServo.write(angle);

  delay(250); //wait for a quarter second
  }
share|improve this question

3 Answers 3

Make correction in If else statement. The problem while checking condition.And pot pin(A0) in interdependent of bluePin & yellowPin . so individually they are working fine.

share|improve this answer

you are doing:

while (digitalRead(button)==LOW);

that code will stop execution of every until the digitalRead became TRUE, so the code after that statement is execute ONLY after the button becomes HIGH

as far as i can undestrand that while is unecessary as the following IF is already tanking care of what to do in case of LOW or HIGH reading. Also the delay(250); seems to be the only part of the code that will block your execution, once the while is removed, so i think that is your only blocking-bug :)

share|improve this answer
    
When I take out the while statement it doesn't work, so I'm not sure what I'm doing wrong in the if else statement. Any suggestions what to change it to so that the switch toggles which light stays on? –  user3350391 Feb 26 '14 at 19:24
    
Now you have to correct your if statement. If button is low, do nothing, if high switch the led status. Note that that way if you keep pressed the button led will continously turn on and off. Also button logic may be inverted, depends on whiring –  lesto Feb 26 '14 at 19:33

This is your error:

while (digitalRead(button)==LOW);

the program stuck on this loop all time this buttom it's not pressed, then when you pressed it continues, but entering into the code which works when this button it's high (true)

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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