Problem with "multitasking". Only works for the first 32 seconds [Arduino IDE]
So I have the below code compiled in my Tennsy 2.0. I hook up the serial monitor to see the analog temperature readings (not yet implemented 100%) and switch four led's on an off in a loop.
But, after 32 seconds (every time) the Teensy goes to "hyperspeed mode" and ignores the if(millis()... code. See monitor readings below the code.
Does anyone know why this happens?
So I have the below code compiled in my Tennsy 2.0. I hook up the serial monitor to see the analog temperature readings (not yet implemented 100%) and switch four led's on an off in a loop.
But, after 32 seconds (every time) the Teensy goes to "hyperspeed mode" and ignores the if(millis()... code. See monitor readings below the code.
Does anyone know why this happens?
Code:
static int firstPin = 2;
static int secondPin = 1;
static int thirdPin = 0;
static int fourthPin = 3;
static int pins[4] = {firstPin, secondPin, thirdPin, fourthPin};
int currentPin = 0;
int temperatureMillis = 0;
static int temperatureInterval = 1000;
int switchPinMillis = 0;
static int switchPinInterval = 100;
void setup() {
Serial.begin(38400);
pinMode(firstPin, OUTPUT);
pinMode(secondPin, OUTPUT);
pinMode(thirdPin, OUTPUT);
pinMode(fourthPin, OUTPUT);
digitalWrite(pins[0], HIGH);
switchPinMillis = millis();
}
float code;
float celsius;
void loop() {
if(millis() - temperatureMillis >= temperatureInterval) {
temperatureMillis = millis();
code = analogRead(10);
celsius = code;
Serial.print("temperature: ");
Serial.print(celsius);
Serial.print(" Celsius millis: ");
Serial.println(millis());
}
if(millis() - switchPinMillis >= switchPinInterval) {
switchPinMillis = millis();
digitalWrite(pins[currentPin], LOW);
if(currentPin == 3) {
currentPin = 0;
} else {
currentPin++;
}
digitalWrite(pins[currentPin], HIGH);
}
}
Code:
temperature: 149.00 Celsius millis: 1000
temperature: 149.00 Celsius millis: 2000
temperature: 149.00 Celsius millis: 3000
temperature: 151.00 Celsius millis: 4000
...
temperature: 150.00 Celsius millis: 31000
temperature: 149.00 Celsius millis: 32000
temperature: 150.00 Celsius millis: 33000
temperature: 151.00 Celsius millis: 33000
temperature: 151.00 Celsius millis: 33000
temperature: 150.00 Celsius millis: 33001
temperature: 150.00 Celsius millis: 33001
temperature: 149.00 Celsius millis: 33001
temperature: 149.00 Celsius millis: 33001
temperature: 148.00 Celsius millis: 33002
temperature: 150.00 Celsius millis: 33002
temperature: 148.00 Celsius millis: 33002
temperature: 148.00 Celsius millis: 33003
temperature: 149.00 Celsius millis: 33003
temperature: 149.00 Celsius millis: 33003
temperature: 150.00 Celsius millis: 33003
temperature: 149.00 Celsius millis: 33004
temperature: 148.00 Celsius millis: 33004
temperature: 149.00 Celsius millis: 33004
temperature: 149.00 Celsius millis: 33005
temperature: 150.00 Celsius millis: 33005
temperature: 148.00 Celsius millis: 33005
temperature: 149.00 Celsius millis: 33005
temperature: 149.00 Celsius millis: 33006
temperature: 148.00 Celsius millis: 33006
temperature: 149.00 Celsius millis: 33006
...