I tried it, up until step four everything works but when I add the last bit of code it nothing happens. Here is the code so far:
const int pwm = 9 ;
const int in_1 = 3 ;
const int in_2 = 2 ;
const int inPin = 11;
void setup()
{
pinMode(pwm,OUTPUT) ;
pinMode(in_1,OUTPUT) ;
pinMode(in_2,OUTPUT) ;
pinMode(inPin, INPUT);
}
void moveMotor()
{
digitalWrite(in_1,HIGH) ;
digitalWrite(in_2,LOW) ;
analogWrite(pwm,255) ;
delay(3000) ;
digitalWrite(in_1,HIGH) ;
digitalWrite(in_2,HIGH) ;
delay(1000) ;
digitalWrite(in_1,LOW) ;
digitalWrite(in_2,HIGH) ;
delay(3000) ;
digitalWrite(in_1,HIGH) ;
digitalWrite(in_2,HIGH) ;
delay(1000) ;
}
void loop() {
static bool motorMayRun = false;
byte buttonState;
static byte previousButtonState = buttonState;
buttonState = digitalRead(inPin);
if (buttonState == LOW and previousButtonState == HIGH) {
if (motorMayRun == false) {
motorMayRun = true;
}
else {
motorMayRun = false;
}
}
if (motorMayRun == true) {
moveMotor();
}
}
I have tried it in a more basic way but that only made the moveMotor(); function go once when I press the button...
I feel that it is close but thus far no cigar..
This is the code that activates the moveMotor() once:
const int pwm = 9 ;
const int in_1 = 3 ;
const int in_2 = 2 ;
const int inPin = 11;
int buttonState = 0;
int previousButtonState = 0;
void setup()
{
pinMode(pwm,OUTPUT) ;
pinMode(in_1,OUTPUT) ;
pinMode(in_2,OUTPUT) ;
pinMode(inPin, INPUT);
}
void moveMotor()
{
digitalWrite(in_1,HIGH) ;
digitalWrite(in_2,LOW) ;
analogWrite(pwm,255) ;
delay(3000) ;
digitalWrite(in_1,HIGH) ;
digitalWrite(in_2,HIGH) ;
delay(1000) ;
digitalWrite(in_1,LOW) ;
digitalWrite(in_2,HIGH) ;
delay(3000) ;
digitalWrite(in_1,HIGH) ;
digitalWrite(in_2,HIGH) ;
delay(1000) ;
}
void pauzeMotor()
{
digitalWrite(in_1,HIGH) ;
digitalWrite(in_2,HIGH) ;
delay(1000) ;
}
void loop() {
buttonState = digitalRead(inPin);
delay(1);
if(buttonState != previousButtonState){
if(buttonState == HIGH){
moveMotor();
}
else pauzeMotor();
}
previousButtonState = buttonState;
}
[code/]