Go Down

Topic: help with RGB LED fading (noob) (Read 2 times) previous topic - next topic

AWOL

#25
Mar 04, 2012, 12:14 am Last Edit: Mar 04, 2012, 12:16 am by AWOL Reason: 1
Quote
Interestingly your code behaves exactly the same as mine in terms of the appearance of the LED

That's because it is your code

Oops, just noticed some irrelevant global variables.
Never mind, same effect.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

chris1982

#26
Mar 04, 2012, 12:25 am Last Edit: Mar 04, 2012, 12:27 am by chris1982 Reason: 1

Seeing the output leading up to 255, and after 255, would be more useful...


Yeah sorry about that;

Code: [Select]
Fade 3
Brightness 3
Value 1023
Fade 3
Brightness 6
Value 1023
Fade 3
Brightness 9
Value 1022
Fade 3
Brightness 12
Value 1023
Fade 3
Brightness 15
Value 1023
Fade 3
Brightness 18
Value 1023
Fade 3
Brightness 21
Value 1023
Fade 3
Brightness 24
Value 1023
Fade 3
Brightness 27
Value 1023
Fade 3
Brightness 30
Value 1023
Fade 3
Brightness 33
Value 1023
Fade 3
Brightness 36
Value 1023
Fade 3
Brightness 39
Value 1023
Fade 3
Brightness 42
Value 1023
Fade 3
Brightness 45
Value 1023
Fade 3
Brightness 48
Value 1023
Fade 3
Brightness 51
Value 1023
Fade 3
Brightness 54
Value 1023
Fade 3
Brightness 57
Value 1023
Fade 3
Brightness 60
Value 1023
Fade 3
Brightness 63
Value 1023
Fade 3
Brightness 66
Value 1023
Fade 3
Brightness 69
Value 1023
Fade 3
Brightness 72
Value 1023
Fade 3
Brightness 75
Value 1023
Fade 3
Brightness 78
Value 1023
Fade 3
Brightness 81
Value 1023
Fade 3
Brightness 84
Value 1023
Fade 3
Brightness 87
Value 1023
Fade 3
Brightness 90
Value 1023
Fade 3
Brightness 93
Value 1023
Fade 3
Brightness 96
Value 1023
Fade 3
Brightness 99
Value 1023
Fade 3
Brightness 102
Value 1023
Fade 3
Brightness 105
Value 1023
Fade 3
Brightness 108
Value 1023
Fade 3
Brightness 111
Value 1023
Fade 3
Brightness 114
Value 1023
Fade 3
Brightness 117
Value 1023
Fade 3
Brightness 120
Value 1023
Fade 3
Brightness 123
Value 1023
Fade 3
Brightness 126
Value 1023
Fade 3
Brightness 129
Value 1023
Fade 3
Brightness 132
Value 1023
Fade 3
Brightness 135
Value 1023
Fade 3
Brightness 138
Value 1023
Fade 3
Brightness 141
Value 1023
Fade 3
Brightness 144
Value 1023
Fade 3
Brightness 147
Value 1023
Fade 3
Brightness 150
Value 1023
Fade 3
Brightness 153
Value 1023
Fade 3
Brightness 156
Value 1023
Fade 3
Brightness 159
Value 1023
Fade 3
Brightness 162
Value 1023
Fade 3
Brightness 165
Value 1023
Fade 3
Brightness 168
Value 1023
Fade 3
Brightness 171
Value 1023
Fade 3
Brightness 174
Value 1023
Fade 3
Brightness 177
Value 1023
Fade 3
Brightness 180
Value 1023
Fade 3
Brightness 183
Value 1023
Fade 3
Brightness 186
Value 1023
Fade 3
Brightness 189
Value 1023
Fade 3
Brightness 192
Value 1023
Fade 3
Brightness 195
Value 1023
Fade 3
Brightness 198
Value 1023
Fade 3
Brightness 201
Value 1023
Fade 3
Brightness 204
Value 1023
Fade 3
Brightness 207
Value 1023
Fade 3
Brightness 210
Value 1023
Fade 3
Brightness 213
Value 1023
Fade 3
Brightness 216
Value 1023
Fade 3
Brightness 219
Value 1023
Fade 3
Brightness 222
Value 1023
Fade 3
Brightness 225
Value 1023
Fade 3
Brightness 228
Value 1023
Fade 3
Brightness 231
Value 1023
Fade 3
Brightness 234
Value 1023
Fade 3
Brightness 237
Value 1023
Fade 3
Brightness 240
Value 1023
Fade 3
Brightness 243
Value 1023
Fade 3
Brightness 246
Value 1023
Fade 3
Brightness 249
Value 1023
Fade 3
Brightness 252
Value 1023
Fade -3
Brightness 255
Value 1023
Fade -3
Brightness 258
Value 1023
Fade -3
Brightness 261
Value 1023
Fade -3
Brightness 264
Value 1023
Fade -3
Brightness 267
Value 1023
Fade -3
Brightness 270
Value 1023
Fade -3
Brightness 273
Value 1023
Fade -3
Brightness 276
Value 1023
Fade -3
Brightness 279
Value 1023
Fade -3
Brightness 282
Value 1023
Fade -3
Brightness 285
Value 1023
Fade -3
Brightness 288
Value 1023
Fade -3
Brightness 291
Value 1023
Fade -3
Brightness 294




Looking at this i get the impression the problem in the code is something to do with the brightness value not being reduced down as it should. The fade goes to -3 from 3 at the 255 point but the brightness keeps climbing. I'm guessing that this is because the brightness is set from the fadeAmountRed value which is set immediately before hand as equal to sensorValueRed/300. The fadeAmountRed value is turned to the negative after the 'if (brightnessRed <= 0 || brightnessRed >= 255)' section but then when the loop reruns the fadeAmountRed is set back to a + value from sensorValueRed/300 before being added to the brightness.

Does that sound plausible?

Code: [Select]
int sensorValueRed;
int brightnessRed;    // how bright the LED is
int fadeAmountRed;    // how many points to fade the LED by
const int ledPinRed = 3;      //pin LED is attached to
const int sensorPinRed = 0;   //pin for the potentiometer

void setup()  
{
 Serial.begin(57600);
}

void loop()  
{
 int sensorValueRed = analogRead(sensorPinRed);
 analogWrite(ledPinRed, brightnessRed);  
 fadeAmountRed = sensorValueRed/300;

 brightnessRed = brightnessRed + fadeAmountRed;

 
if (brightnessRed <= 0 || brightnessRed >= 255)  
 {
   fadeAmountRed = -fadeAmountRed ;
 }  
  Serial.print ("Value ");
  Serial.println(sensorValueRed);
  Serial.print ("Fade ");
  Serial.println(fadeAmountRed);
  Serial.print ("Brightness ");
  Serial.println(brightnessRed);
}


Also i tried to add the <= and >= as suggested by paul

I'm guessing i need to reorder the code somehow.

AWOL

Code: [Select]
fadeAmountRed = -fadeAmountRed ;

Right up until the point where you read a new value of fadeAmountRed, next time through "loop"..
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

PaulS

Code: [Select]
  fadeAmountRed = sensorValueRed/300;
When brightnesss exceeds 255, you set fad amount to negative. Then, on the next pass through loop, you make it positive again, then set it negative. But, you set it negative AFTER you increment by the positive amount.

chris1982

#29
Mar 04, 2012, 12:47 am Last Edit: Mar 04, 2012, 12:55 am by chris1982 Reason: 1
I finally worked that one out myself as well, but only after reading through all the help provided, thanks.

I tried to fix it by altering the order of commands;

Code: [Select]

int sensorValueRed;
int brightnessRed;    // how bright the LED is
int fadeAmountRed;    // how many points to fade the LED by
const int ledPinRed = 3;      //pin LED is attached to
const int sensorPinRed = 0;   //pin for the potentiometer

void setup() 
{
  Serial.begin(57600);
}

void loop() 
{
  int sensorValueRed = analogRead(sensorPinRed);
  analogWrite(ledPinRed, brightnessRed); 
  fadeAmountRed = sensorValueRed/300;

 

 
if (brightnessRed <= 0 || brightnessRed >= 255)   
  {
    fadeAmountRed = -fadeAmountRed ;
   
  }
brightnessRed = brightnessRed + fadeAmountRed;   
   Serial.print ("Value ");
   Serial.println(sensorValueRed);
   Serial.print ("Fade ");
   Serial.println(fadeAmountRed);
   Serial.print ("Brightness ");
   Serial.println(brightnessRed);
}


But now the LED fades in reverse order.

Go Up