Offline
Newbie
Karma: 0
Posts: 32
|
 |
« Reply #15 on: March 03, 2012, 04:57:56 pm » |
#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?)
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
UK
Online
Brattain Member
Karma: 136
Posts: 18992
I don't think you connected the grounds, Dave.
|
 |
« Reply #16 on: March 03, 2012, 04:58:37 pm » |
As madworm says, there should be one statement ONLY in your setup () Serial.begin (bitRateOfyourChoice); Get rid of the green and blue code, it's just noise.
|
|
|
|
|
Logged
|
Pete, it's a fool looks for logic in the chambers of the human heart.
|
|
|
|
"The old Europe"
Offline
Edison Member
Karma: 0
Posts: 2003
Bootloaders suck!
|
 |
« Reply #17 on: March 03, 2012, 05:05:29 pm » |
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".
|
|
|
|
|
Logged
|
• 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!
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 32
|
 |
« Reply #18 on: March 03, 2012, 05:17:47 pm » |
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; 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;
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
UK
Online
Brattain Member
Karma: 136
Posts: 18992
I don't think you connected the grounds, Dave.
|
 |
« Reply #19 on: March 03, 2012, 05:23:19 pm » |
Compiled but obviously untested. 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); }
|
|
|
|
« Last Edit: March 03, 2012, 05:30:33 pm by AWOL »
|
Logged
|
Pete, it's a fool looks for logic in the chambers of the human heart.
|
|
|
|
Seattle, WA USA
Offline
Brattain Member
Karma: 311
Posts: 35470
Seattle, WA USA
|
 |
« Reply #20 on: March 03, 2012, 05:27:28 pm » |
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 <=.
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 32
|
 |
« Reply #21 on: March 03, 2012, 05:28:23 pm » |
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.
|
|
|
|
|
Logged
|
|
|
|
|
Seattle, WA USA
Offline
Brattain Member
Karma: 311
Posts: 35470
Seattle, WA USA
|
 |
« Reply #22 on: March 03, 2012, 06:05:34 pm » |
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.
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 32
|
 |
« Reply #23 on: March 03, 2012, 06:07:57 pm » |
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; 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
|
|
|
|
|
Logged
|
|
|
|
|
Seattle, WA USA
Offline
Brattain Member
Karma: 311
Posts: 35470
Seattle, WA USA
|
 |
« Reply #24 on: March 03, 2012, 06:13:03 pm » |
Seeing the output leading up to 255, and after 255, would be more useful...
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
UK
Online
Brattain Member
Karma: 136
Posts: 18992
I don't think you connected the grounds, Dave.
|
 |
« Reply #25 on: March 03, 2012, 06:14:56 pm » |
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.
|
|
|
|
« Last Edit: March 03, 2012, 06:16:41 pm by AWOL »
|
Logged
|
Pete, it's a fool looks for logic in the chambers of the human heart.
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 32
|
 |
« Reply #26 on: March 03, 2012, 06:25:20 pm » |
Seeing the output leading up to 255, and after 255, would be more useful...
Yeah sorry about that; 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? 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.
|
|
|
|
« Last Edit: March 03, 2012, 06:27:37 pm by chris1982 »
|
Logged
|
|
|
|
|
Global Moderator
UK
Online
Brattain Member
Karma: 136
Posts: 18992
I don't think you connected the grounds, Dave.
|
 |
« Reply #27 on: March 03, 2012, 06:31:16 pm » |
fadeAmountRed = -fadeAmountRed ;
Right up until the point where you read a new value of fadeAmountRed, next time through "loop"..
|
|
|
|
|
Logged
|
Pete, it's a fool looks for logic in the chambers of the human heart.
|
|
|
|
Seattle, WA USA
Offline
Brattain Member
Karma: 311
Posts: 35470
Seattle, WA USA
|
 |
« Reply #28 on: March 03, 2012, 06:41:02 pm » |
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.
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 32
|
 |
« Reply #29 on: March 03, 2012, 06:47:12 pm » |
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; 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.
|
|
|
|
« Last Edit: March 03, 2012, 06:55:37 pm by chris1982 »
|
Logged
|
|
|
|
|
|