Obstacle Voiding Robot

I’m trying to make an obstacle void robot using an IR sensor, stepper motors, and micros. I’m at a loss in trying to get the robot to reverse direction after tripping the sensor. This program uses case structure with the first case being the robot moving forward and the second case of the robot reversing direction for a set period of time. However, it just quickly jumps between forward and reverse, it isn’t reversing for a set period of time. I’m using a ShieldBuddy that has three cores, but is programable using the Arduino IDE. The IR sensor resides in the second core (Core 1). The main program is in the first core and uses the sensor value. Void straight() and void turn() both function properly. Any help would be appreciated, including a different approach.

Chuck

//trying to establish program to avoid obstacles without using delay.
//Go forward until IR sensor hits threshold, then turn for two seconds
//Both the straight and turn programs work separately


const int motor1 =  5;      // the number of the step pin
const int motor2 = 8;       // the number of the step pin
int motor1State = LOW;             // Set the initial state
int motor2State = LOW;


// constants won't change:
const long interval = 400;           // Sets the speed of the stepper motors
const long turnSpeed = 500;
unsigned long currentMillis = millis();
unsigned long currentPrevious = 0;
unsigned long previousMicros = 0;
unsigned long previousCycle = 0;
//unsigned long pause = 0;
const long TimeForward = 5000;
int forward = 2000;
int rotate = 5000;

int sensorpin = 2;                 // analog pin used to connect the sharp sensor
int val = 0;                 // variable to store the values from sensor(initially zero)
void setup() {

  SerialASC.begin(9600);
  //Motor 1
  pinMode(6, OUTPUT); // Enable
  pinMode(5, OUTPUT); // Step
  pinMode(12, OUTPUT); // Dir (pin 4 was damaged - switched to 12)
  digitalWrite(6, LOW); // Set Enable low
  digitalWrite(12, HIGH); // Dir  Motor  1

  //Motor 2
  pinMode(10, OUTPUT); // Enable
  pinMode(8, OUTPUT); // Step  Motor 2
  pinMode(9, OUTPUT); // Dir  Motor  2
  digitalWrite(10, LOW); // Set Enable low
  digitalWrite(9, HIGH); // Dir  Motor  2
  pinMode(13, OUTPUT);

}
void loop()
{ int y;
  unsigned long currentCycle = millis();
 
  if (val <= 350) //val comes from the process running in Core 1
    y = 0;

  else
    y = 1;

  switch (y)
  {
    case 0:  //On
      straight();
      break;

    case 1:  //Off
      if (currentCycle - previousCycle <= forward)
      {
        turn();
      }
      else
      { previousCycle = currentCycle;
      }
      break;
  }
}


void straight() {
  digitalWrite(9, HIGH); // Dir  Motor  2

  {
    unsigned long currentMicros = micros();

    if (currentMicros - previousMicros >= interval)
    {
      previousMicros = currentMicros;  // save the last time

      if (motor1State == LOW && motor2State == LOW)
      {
        motor1State = HIGH;
        motor2State = HIGH;
      }
      else
      {
        motor1State = LOW;
        motor2State = LOW;
      }
      digitalWrite(motor1, motor1State);
      digitalWrite(motor2, motor2State);
    }

  }


}

void turn() {
  digitalWrite(9, LOW); // Dir  Motor  2
  unsigned long currentMicros = micros();

  if (currentMicros - previousMicros >= turnSpeed)
  {
    previousMicros = currentMicros;  // save the last time you blinked the LED

    if (motor1State == LOW && motor2State == LOW) // if the LED is off turn it on and vice-versa:
    {
      motor1State = HIGH;
      motor2State = HIGH;
    }
    else
    {
      motor1State = LOW;
      motor2State = LOW;
    }
    digitalWrite(motor1, motor1State);
    digitalWrite(motor2, motor2State);
  }




}

//*** Core 1 ***/

void setup1()
{

}


void loop1()
{ val = analogRead(sensorpin);       // reads the value of the sharp sensor
  //  SerialASC.println(val);            // prints the value of the sensor to the serial monitor
  //  delay(100);                    // wait for this much time before printing next value

}




/*** Core 2 ***/

/* CPU2 Uninitialised Data */
StartOfUninitialised_CPU2_Variables
/* Put your CPU2 fast access variables that have no initial values here e.g. uint32 CPU2_var; */
EndOfUninitialised_CPU2_Variables

/* CPU2 Initialised Data */
StartOfInitialised_CPU2_Variables
/* Put your CPU2 fast access variables that have an initial value here e.g. uint32 CPU2_var_init = 1; */
EndOfInitialised_CPU2_Variables


void setup2() {
  // put your setup code for core 2 here, to run once:


}


void loop2() {
  // put your main code for core 2 here, to run repeatedly:


}

Where does the reversing happen in the code ?
Are you deliberately using micros() for timing ?

Half a millisecond?

UKHeliBob:
Where does the reversing happen in the code ?

The reversing for now takes place in void() rotate

Are you deliberately using micros() for timing ? Yes

If you can see a "void()rotate" you're not looking at the code you've shown us.

Steve

Sorry, void turn(), not rotate.

What do you expect a motor will do in 1/2000th of a second?

AWOL,

Unless I'm missing your point, the microseconds are used to control the rotation and speed of the stepper motors. Millis were used to keep the reverse motion of the robot for a period of time.

Chuck

Oh dear, I think I’ve just worked out that you’re using loop() with variables currentCycle and previousCycle for the timing and the value that is supposed to control how long you reverse for is called “forward”. My brain now hurts and I’ve given up trying to see if anything else odd is going on.

Steve