Adding on/off switch to motor

Hi there! I am an absolute beginner at this so probably this will be easy to solve but;

I have found a code to make a DC motor move clockwise and counterclockwise at different intervals (see attachment). Now I want to add an on/off switch to the breadboard to toggle this function on or off. So far I've managed to make the whole loop act out once when I press the switch, but I want a push of the button to activate an endless loop, and another push to stop it.

Here's my code:

const int pwm = 9 ;	
const int in_1 = 3 ;
const int in_2 = 2 ;


void setup()
{
pinMode(pwm,OUTPUT) ;  	
pinMode(in_1,OUTPUT) ; 
pinMode(in_2,OUTPUT) ;
}

void loop()
{

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) ;
 }

Please help!

motor l:r.pdf (19.6 KB)

The program in the Original Post does not make any attempt to read a switch.

The simplest thing might be to evolve the program in a few stages like this. First change the name of the function from loop() to moveMotor(). Second, create a new function called loop() like this

void loop() {
   moveMotor();
}

Third, upload that program and confirm that it works exactly like the original.

Fourth (assuming step 3 works OK) change the code in loop() so it is like this

void loop() {
   static bool motorMayRun = false;
   byte buttonState;
   static byte previousButtonState = buttonState;
   buttonState = digitalRead(buttonPin);
   if (buttonState == LOW and previousButtonState == HIGH) {
      if (motorMayRun == false) {
           motorMayRun = true;
      }
      else {
          motorMayRun = false;
      }
    }
    if (motorMayRun == true) {
       moveMotor();
    }
}

...R

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/]

stefveldhuis:
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:

I just noticed that you have

pinMode(inPin, INPUT);

My code was written assuming you have

pinMode(inPin, INPUT_PULLUP);

which means that the pin is normally HIGH and you should have the switch wired so that pressing it pulls the line to GND

...R

First of all thank you so much for your help! It really taught me a lot.
Unfortunately it still does not do anything. Here is the code i have now:

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_PULLUP);
}

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();
    }
}

and attached is a photo of my setup. What am i missing? [/code]

Images from Reply #4 so we don't have to download them. See this Simple Image Posting Guide

...R

stefveldhuis:
First of all thank you so much for your help! It really taught me a lot.
Unfortunately it still does not do anything. Here is the code i have now:

Pictures of the hardware don't provide a reliable indication of how things are connected. Just make a simple pencil drawing with the connection points clearly labelled and post a photo of the drawing.

If you are using INPUT_PULLUP the switch should be wired like this (note that there is no external resistor)

I/O pin --------- switch ------------- GND

You also need to be careful that you have that little push-button switch oriented the correct way.

...R

You also need to be careful that you have that little push-button switch oriented the correct way.

The easiest way to ensure this is to wire diagonally across the button switch.

The way the buttons are designed, two of the four legs are always connected and two of them are switched. When you connect diagonally across the switch you will always get a switched pair.