" I want the motor to repeatedly run back and forth for a small amount very slowly, just to give some motion when it is at rest to hint at the potential for the installation, and I want the LEDs to be on very dim. Then if the sound level goes over a certain threshold, I want the motor to move back and forth at a fast speed and for the LEDs to flash. This 'alert' mode should last for a couple of seconds and then check to see if the sound level is still above the threshold before returning to the 'at rest' mode."
- the noise of the installation could set off the sound sensor, how can you deal with this?
- if we are using a stepper motor we can't tell it to go to a specific location or find out where it is without extra circuitry, we can only tell it to move forward or backward by a certain amount of steps. We may need a setup routine that sends it to the home location before we begin moving it.
- a DC motor will just run forward or back at different speeds, is this enough control? Do you need sensors to decide when to stop it/ change speed or direction?
- servos can be told to go to specific locations, but they can be noisy and most do not do full rotations without modification.
Basic pseudocode (based on servo):
motor goto 45 degrees
wait for it to get there
set LED on dim
motor goto 135 degrees over 2 seconds
motor goto 45 degrees over 2 seconds
check the sound sensor
if sound is over threshold trigger the 'alert' routine
else repeat 'atRest' routine
LEDs on for half a second
LEDs off for half a second
motor goto 180 degrees over 1 second
motor goto 0 degrees over 1 second
check the sound sensor
if sound is over threshold repeat 'alert' routine
else return to 'atRest' routine
- code is blocking, i.e. when we are moving a servo over a couple of seconds or telling an LED to stay on for a timed delay period, we could stop the rest of the code running. That means we can't check the sound sensor and may miss a sound going over the threshold. The sound sensor is an 'interrupt', we need to be able to check it during the current routine and without some form of multitasking, or doing more than one thing at once, we are sunk. The general way of getting around this is to set a timer up that keeps track of time, then we can check it to see if, for example, half a second has passed and we can now turn the LED off, or if it hasn't and we should carry on with rest of code. There is an excellent tutorial about multi-tasking on the Arduino from Adafruit here, which conveniently focusses on controlling motors and LEDs at the same time.