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