Go Down

Topic: timedAction library trouble (Read 849 times) previous topic - next topic

MrModest

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.

Code: [Select]

#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();

}
}



dhunt

You appear to be using delay() inside your action functions.  I would expect that to mess up your timing.  While delay() is executing nothing else happens.

MrModest

I must have a delay or the motor will not work properly. Without the delay in steps it will be told to spin so fast the motor will not spin at all issuing a high pitch noise at a frequency only a dog could possibly hear.

dhunt

If you want to be able to do several things at once, you will have to get rid of the delays.  There are other ways to make things happen at certain times, like the TimedAction library you are using.  Another option is to take a look at the BlinkWithoutDelay example sketch that comes with the IDE.

For example, you could make the moveforward() action get run every 4msecs instead of every 100msecs.  The "while (sensorVal >= 20)" loop in moveforward() will be a problem since it will loop forever if sensorVal is 20 or higher, so replace it with an "if (sensorVal >= 20)".  The looping is taken care of because moveforward() is called every 4msecs so the behaviour is the same as what you wanted when you used delay().

dhunt

Quote
I understand 2 functions cannot run at the same time but I thought the timedAction library worked like interrupts.


The timedAction library doesn't use interrupt, it uses the same technique demonstrated by the BlinkWithoutDelay sketch. When you call the check function the library compares the current time with the time it last ran the action and if the difference is more than the duration you created it with then it runs the action.

Go Up