I am writing code for a robot and I am trying to use the timedAction library with my Arduino Mega 2560. In the code below I am trying to implement 2 functions simultaneously. I have read how to use the library but I am having a bit of trouble with it. I want to be able to move my 2 stepper motors at the same time (not yet implemented) but first I want to achieve properly reading from my range sensor and at least move one stepper motor. Right now, I start my code below in my arduino and it reads the first value from my range sensor then after that it runs the motor very smoothly but never again receives a range sensor value. Most of the code below has nothing to do with just getting both of those functions to work, but just look into my range function and my motor forward function. I set up an instance for each function with the library at the top of my code. I am not sure where to put the enable and disables. I have tried following examples for this but I am having trouble still. Please help if you can. Look at the bottom of the code I posted to see where I used the .check method and at the top of my code I set up the timing. Not sure why the motor forward and range sensor function does not work at the same time(or at least appear to). I understand 2 functions cannot run at the same time but I thought the timedAction library worked like interrupts.
#include <TimedAction.h> // for interrupts
#include <avr/wdt.h> // for watchdog timer function
TimedAction forwardAction = TimedAction(100,moveForward);
TimedAction rangeAction = TimedAction(500,readrange);
void readrange(); // range sensor
void accel(); // accelerometer
int motorPins[] = {2, 3, 4, 5};
int motorPins2[] = {6, 7, 8, 9};
int count = 0;
int count2 = 0;
int delayTime = 4;
int val = 0;
int analogInPin = A0; // pin for range sensor
const int xPin = 9; // x pin for accelerometer
const int yPin = 10; // y pin for accelerometer
float sensorValue; // value of sensor
void setup() {
Serial.begin(9600); //9600 baud rate
for (count = 0; count < 4; count++) {
pinMode(motorPins[count], OUTPUT);
}
pinMode(analogInPin, INPUT); // range sensor analog input 0
pinMode(xPin, INPUT); // accelerometer x digital input 9
pinMode(yPin, INPUT); // accelerometer y digital input 10
}
void moveBackward() {
if ((count2 == 0) || (count2 == 1)) {
count2 = 16;
}
count2>>=1;
for (count = 3; count >= 0; count--) {
digitalWrite(motorPins[3 - count], count2>>count&0x01);
}
delay(delayTime);
}
void readrange(){ // range sensor function
int temp; // temporary variable for storing value
temp = analogRead(analogInPin);
sensorValue = (6787.0 /((float)temp - 3.0)) - 4.0; // conversion to cm
// approximately 10 to 80 cm
if(sensorValue <= 80 && sensorValue >= 0){
Serial.println(sensorValue);
}
else {
Serial.println("out of range");
}
delay(300);
}
void moveForward() {
while (sensorValue >= 20){
if ((count2 == 0) || (count2 == 1)) {
count2 = 16;
}
count2>>=1;
for (count = 3; count >= 0; count--) {
digitalWrite(motorPins[count], count2>>count&0x01);
}
delay(delayTime);
}
}
// check() will execute the function is is refered to if the interval has been met.
// enable() will enable "thread" execution.
// disable() will disable "thread" execution.
// reset() will reset the time relative to the TimedAction
// setInterval(unsigned int newInterval) will change the interval of this TimedAction.
void loop() {
val = 1000;
//val = 400;
//moveForward();
//readrange(); // range sensor function
//accel(); // accelerometer function
rangeAction.check(); // should run both functions at the timing I specified right? it doesn't.
forwardAction.check();
if (val < 480) {
moveBackward();
}
}