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
}