I am trying to use a pot to fade between two leds. Here is the code I have so far:
int redPin = 11;
int greenPin = 10;
int potPin = 1;
int val1;
int val2;
void setup() {
pinMode(redPin, OUTPUT);
pinMode(greenPin, OUTPUT);
}
void loop() {
{
val1 = analogRead(potPin);
val2 = analogRead(potPin);
val1 = map(val1, 0, 1023, 225, 0);
val2 = map(val2, 0, 1023, 0, 225);
analogWrite(redPin, HELP!);
analogWrite(greenPin, HELP!);
delay(5);
}
the places were i put HELP! is what i need help with... i want it to fade with pwm. So as i turn the pot, it fades between the red and the green...
The value you are reading from the potentiometer will be (nearly) the same in both cases, so it really isn't necessary to read it twice.
You are then mapping the potentiometer reading to a value between 0 and 255, or between 255 and 0.
The output values then are what need to be used on the PWM pins to control the level of brightness of the red and green LEDs.
Replace the 1st HELP! with val1 and the 2nd HELP! with val2.
I did it, and it didnt work...
int redPin = 11;
int greenPin = 10;
int potPin = 1;
int val1;
int val2;
void setup() {
pinMode(redPin, OUTPUT);
pinMode(greenPin, OUTPUT);
}
void loop() {
val1 = analogRead(potPin);
val2 = analogRead(potPin);
val1 = map(val1, 0, 1023, 225, 0);
val2 = map(val2, 0, 1023, 0, 225);
analogWrite(redPin, val1);
analogWrite(greenPin, val2);
delay(1);
}
It didn't, eh?
My crystal ball is at the cleaners, and I lent my Ouija board out. You'll have to help out a little.
What happened?
both lights are on, and i cant tell how bright, but they look full on to my eye...
Beginning to sound like a hardware problem - like the pot is not connected correctly.
A schematic or picture would be useful.
Try adding Serial.begin(9600); in setup, and Serial.print and Serial.println statements in loop, to print the analog pin reading before and after mapping.
The upper (or lower) limit on the to range should be 255, not 225, unless you have some reason for limiting the output to a smaller value.
Any particular reason for using analogue input one, and not analogue input zero?
OMG I am an idiot! i wrote 1 and hooked it to 0... Dee dee dee! I am a tard!
i changed the analog to 0 ,and changed the delay from 1ms to 10ms to fix a freezing issue. My only issue now is that when one is off, and one is full, the off one is flickering.
int redPin = 11;
int greenPin = 10;
int potPin = 0;
int val1;
int val2;
void setup() {
pinMode(redPin, OUTPUT);
pinMode(greenPin, OUTPUT);
}
void loop() {
val1 = analogRead(potPin);
val2 = analogRead(potPin);
val1 = map(val1, 0, 1023, 225, 0);
val2 = map(val2, 0, 1023, 0, 225);
analogWrite(redPin, val1);
analogWrite(greenPin, val2);
delay(10);
}
Does Red flicker when Green is full-brightness?
I'd imagine your input is going higher than 1023?
A ten bit ADC going higher than 1023?
BTW why 225 and not 255?
too much mapping, the values are complimentary
val1 = divide analog in by 4
val2 = 255-val1
D.
BTW why 225 and not 255?
ive always thought that an led goes 0 - 225...
So from what your saying, you do 0 - 255?
wow im stupid! well, thanks for telling me...