Hello everyone,
i'm a beginner in arduino and coding, so i apologize in advance for any silly questions.
I just recently learned that if you want to simulate multitasking you need to get rid of the delay() functionality and start using timers instead. I learned the blink led without delay example and started playing around with multiple outputs "simoultaneously". I have two leds one dc motor, one servo and a temperature sensor.
The main issue is how to change the time on, time off cycle of the dc motor. Basically changing the duty cycle from now 50% to another value. I found this code online, but i don't fully understand it:
#define LED_PIN 13
#define LED_ON 800 //milliseconds
#define LED_OFF 200
unsigned long ms; //time from millis()
unsigned long msLast; //last time the LED changed state
boolean ledState; //current LED state
void setup(void)
{
pinMode(LED_PIN, OUTPUT);
}
void loop(void)
{
ms = millis();
blinkLED();
}
void blinkLED(void)
{
if (ms - msLast > (ledState ? LED_ON : LED_OFF)) {
digitalWrite(LED_PIN, ledState = !ledState);
msLast = ms;
}
}
This part confuses me
(ledState ? LED_ON : LED_OFF)
(LED_PIN, ledState = !ledState);
can anyone explain what is happening here? I haven't tried the code out yet, because i'd like to first understand it.
Since we're talking about timers, i'd like to ask a few more things.
This is a code i wrote for running multiple things at once.
int op1 = 6;
int op2 = 9;
int temp = 0;
int aread;
int rfun;
long intOp1 = 5000;
long intOp2 = 1000;
int op1State = LOW;
int op2State = LOW;
long prevOp1 = 0;
long prevOp2 = 0;
int readTemp()
{
if (millis() - prevOp2 > intOp2) {
prevOp2 = millis();
aread = analogRead(temp);
return aread;
}
}
void setup()
{
Serial.begin(9600);
pinMode(op1, OUTPUT);
pinMode(op2, OUTPUT);
}
void loop()
{
rfun = readTemp();
Serial.println(rfun);
if (rfun > 0)
{
if (millis() - prevOp1 > intOp1) {
prevOp1 = millis();
if (op1State == LOW)
op1State = HIGH;
else
op1State = LOW;
digitalWrite(op1, op1State);
}
}
}
the readTemp function runs an analogRead, writes it in the serial monitor and triggers a led blink.
If you look at the serial monitor while it's running you get this kind of readout:
0
0
...
0
0
100 // after one second
0
0
...
0
0
167 // after another second
0
0
...
0
0
234 // after another second
...
as i understand it the main loop() basically cycles very fast and the analogRead() takes readings only every second, because the interval for the function is set to 1000 milliseconds.
Thing is that the led acts accordingly...meaning it lights up only when the read out is at a certain value (in this case i set it to "above o").
If i set the read interval to let's say 1 millisecond then it acts seemingly constant.
Does this mean that the blinking timing is dependent on the sensor read interval and the led interval? If we change the led with a motor, would this mean that the motor get's "double PWM" instructions?
- run motor when the readout > 0
- run the motor at desired PWM frequency and duty cycle
With using delay functionality it's more linear and understandable:
- read sensor
- if sensor > value
3.run motor at desired PWM
What i'm probably trying to ask...is this the right way to control outputs based on an input signal while multitasking?
I'm probably just over-thinking this "problem" or failing to see the big picture.
I greatly appreciate any help or comments. Thanks.