I created a learning circuit in which I am trying to use an NPN transistor in conjunction with a RGB LED.
When current through the base is full, the led outputs green, when its 66% full, its yellow, and when its 33% or less, its Red.
I am not sure this circuit works since when I go through the range of values for setting, I am seeing really strange results such as cycling from 800 to 100 in val. Any feedback would be appreciated.
I have 330ohm resistors to the RBG legs in the LED, and one 330 ohm resistor in the base leg of the transistor.
Thanks!
int RED_PIN = 11;
int GREEN_PIN = 10;
int BLUE_PIN = 9;
int BASE = 6;
int EMITTER = A0;
int setting = 5; //255 is max, 0 is lowvoid setup() {
// put your setup code here, to run once:
pinMode(RED_PIN, OUTPUT);
pinMode(GREEN_PIN, OUTPUT);
pinMode(BLUE_PIN, OUTPUT);
pinMode(BASE, OUTPUT);
Serial.begin(9600);
}void loop() {
analogWrite(BASE, setting);
delay(100);
int val = analogRead(EMITTER);Serial.println(val);
if(val<341)
{
digitalWrite(RED_PIN, LOW);
digitalWrite(GREEN_PIN, HIGH);
digitalWrite(BLUE_PIN, LOW);
}
else if(val<682)
{
digitalWrite(RED_PIN, HIGH);
digitalWrite(GREEN_PIN, LOW);
digitalWrite(BLUE_PIN, HIGH);
}
else if(val<=1023)
{
digitalWrite(RED_PIN, HIGH);
digitalWrite(GREEN_PIN, LOW);
digitalWrite(BLUE_PIN, LOW);
}
else{
digitalWrite(RED_PIN, HIGH);
digitalWrite(GREEN_PIN, HIGH);
digitalWrite(BLUE_PIN, HIGH);
}}
}