Trouble incorporating push button into DC speed control with potentiometer

Hi all,

I am very new to arduino and am trying to figure out how to incorporate a push button into my circuit. The circuit is a DC motor with its speech controlled by a 10K potentiometer. The idea of the push button is that it will immediately cut power to the motor when pressed, and then pressing the button again should restart the motor. I have attached of schematic of my current circuit and my code is below. I am a bit lost on where to start/go from here. Thanks in advance!

int pwmPin = 12; 
int pot = A0;
int c1 = 0;   
int c2 = 0;  

void setup()  // setup loop
{
  pinMode(pwmPin, OUTPUT); 
  pinMode(pot, INPUT);  
}

void loop()
{
  c2= analogRead(pot); 
  c1= 1024-c2;         // subtracts c2 from 1000 ans saves the result in c1
  digitalWrite(pwmPin, HIGH); 
  delayMicroseconds(c1);   
  digitalWrite(pwmPin, LOW);  
  delayMicroseconds(c2);  
}

Here is an example using the state change detection method to toggle the LED on pin 13 and the boolean variable runState that you can incorporate into your code. Use the runState state to choose whether to run the motor or stop it.

// by C Goulding aka groundFungus


const byte  buttonPin = 4;    // the pin that the pushbutton is attached to
const byte ledPin = 13;       // the pin that the LED is attached to

boolean buttonState = 0;         // current state of the button
boolean lastButtonState = 0;     // previous state of the button

boolean runState = false;

void setup()
{
   // initialize the button pin as a input with internal pullup enabled
   pinMode(buttonPin, INPUT_PULLUP);
   // initialize the LED as an output:
   pinMode(ledPin, OUTPUT);
   // initialize serial communication:
   Serial.begin(9600);
}

void loop()
{
   static unsigned long timer = 0;
   unsigned long interval = 50;  // check switch 20 times per second
   if (millis() - timer >= interval)
   {
      timer = millis();
      // read the pushbutton input pin:
      buttonState = digitalRead(buttonPin);
      // compare the new buttonState to its previous state
      if (buttonState != lastButtonState)
      {
         if (buttonState == LOW)
         {
            // if the current state is LOW then the button
            // went from off to on:
            digitalWrite(ledPin, !digitalRead(ledPin)); // toggle the output
            runState = !runState;
         }
      }
      // save the current state as the last state,
      //for next time through the loop
      lastButtonState = buttonState;
   }
}
int pwmPin = 12;

Pin 12 is not a PWM pin on the Uno.

Karma point for reading the instructions before posting in order to insert code. The forum software inserted the image for you. :grinning:

But ...

Missing series resistor in the base connection of the transistor.

You connect a button between an input pin and ground using INPUT_PULLUP; it reads LOW when the button is pressed. Unfortunately that is where it becomes tricky as the button must be "de-bounced" with the code groundFungus cites (or mine!).

On stuff like this, its nice to have the "STOP THE MACHINE!' button actually disconnect the power manually. Then have the code do a gradual, "Oh my lord! What happened?" reboot. The first time you get in a situation where the processor goes down and that's the only way to shut off the monster.. You will become a believer as well.

-jim lee

Ah, but he wants a toggle.

If you are going to get fussy about it, an Arduino is entirely superfluous.

Just one of these:

switched by one of these:

int pwmPin = 12;

Not on an Uno.
Leo..

See #1!

Paul__B:
See #1!

But also note that it really doesn't matter because in this case the pin name is misleading. The code isn't trying to use hardware PWM but hand-coded PWM with digitalWrites and delayMicroseconds. That will work on pin 12 just as well as on any other non-PWM digital pin.

Steve