I'm fairly proficient with Arduino, but this sketch truly blows my mind. It's a very simple sketch that uses millis() and 3 if() statements. The thing that's driving me nuts is the output in the serial monitor. As soon as the sketch starts, the monitor is saying that x = 2 and red = 3000 even though those values should be x = 1 and red = 0 until time= 5000. The first 3 values in the serial monitor should be ( 0, 1, 0 ) until time = 5000 but I'm getting ( 0, 2, 3000). Can someone please explain why this is happening?
int red;
unsigned long time;
int x;
void setup()
{
Serial.begin(9600);
}
void loop()
{
time = millis();
if (time < 5000) {
x = 1; }
if (time >= 5000) {
x = 2; }
if (x = 1) {
red = 0; }
if (x = 2) {
red = 3000; }
Serial.println(time);
Serial.println(x);
Serial.println(red);
delay(500);
}
int red;
unsigned long time;
int x;
void setup()
{
Serial.begin(9600);
}
void loop()
{
time = millis();
if (time < 5000) {
x = 1;
}
else {
x = 2;
}
if (x == 1) {
red = 0;
}
else if (x == 2) {
red = 3000;
}
Serial.println(time);
Serial.println(x);
Serial.println(red);
delay(500);
}
Look too to the way that the code is wrote (mostly where the text start in each line). You can do this autocratically chosing the option auto-format from the menu tools from de Arduino IDE.
EDIT:
That is an incredibly stupid mistake. I am truly embarrassed.
That's why AWOL answer like he did. But in my opinion everyone need to start some day, and everyone can make "stupid" mistakes. So don't worry .
I originally had if/else statements in the sketch, but I removed them because I was getting frustrated. This sketch used to be fairly complicated, but I just kept stripping it down to find the bug. I should use the auto format feature more often. I think I just prefer my own way of formatting for some reason. Like I said, this was an incredibly stupid mistake. Thank you so much for finding pointing out the issue.
The most embarrassing this about this is that I fully understand create delays without the delay() function. This was just a stupid mistake. This is an example of code I've written. It isn't beautiful or refined, but it works.
int potPin = 2;
long pm = 0;
long pm1 = 0;
long interval, interval1, L1;
unsigned long cm, cm1;
int val, b, b1, c, A, B;
float a, a1;
int Q = 1;
int Q1 = 1;
int x = 1;
int x1 = 1;
#include "Tlc5940.h"
void setup()
{
Tlc.init();
Serial.begin(9600);
}
void loop()
{
Tlc.clear();
interval = analogRead(potPin);
cm = millis();
if (cm - pm > interval) {
pm = cm;
if (Q == 1) {
a = 1.1;
b = 1;
x = ((x * a)+1);
if (x == 4440) Q = 2;
}
if (Q == 2) {
a = 0.9091;
b = -1;
x = ((x * a)+b);
if (x < 2) {
Q = 1;
L1 = millis(); }
}
int y = map(x, 0, 4036, 4036, 0);
Tlc.set(3, x*.4);
Tlc.set(0, y*.4);
Serial.println(x);
//Serial.println(y);
Tlc.update();
luisilva,
I didn't know using else if() would give you a complier error if you use on = sign. That's very nice to know.
AWOL,
I can't believe I forgot the two = signs, but I guess crap like that happens once in a while. I'm slightly hung over so that might be part of the problem