wanting a small motor to drive forwards for 2 min then back for 2mins

Hi all

very new at this so sorry if it rubish
This is will be for a cabinet auto open and close

I am wanting to be able to push a momentary switch once and a motor drive forward for (1 min for example)
then wait until I require, then push the button again then the motor drive backwards for (1 min for example)
next use
push button drives forward and so forth.

Here is my attempt all the individual components work but when I try and add them together they don't work

I am using a uno and a motor shield L2986HN(DM)

button wire into pin 7 and gnd
motor into A+A- on the motor shield powered by the 5v

sorry if i have missed anything

//pin definitions
int motorHigh = 12;
int motorBreak = 9;
int buttonPin = 7;

// Globe variables
int toggleState;
int lastButtonState =1;
long unsigned int lastPress;
int debounceTime = 20;
int motorRunD;

void setup() {
pinMode(buttonPin, INPUT_PULLUP);
pinMode(12, OUTPUT); //Initiates Motor Channel A pin
pinMode(9, OUTPUT); //Initiates Brake Channel A pin
pinMode(LED_BUILTIN, OUTPUT);
Serial.begin(9600);
// put your setup code here, to run once:

}

void loop() {{
int buttonState = digitalRead(buttonPin); //read the button pin and stores it as buttonstate (0 or 1)

if((millis() - lastPress) > debounceTime) //if the time between the last buttonChange is greater than the debounceTime
{
lastPress = millis(); //update lastPress

if(buttonState == 0 && lastButtonState == 1) //if button is pressed and was released last change
{
toggleState =! toggleState; //toggle the LED state

toggleState =! toggleState; //toggle the LED state
digitalWrite(LED_BUILTIN, toggleState);

Serial.print("Button press");
Serial.print(buttonState);
Serial.print("motor");
Serial.print(motorRunD);

lastButtonState = 0 ; //record the lastButtonState
}

if(buttonState == 1 && lastButtonState == 0) //if button is not pressed, and was pressed last change
{
lastButtonState = 1; //record the lastButtonState
Serial.print("Button relese");
Serial.print(buttonState);

}}

}
if (toggleState == 1){
//forward @ full speed
digitalWrite(12, HIGH); //Establishes forward direction of Channel A
digitalWrite(9, LOW); //Disengage the Brake for Channel A
analogWrite(3, 255); //Spins the motor on Channel A at full speed

delay(2000);

digitalWrite(9, HIGH); //Eengage the Brake for Channel A

delay(400);

if (toggleState == 0){
//backward @ full speed
digitalWrite(12,LOW); //Establishes forward direction of Channel A
digitalWrite(9, LOW); //Disengage the Brake for Channel A
analogWrite(3, 255); //Spins the motor on Channel A at full speed

delay(2000);

digitalWrite(9, HIGH); //Eengage the Brake for Channel A

delay(400);
}}}

thanks for you time

they don't work

Which means..?

Please remember to use code tags when posting code

Use CTRL T to format your code.
Attach your ‘complete’ sketch between code tags

[code]Paste your sketch here[/code]

What do these two lines do ?
toggleState =! toggleState; //toggle the LED state

toggleState =! toggleState; //toggle the LED state

larryd:
What do these two lines do ?
toggleState =! toggleState; //toggle the LED state

toggleState =! toggleState; //toggle the LED state

They negate the current variable. More commonly written as (so as not to look like a compound assignment)

   toggleState = !toggleState;                 //toggle the LED state
   toggleState = ! toggleState;                 //toggle the LED state

but to the compiler, they are all the same.

blh64:
They negate the current variable.

I suspect the point of larryd's question was that those two statements in succession effectively do nothing.

dougp:
I suspect the point of larryd's question was that those two statements in succession effectively do nothing.

That's not strictly true (no pun intended).
Double NOTs have been used (I think the purists will frown) to ensure that a non-zero value becomes a 'pure' Boolean '1'

My tutorial on Multi-tasking in Arduino and How to write Timers and Delays in Arduino are applicable to your case
The times tutorial has small millisDelay class that simplifies running multiple timers/delays

blh64:
They negate the current variable. More commonly written as (so as not to look like a compound assignment)

   toggleState = !toggleState;                 //toggle the LED state

toggleState = ! toggleState;                //toggle the LED state



but to the compiler, they are all the same.

This is to show me on first press the I have a 1 and second push it gives me a 0, so I can go forward or backwards. The note I should of reoved as it was off another sketch
To note the motor is not running as intended.
I am not after 2 separate thing to happen I want the motor to run at the push of a momentary for a set time then at a second push of the the same button the motor to run in reverse for the same amount of time. Then and the 3rd push run forward so on so forth.
Sorry I may be barking up the wrong tree.

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