Reverse DC motor direction at specific time

I have the Arduino Starter Kit and I'm on project 10 Zoetrope. I've removed the switch that changes the direction as I don't want the reverse to happen when I press a switch. I want the reverse to happen based on time.

Using code, i want to change the direction as the time passes by. For example i want the direction to be clockwise when i press the on/off switch then after 5 seconds change it to counterclockwise then after half a second change to clockwise then after 3 seconds change to counterclockwise etc.

I've search for solutions but I haven't found anything yet. I had no luck with the millis function as well.

Not everyone is familiar with the Starter Kit and the Zoetrope example. You need to post the code showing your attempt if you want much help. (Between [code]code tags[/code] please.)

It's not that hard.
Motor off.
Possibly a delay to allow the Motor to lose kinetic energy.
Set direction Clockwise
Delay for x seconds
Motor off.
Possibly a delay
Set direction Counter ClockWise
Delay for x seconds

Keep repeating...

I have attached the circuit illustration. I have removed the second switch as i don't need it.

This is my code.

#include <avr/interrupt.h>
#include <avr/io.h>   

const int controlPin1 = 2; // connected to pin 7 on the H-bridge
const int controlPin2 = 3; // connected to pin 2 on the H-bridge
const int enablePin = 9;   // connected to pin 1 on the H-bridge
const int directionSwitchPin = 4;  // connected to the switch for direction
const int onOffSwitchStateSwitchPin = 5; // connected to the switch for turning the motor on and off
const int potPin = A0;  // connected to the potentiometer's output

// create some variables to hold values from your inputs
int onOffSwitchState = 0;  // current state of the On/Off switch
int previousOnOffSwitchState = 0; // previous position of the on/off switch
int directionSwitchState = 0;  // current state of the direction switch
int previousDirectionSwitchState = 0;  // previous state of the direction switch

int motorEnabled = 0; // Turns the motor on/off
int motorSpeed = 0; // speed of the motor
int motorDirection = 1; // current direction of the motor

int endOfWhile = 0;

//##############

// Generally, you should use "unsigned long" for variables that hold time
// The value will quickly become too large for an int to store
unsigned long myTime = 0;        // will store last time LED was updated

// constants won't change :
const long interval = 1000;   

//##############

void setup() {
  // intialize the inputs and outputs
  pinMode(directionSwitchPin, INPUT);
  pinMode(onOffSwitchStateSwitchPin, INPUT);
  pinMode(controlPin1, OUTPUT);
  pinMode(controlPin2, OUTPUT);
  pinMode(enablePin, OUTPUT);

  // pull the enable pin LOW to start
  digitalWrite(enablePin, LOW);
}

void loop() 
{

  byte direction=0, second, prev=0;
  // read the value of the on/off switch
  onOffSwitchState = digitalRead(onOffSwitchStateSwitchPin);
  delay(1);

  // read the value of the direction switch
  directionSwitchState = digitalRead(directionSwitchPin);

  // read the value of the pot and divide by 4 to get
  // a value that can be used for PWM
  motorSpeed = analogRead(potPin) / 4;

 

  // if the direction button changed state since the last loop()
  //Code section not needed as we don't have direction button
  /*
  if (directionSwitchState != previousDirectionSwitchState) 
  {
    // change the value of motorDirection if pressed
    if (directionSwitchState == HIGH) 
    {
      motorDirection = !motorDirection;
    }
  }
  */

//######Main Code START#########

   // if the on/off button changed state since the last loop()
  if (onOffSwitchState != previousOnOffSwitchState) 
  {
    // change the value of motorEnabled if pressed
    if (onOffSwitchState == HIGH) 
    {
      motorEnabled = !motorEnabled;
    }

  }  

//######Main Code END#######

  //the motor spins clockwise with this function
  motor_right();

  //the motor spins counterclockwise with this function
  //of course i can't use the both at the same time as the last on will be used
  //motor_left();

  
  // if the motor is supposed to be on
  if (motorEnabled == 1) 
  {
    // PWM the enable pin to vary the speed
    analogWrite(enablePin, motorSpeed);
  } 
  else 
  { // if the motor is not supposed to be on
    //turn the motor off
    analogWrite(enablePin, 0);
  }
  // save the current On/Offswitch state as the previous
  previousDirectionSwitchState = directionSwitchState;
  // save the current switch state as the previous
  previousOnOffSwitchState = onOffSwitchState;
}

void motor_left()
{
  digitalWrite(controlPin1, HIGH);
  digitalWrite(controlPin2, LOW);
}

void motor_right()
{
  digitalWrite(controlPin1, LOW);
  digitalWrite(controlPin2, HIGH);  
}

void motor_stop()
{
  digitalWrite(controlPin1, LOW);
  digitalWrite(controlPin2, LOW);  
}

[code]

[/code]

welcome to the forum !

since you are new here, my we invite you to read 'how to use this forum'
especially #7 on how to post code.

to edit a post, you hit the edit button on the bottom right of the post you want to edit.
to add pcitures, you need to select modify.

to post a picture, the steps are very convoluted.
first attach your picture. need to be in the preview screen I think.

then post to the forum.

then modify your post
copy the url in your post
select the photo button and paste the url from the attachment.
save

it is a clunky 2 step process. first to save it as an attachment
then use the link from that attachment to post it into your post.

pavalispace:
I have the Arduino Starter Kit and I'm on project 10 Zoetrope. I've removed the switch that changes the direction as I don't want the reverse to happen when I press a switch. I want the reverse to happen based on time.

the solution you seek is hidden in blink without delay.

it is a bit of a steep learning curve as you have to use flags that are not really involved in timing.

We live in continuing moments of 'now'
you get the time. that immediately becomes 'then'
you check if now - them is the time you seek.

if now - then is say, 20 seconds or more
change motor direction
re-set then

there are a few ways to use that and there are some tutorials that help you understand it more easily.
but BWD or blink-without-delay is the programming you seek.

pavalispace:
Using code, i want to change the direction as the time passes by. For example i want the direction to be clockwise when i press the on/off switch then after 5 seconds change it to counterclockwise then after half a second change to clockwise then after 3 seconds change to counterclockwise etc.

blink without delay is based on

if (now - then > time_period)
do your thing

but 'time_period' if often a fixed value.
you can easly toggle between two values, or change to random values.
you can set time_period by means of a potentiometer.

from your post, it seems you want either random numbers or a pre-set sequence of values ?

@pavalispace , I guess I wasted my time mentioning that you need to use code tags, since you completely ignored my suggestion.
I'll leave you to it.

Sorry for the misunderstanding guys. I'm a new user here and i was using my phone when i made the post. Again sorry for any inconvenience.

pavalispace:
Sorry for the misunderstanding guys. I'm a new user here and i was using my phone when i made the post. Again sorry for any inconvenience.

Thanks for fixing it.