How to "fake" multitasking on an 8-bit processor?

I have an Arduino with an Adafruit motor shield as part of my robot. I want to start the engines at the same time when I play a tone on a piezoelectric element.

The problem is that I don’t quite know how to fake / simulate multitasking in my code. I tried something like this:

void goForward(int duration) { for (int i; i<duration; i++) { tl.run(FORWARD); tr.run(BACKWARD); bl.run(FORWARD); br.run(BACKWARD); counter++; if (counter%4==0) { piezo != piezo; } delay(1); } } 

This starts the engines, however it does not create a tone on my piezoelectric element. What would be the best way to program this so that the piezo can be turned on / off at 440 Hz when the motor commands are working?

+4
source share
3 answers

I do not know the Arduino board, but most microcontrollers have a timer interrupt. Have you tried to set such a timer? In the timer interrupt handler, you can enable / disable the piezoelectric element, while the main loop can be used to control the engine.

+3
source

Regular Arduino boards have only 3 timers. This limits the possibility of "multitasking" 3 "threads". Arduino Mega has 16 timers. In other words, the Arduino Mega might be your solution. No?

0
source

Create a scheduler for creating parallel tasks and use two timers for your engines, and if you have a third, use it for a buzzer.

0
source

All Articles