Skip to content

Instantly share code, notes, and snippets.

@mor10
Created February 7, 2018 02:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mor10/f4fb074e08602caff222d6f4f5dd46f2 to your computer and use it in GitHub Desktop.
Save mor10/f4fb074e08602caff222d6f4f5dd46f2 to your computer and use it in GitHub Desktop.
Arduino script for detecting three analog potentiometer inputs and output four LEDs: red, green, blue, and RGB
// Pin declarations
int potPinR= A0; // Declare potPinR to be analog pin A0
int potPinG= A1; // Declare potPinG to be analog pin A1
int potPinB= A2; // Declare potPinB to be analog pin A2
int LEDPinR= 9; // Declare LEDPinR to be arduino pin 9
int LEDPinG= 10; // Declare LEDPinR to be arduino pin 10
int LEDPinB= 11; // Declare LEDPinR to be arduino pin 11
// Variables
int readValueR; // Use to store value from red potentiometer
int writeValueR; // Use to write value to red LED
int readValueG; // Use to store value from green potentiometer
int writeValueG; // Use to write value to green LED
int readValueB; // Use to store value from blue potentiometer
int writeValueB; // Use to write value to blue LED
// I/O declarations
void setup() {
pinMode(potPinR, INPUT); //set potPinR to be an input
pinMode(potPinG, INPUT); //set potPinG to be an input
pinMode(potPinB, INPUT); //set potPinB to be an input
pinMode(LEDPinR, OUTPUT); //set LEDPinR to be an OUTPUT
pinMode(LEDPinG, OUTPUT); //set LEDPinG to be an OUTPUT
pinMode(LEDPinB, OUTPUT); //set LEDPinB to be an OUTPUT
Serial.begin(9600); // turn on Serial Port
}
// Run a loop;
void loop() {
readValueR = analogRead(potPinR); // Read the voltage from the red potentiometer
writeValueR = (255./1023.) * readValueR; // Calculate the write value for the red LED (between 0 and 255)
analogWrite(LEDPinR, writeValueR); // Write the value to the red LED
readValueG = analogRead(potPinG); // Read the voltage from the green potentiometer
writeValueG = (255./1023.) * readValueG; // Calculate the write value for the green LED (between 0 and 255)
analogWrite(LEDPinG, writeValueG); // Write the value to the green LED
readValueB = analogRead(potPinB); // Read the voltage from the blue potentiometer
writeValueB = (255./1023.) * readValueB; // Calculate the write value for the blue LED (between 0 and 255)
analogWrite(LEDPinB, writeValueB); // Write the value to the blue LED
// Print values in the terminal to see what's happening
Serial.print("LED write: ");
Serial.print("\t");
Serial.print(writeValueR);
Serial.print("\t");
Serial.print(writeValueG);
Serial.print("\t");
Serial.println(writeValueB);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment