Go Down

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

chris1982

Code: [Select]
#include <SoftwareSerial.h>

SoftwareSerial mySerial(2, 3);

void setup() 
{
  Serial.begin(57600);
  Serial.println("Goodnight moon!");

  // set the data rate for the SoftwareSerial port
  mySerial.begin(4800);
  mySerial.println("Hello, world?");
}

void loop() // run over and over
{
  if (mySerial.available())
    Serial.write(mySerial.read());
  if (Serial.available())
    mySerial.write(Serial.read());
}


Is not all that easy for me to understand, but i'll go away and try and work out how to make that output the fadeAmountRed value (i presume that's what you are getting at?)

AWOL

As madworm says, there should be one statement ONLY in your setup ()
Code: [Select]
Serial.begin (bitRateOfyourChoice);

Get rid of the green and blue code, it's just noise.
"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.

madworm

Let it output whatever you need.

Not just the fade-amount, but also the counting variable itself. Only with the latter you will more easily see if it gets stuck somewhere. And of course it is the variable that gets checked by your if-statement.

And I recommend to have a look at the example code: "Files-->Examples-->Basics-->DigitalReadSerial".
• Upload doesn't work? Do a loop-back test.
• There's absolutely NO excuse for not having an ISP!
• Your AVR needs a brain surgery? Use the online FUSE calculator.
My projects: RGB LED matrix, RGB LED ring, various ATtiny gadgets...
• Microsoft is not the answer. It is the question, and the answer is NO!

chris1982

Quote
And I recommend to have a look at the example code: "Files-->Examples-->Basics-->DigitalReadSerial".


I don't seem to have those. I have files>examples> but no basics within that subsection. I guess i'll try reinstalling. Have they been added recently?

I've minimised the code slightly, i still don't seem to be getting any joy;

Code: [Select]
int sensorValueRed = 0; //store the value from the potentiometer
int brightnessRed = 0;    // how bright the LED is
int fadeAmountRed = 0;    // how many points to fade the LED by
int ledPinRed = 3;      //pin LED is attached to
int sensorPinRed = 0;   //pin for the potentiometer
#include <SoftwareSerial.h>

void setup()  {
  // declare pin 9 to be an output:
  Serial.begin(57600);
  pinMode(ledPinRed, OUTPUT);
}

void loop()  {
  sensorValueRed = analogRead(sensorPinRed); //read the value from the sensor as sensorValue
  analogWrite(ledPinRed, brightnessRed);    //set ledPinRed to brightnessRed
  fadeAmountRed = sensorValueRed/300;       //set the fadeamountred from potentiometer value


  brightnessRed = brightnessRed + fadeAmountRed;   // change the brightness for next time through the loop:

 
  if (brightnessRed < 0 || brightnessRed > 255)   // reverse the direction of the fading at the ends of the fade:
  {
    fadeAmountRed = -fadeAmountRed ;
  }   
   Serial.write(sensorValueRed);
     delay(1);                                     //delay 1 ms
}


The seriel monitor outputs gobbledygook even when i set the bitrate correctly. Maybe i've picked too difficult a project to start with after doing the basic examples.

O well, i'll get it working eventually. So far i can make it do this;

http://www.youtube.com/watch?v=6mBG5CyCyF0&list=UUvhD47CGShC95XVnlR0E_EA&index=2&feature=plcp

AWOL

#19
Mar 03, 2012, 11:23 pm Last Edit: Mar 03, 2012, 11:30 pm by AWOL Reason: 1
Compiled but obviously untested.
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);
}

"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

Quote
Doesn't seem to help at all.

The idea was that you test for at or beyond the limits, not just at, or beyond, the limits. So, the >= is good, but the < should be <=.

chris1982

Ok i've got the seriel monitor outputting sensible results now, i think. I can see how it should hopefully make trying to fix the problem easier - i'll let you know how it goes, thanks.

PaulS

Seeing what the Arduino is doing is essential to understanding what it is thinking. Now that you have that working, keep adding more Serial.print() statements (with identifying information) until you do fully understand what is happening. Good luck.

Come on back if you get stuck/don't understand why something is happening.

chris1982


Compiled but obviously untested.


Thanks, it was very informative to see code similar to mine written by someone more experienced.

Interestingly your code behaves exactly the same as mine in terms of the appearance of the LED. It ramps up in brightness from zero to full brightness as i want then snaps back to zero again.

I coped the output of the seriel monitor in case it sheds any light;

Code: [Select]
Fade 3
Brightness 3
Value 1023
Fade 3
Brightness 6
Value 1023
Fade 3
Brightness 9
Value 1023
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 1022
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



PaulS

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

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