Hello guys! I am new to Arduino(today I received my Arduino Uno Starter Kit!), and I am really excited. I've been waiting over a year to get my hands on one of these :D.
Well I started with the easy part first, learning from a few tutorials. I blinked a led for the first time, then I tried doing it with 4 leds in a row and... it's just wonderful to see that programming can have effects in the real world.
But now I am stuck with something I wanted to do next(I'm not really a guy to respect the order of tutorials :), I basically inspired myself from a potentiometer control and a button reading tutorial to do this).
I wanted to control a RGB LED like this: pressing a button adjusts the "color channel" for the potentiometer to control.
I don't know what's wrong with my code, but I have a funny result: pressing the button has no effect(it should even blink de 13th input LED, but it stays open). The Red light is on whatever I do. Turning the potentiometer seems to light up the blue colour, and when that's maxed out, it will light the green one until all of the 3 colours are maxed out.
Here's a diagram of my experiment:
And here's the code:
/*
AnalogReadSerial
Reads an analog input on pin 0, prints the result to the serial monitor
This example code is in the public domain.
*/
int ledPinIND= 13;
int Rpin= 1;
int Gpin= 2;
int Bpin= 3;
int patincrement= 0;
int Rval=0, Gval=0, Bval=0;
void setup() {
pinMode(ledPinIND, OUTPUT);
pinMode(Rpin, OUTPUT);
pinMode(Gpin, OUTPUT);
pinMode(Bpin, OUTPUT);
pinMode(0, INPUT);
Serial.begin(9600);
}
void loop() {
digitalWrite(ledPinIND, LOW);
int sensorValue = digitalRead(0);
if(sensorValue==1){ digitalWrite(ledPinIND, HIGH);
delay(150);
patincrement++; if(patincrement>2){patincrement=0;}
}
int potValue = analogRead(A0);
Serial.println(potValue, DEC);
if(patincrement==0){
Rval= potValue/5;}
if(patincrement==1){
Gval= potValue/5;}
if(patincrement==2){
Bval= potValue/5;}
analogWrite(Rpin, Rval); analogWrite(Gpin, Gval); analogWrite(Bpin, Bval);
}
Also, I partly understand why we need a 10K resistor for the button, but I don't understand why do the LEDs need a 220 ohm resistor(I still used 220ohm resistors on the RGB because I am confused and don't want to break my Arduino). Lowering the voltage and current even for the other colors besides blue should not exceed 100 ohms( for 5V output, which is what Arduino supplies from what I understand).