//EMNG2021 - LAB 5 - SAMI AL-HAKKAK
//11/15/2023
// Define and declare pins
const int potPin = A0; // Pin where potentiometer is connected
const int ledPin1 = 9; // Pin where Red LED is connected
const int ledPin2 = 5; // Pin where Yellow LED is connected
const int ledPin3 = 1; // Pin where Green LED is connected
void setup() {
pinMode(ledPin1, OUTPUT); //Set ledPin1 as OUTPUT
pinMode(ledPin2, OUTPUT); //Set ledPin2 as OUTPUT
pinMode(ledPin3, OUTPUT); //Set ledPin3 as OUTPUT
}
void loop() {
int potValue = analogRead(potPin); // Read the value from potentiometer
int ledSwitch = map(potValue, 0 ,1023, 0, 255); // Map potentiometer position scale to arduino PWM range
// Switch on specific LED base on potentiometer position (divided equally into 3 zones)
if(ledSwitch >= 0 && ledSwitch <= 84){
digitalWrite(ledPin1, 255);
digitalWrite(ledPin2, 0);
digitalWrite(ledPin3, 0);
delay(10); // Short delay for stability
}
else if(ledSwitch >= 85 && ledSwitch <= 169){
digitalWrite(ledPin2, 255);
digitalWrite(ledPin1, 0);
digitalWrite(ledPin3, 0);
delay(10); // Short delay for stability
}
else if (ledSwitch >= 170 && ledSwitch <= 255){
digitalWrite(ledPin3, 255);
digitalWrite(ledPin1, 0);
digitalWrite(ledPin2, 0);
delay(10); // Short delay for stability
}
delay(10); // Short delay for stability
//Serial.println(ledSwitch);
}