Robot turning code

Hello , I'm just working on a little project, creating a little robot kind of thing out of things I already have at home and was wondering if someone could check if my code was feasible. Right now I'm just trying to set up some kind of sequence for it to turn (I'm just using a motor driver and motors)

Theres 2 motors (a and b) and they have pins set so that one of them is forward and the other is back (f and b respectively)

Heres what I have so far: (I still have to do some testing as to what time intervals will be best in order to have it turn the right degrees and such)

int motoraf = 5;
int motorab = 6;
int motorbf = 10;
int motorbb = 11;
const long eventTime = 2000;
const long eventTime_2 = 6000;
bool turn = false;

unsigned long previousTime = 0;
void setup() {
  // put your setup code here, to run once:
Serial.begin(9600);
pinMode(motoraf, OUTPUT);
pinMode(motorab, OUTPUT);
pinMode(motorbf, OUTPUT);
pinMode(motorbb, OUTPUT);
}

void loop() {
  // put your main code here, to run repeatedly:
 
digitalWrite(motoraf, HIGH);
digitalWrite (motorbf, HIGH);

if (turn == true) {
  unsigned long Ms = millis(); 
  digitalWrite(motoraf, LOW);
  digitalWrite(motorbf, LOW);
  digitalWrite(motorab, HIGH);
  digitalWrite(motorbb, HIGH);
  if (Ms - previousTime >= eventTime){
    previousTime = Ms;
    digitalWrite(motorab, LOW);
    digitalWrite(motorbb, LOW);
    digitalWrite(motorbf, HIGH);
    if (Ms - previousTime >= eventTime){
      previousTime = Ms;
      digitalWrite(motoraf, HIGH);
      if (Ms - previousTime >= eventTime_2){
        previousTime = Ms;
        digitalWrite(motorbf, LOW);
        if (Ms  - previousTime >= eventTime{
          previousTime = Ms;
          bool turn = false
    
    


        }
        
  }
  
}

}}}

Your post was MOVED to its current location as it is more suitable.

If you expect someone to read you mind, add comments to your code.

i don't see any code that sets "turn" to true

f

i don't see how the 2nd test of Ms - previousTime >= eventTime can ever be true since previousTime is just set to Ms

seems like you should have a state machine and a timer which advances the state each time the timer expires. each state can set the directions (speed?) of the motors

consider


#define MaxState = 3
int           state;

#define Period  1000
unsigned long msecLst;

// -----------------------------------------------------------------------------
void
loop ()
{
    unsigned long msec = millis ();

    if ((msec - msecLst) < Period)
        return;

    msecLst = msec;

    switch (state)  {
    case 0:
        Serial.println ("state 0");
        state++;
        break;

    case 1:
        Serial.println ("state 1");
        state++;
        break;
    case 2:
        Serial.println ("state 2");
        state = 0;
        break;
    }
}

// -----------------------------------------------------------------------------
void setup () {
    Serial.begin (9600);
}

Thanks for the help, with the state machine and timer would something like this be fitting?

int MaxState = 4
int          state;

unsigned long Period = 1000;
unsigned long msecLst;
int motoraf = 5;
int motorab = 6;
int motorbf = 11;
int motorbb = 12;

void setup() {
  // put your setup code here, to run once:
Serial.begin (9600);
pinMode(motoraf, OUTPUT);
pinMode(motorab, OUTPUT);
pinMode(motorbf, OUTPUT);
pinMode(motorbb, OUTPUT);
}

void loop() {
  // put your main code here, to run repeatedly:
unsigned long msec = millis ();
//is it time to proceed to the next part of the code?
    if ((msec - msecLst) < Period)
//no it isn't
        return;
//now it is
    msecLst = msec;
//stating the code
    switch (state)  {
    
    
    case 0: //first part it should do
        Serial.println ("state 0");
        digitalWrite(motorab, HIGH); //left wheel going backwards
        digitalWrite(motorbb, HIGH); //right wheel going backwards
        Period = 2000UL; //time it should go backwards for
        state++; //adds 1 to the value of state so it can go to the next one
        break;

    case 1:
        Serial.println ("state 1");
        digitalWrite(motorab, LOW); //stops left wheel going back
        digitalWrite(motorbb, LOW); //stops right wheel going back
        digitalWrite(motorbf, HIGH); //makes only right wheel go forward
        Period = 2000UL; //time the right wheel should turn for, should make body go left
        state++;
        break;
    
    case 2:
        Serial.println ("state 2");
        digitalWrite(motoraf, HIGH); //makes left wheel join the right, should make rover go forward normally
        Period = 6000UL; //time it should take going forward (away from obstacle)
        state++;
        break;

    case 3:
        Serial.println ("state 3");
        digitalWrite(motorbf, LOW); //stops turning right wheel forward (left wheel turning so it should turn left back into original position)
        Period = 2000;; //time it should take turning back into og rotation
        state = 0;
        break;
    }}

i believe so. does it make more sense to you?

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.