Atividade 4

RGB com 3 potenciômetros

/*

Controlando Led RGB com 3 potenciômetros de 10 K ohms

Resistores de 330 ohms

http://www.silvinopresa.com/how-to/arduino/rgb-led-common-annode-controlled-by-potentiometers-and-arduino/

Adapatado por César Bastos em 26 de outubro de 2018

*/

int redPin = 9; // configura o verde no pino PWM 9

int greenPin = 10; // configura o azul no pino PWM 10

int bluePin = 11; // configura o vermelho no pino PWM 11

int potPin_red = A0; // configura o potentiometer A0 para o vermelho

int potPin_green = A1;// configura o potentiometer A1 para o azul

int potPin_blue = A2; // configura o potentiometer A2 para o verde

int readValue_red; //declare variable to store the read value from the potentiometer which controls the red LED

int readValue_green; //declare variable to store the read value from the potentiometer which controls the green LED

int readValue_blue; //declare variable to store the read value from the potentiometer which controls the blue LED

int writeValue_red; //declare variable to send to the red LED

int writeValue_green; //declare variable to send to the green LED

int writeValue_blue; //declare variable to send to the blue LED

void setup() {

pinMode(potPin_red, INPUT); //set potentiometer for red LED as input

pinMode(potPin_green, INPUT); //set potentiometer for green LED as input

pinMode(potPin_blue, INPUT); //set potentiometer for blue LED as input

pinMode(redPin,OUTPUT); //set pin for red LED as output

pinMode(bluePin,OUTPUT); //set pin for green LED as output

pinMode(greenPin, OUTPUT); //set pin for blue LED as output

}

void loop() {

readValue_red = analogRead(potPin_red); //Read voltage from potentiometer to control red LED

readValue_green = analogRead(potPin_green); //Read voltage from potentiometer to control green LED

readValue_blue = analogRead(potPin_blue); //Read voltage from potentiometer to control blue LED

writeValue_red = (255./1023.)*readValue_red; //Calculate the value to write on the red LED (add point to change to float point)

writeValue_green = (255./1023.)*readValue_green; //Calculate the value to write on the green LED

writeValue_blue = (255./1023.)*readValue_blue; ///Calculate the value to write on the blue LED

analogWrite(redPin,writeValue_red); //write value to set the brightness of the red LED

analogWrite(greenPin,writeValue_green); //write value to set the brightness of the green LED

analogWrite(bluePin,writeValue_blue); //write value to set the brightness of the blue LED

}