Hello, world!
First time poster to this forum; long history of using forums - so I did my best to follow rules and find my own solution, but I admit defeat. I'm baffled.
So this is my first Arduino sketch, after analyzing examples, code documentation and some online tutorials.
The real purpose is, for reasons unexplained here, to use analogWrite to set a needle on a meter to an arbitrary position, then using RANDOM(3) to 'jitter' that needle up/down (or unchanged) for a WHILE loop.
I've randomized the seed (using analogRead(0)) and by using the serial monitor I get the expected results of a random(3) ... namely, random digits from 0 to 2.
However, as soon as I add in an IF statement, the otherwise identical code now returns the digit 2 every single time.
Why?
Code below is clipped from my real sketch, note the two functions JITTER and BROKEN_JITTER are identical except for the single IF statement.
/*
Meter Test - Excerpt for forum post example of unexpected random number results.
Actual sketch moves a needle on a meter to a set position,
then randomly "jitters" needle a variable amount for a period of time.
Most code removed, to show only the issue I can't figure out:
The random(2) results, serial printed to monitor, are as expected random integers
from 0 to 2 UNLESS/UNTIL an IF statement is added (commented back in, see below) at
which point the result is always 2.
*/
int meter = 9; // the PWM pin the meter is attached to
int led = 11; // Pin the LED is attached to
int jitter_count = 0; // For the WHILE loop count
int meter_level = 0; // Value to analogWrite to move needle to desired position
int new_meter_level = 0; // Value used to increment/decrement meter randomly based on random_roll value
int random_roll = 0; // Random number used to decide to increment, decrement, or unchange needle position
// the setup routine runs once when you press reset:
void setup() {
pinMode(led, OUTPUT);
pinMode(meter, OUTPUT);
Serial.begin(9600);
randomSeed(analogRead(0));
}
// the loop routine runs over and over again forever:
void loop() {
// Set needle position, light LED, and then call jitter function.
meter_level = 36;
analogWrite(meter,meter_level);
analogWrite(led, 200);
jitter();
// Reset needle to zero position, serial print "RESET", and outen der blinkenlight.
meter_level = 0;
Serial.println();
Serial.println("Reset meter, LED off and 3 second pause...");
Serial.println();
analogWrite(meter,0);
analogWrite(led, 0);
delay (5000);
// Set needle, light LED and call the broken_jitter function
meter_level = 36;
analogWrite(meter,meter_level);
analogWrite(led, 200);
broken_jitter();
// Reset needle to zero position, serial print my bafflement, and outen der blinkenlight.
meter_level = 0;
Serial.println();
Serial.println("WHY WAS IT ALWAYS 2?!?!?!?");
Serial.println("(10 second pause before repeat.)");
Serial.println();
analogWrite(meter,0);
analogWrite(led, 0);
delay (10000);
// END loop
}
//JITTER function
void jitter()
{
Serial.println();
Serial.println("Jittering meter needle...");
Serial.println();
// FORUM POST NOTE: This function has no IF statement, it just writes out the results, which
// are the expected random results from 0 to 2.
while (jitter_count < 25) {
random_roll = random(3);
// Print results of the above line(s) in the serial monitor, zero random_roll,
// and increment WHILE counter:
Serial.print("random_roll = ");
Serial.println(random_roll);
delay(300);
random_roll = 0;
jitter_count++;
}
// Zero the count.
jitter_count = 0;
//End of JITTER function.
}
//BROKEN_JITTER function.
void broken_jitter()
{
Serial.println();
Serial.println("Jittering meter needle...but it's broken!");
Serial.println();
// FORUM POST NOTE: Exact same function (copy-paste) with exception of adding a single IF
// statement below. Now, every result is 2.
while (jitter_count < 25) {
random_roll = random(3);
if (random_roll = 2) {
Serial.print("Why is it always 2?!? "); //Print what I already know - it will always be 2 now
//that I add this IF statement. WHY?!?
//
//Note: In actual sketch, would have IF statements moving
//needle -1, +1, or no change based on random_roll result.
}
// Print results of the above line(s) in the serial monitor, zero random_roll,
// and increment WHILE counter:
Serial.print("random_roll = ");
Serial.println(random_roll);
delay(300);
random_roll = 0;
jitter_count++;
}
jitter_count = 0;
}
//end BROKEN JITTER function
Thanks very much in advance, and please, go easy on me...
-JD