This code is written in C++ for use with an Arduino microcontroller. It sets up three output pins for an RGB LED, as well as an input pin for a potentiometer. The setup function configures the serial communication and sets the pin modes for the LED pins. The loop function reads the value of the potentiometer and sends it to the serial monitor. It also uses the potentiometer value to control the color of the RGB LED by turning on and off the individual red, yellow, and green pins. The delay function is used to pause the loop for 500 milliseconds before repeating the process.
const int RED_PIN = 5; // Declare constant
const int YELLOW_PIN = 3; // Declare constant
const int GREEN_PIN = 4; // Declare constant
const int pot_pin = A0; // Declare constant
int pot_val; // Declare variable
void setup() {
Serial.begin(9600); // Start serial communication
pinMode(RED_PIN, OUTPUT); // Set pin mode
pinMode(YELLOW_PIN, OUTPUT); // Set pin mode
pinMode(GREEN_PIN, OUTPUT); // Set pin mode
}
void loop() {
pot_val = analogRead(pot_pin); // Read potentiometer value
Serial.println(pot_val); // Print value to serial monitor
if(pot_val <= 341){
digitalWrite(RED_PIN, HIGH); // Turn on red LED
digitalWrite(YELLOW_PIN, LOW); // Turn off yellow LED
digitalWrite(GREEN_PIN, LOW); // Turn off green LED
}
else if(pot_val <= 682){
digitalWrite(RED_PIN, LOW); // Turn off red LED
digitalWrite(YELLOW_PIN, HIGH); // Turn on yellow LED
digitalWrite(GREEN_PIN, LOW); // Turn off green LED
}
else{
digitalWrite(RED_PIN, LOW); // Turn off red LED
digitalWrite(YELLOW_PIN, LOW); // Turn off yellow LED
digitalWrite(GREEN_PIN, HIGH); // Turn on green LED
}
delay(500); // Wait for 500 milliseconds
}