recientemente empecé a utilizar programar en Arduino por una materia que tengo en la universidad y todavía no me queda claro qué sucede cuando no inicializamos una variable.
el maestro nos pasó un código de ejemplo y no comprendí muy bien:
unsigned long changeTime;
no está inicializada y más adelante se utiliza en una operación arithmetical:
if (state == HIGH && (millis() - changeTime) > 5000)
mi pregunta es: si no inicializamos el valor de la variable, automáticamente tiene un valor de 0? o por qué se pudo usar "millis() - changeTime" ? además se encuentra
millis() = changeTime;
hasta lo último del código del programa y no entiendo muy bien la razón del porque se puede utilizar al principio si no está inicializada como operación aritmetica.
también he visto que cuando se trata de millis(); o micros(); se declara con "unsigned int". ¿siempre cuando se trate de esas dos funciones hay que declararlas como unsigned int"?
y ya por último, cuando imprimo los valores de este código:
// Project 4 - Interactive Traffic Lights
int carRed = 12; // assign the car lights
int carYellow = 11;
int carGreen = 10;
int pedRed = 9; // assign the pedestrian lights
int pedGreen = 8;
int button = 2; // button pin
int crossTime = 5000; // time allowed to cross
unsigned long changeTime; // time since button pressed
void setup() {
Serial.begin(9600);
}
void loop() {
Serial.println(millis());
delay(500);
}
me aparece así:
¿de dónde salen esas últimas unidades?
100.1
150.2
200.2
si se supone que son 500 milisegundos cerrados?
el código completo es:
// Project 4 - Interactive Traffic Lights
int carRed = 12; // assign the car lights
int carYellow = 11;
int carGreen = 10;
int pedRed = 9; // assign the pedestrian lights
int pedGreen = 8;
int button = 2; // button pin
int crossTime = 5000; // time allowed to cross
unsigned long changeTime; // time since button pressed
void setup() {
pinMode(carRed, OUTPUT);
pinMode(carYellow, OUTPUT);
pinMode(carGreen, OUTPUT);
pinMode(pedRed, OUTPUT);
pinMode(pedGreen, OUTPUT);
pinMode(button, INPUT); // button on pin 2
// turn on the green light
digitalWrite(carGreen, HIGH);
digitalWrite(pedRed, HIGH);
}
void loop() {
int state = digitalRead(button);
/* check if button is pressed and it is
over 5 seconds since last button press */
if (state == HIGH && (millis() - changeTime) > 5000) {
// Call the function to change the lights
changeLights();
}
}
void changeLights() {
digitalWrite(carGreen, LOW); // green off
digitalWrite(carYellow, HIGH); // yellow on
delay(2000); // wait 2 seconds
digitalWrite(carYellow, LOW); // yellow off
digitalWrite(carRed, HIGH); // red on
delay(1000); // wait 1 second till its safe
digitalWrite(pedRed, LOW); // ped red off
digitalWrite(pedGreen, HIGH); // ped green on
delay(crossTime); // wait for preset time period
// flash the ped green
for (int x=0; x<10; x++) {
digitalWrite(pedGreen, HIGH);
delay(250);
digitalWrite(pedGreen, LOW);
delay(250);
}
// turn ped red on
digitalWrite(pedRed, HIGH);
delay(500);
digitalWrite(carYellow, HIGH); // yellow on
digitalWrite(carRed, LOW); // red off
delay(1000);
digitalWrite(carGreen, HIGH);
digitalWrite(carYellow, LOW); // yellow off
// record the time since last change of lights
changeTime = millis();
// then return to the main program loop
}