Help with basic programming


So I'm an Arduino hobbyist - I've no experience in programming outside of this. I set up a circuit with a vibration motor, the idea is I want it perform a loop procedure once the push button is pressed and then stop once the button is pressed again.

Looking around the internet I've copied and altered some code out there but I can't seem to get it exactly right. The code below just gets the motor to do 3 loops before stopping and pressing the button again during the loop doesn't do anything.

Any pointers would be awesome as I've not managed to get any headway in a while.


const int buttonPin = 2;     
const int motorPin =  3;      

// Variables will change:
int motorState = LOW;         
int buttonState;             
int lastButtonState = LOW;   
long lastDebounceTime = 0;  
long debounceDelay = 1;    

void setup() {
  pinMode(buttonPin, INPUT);
  pinMode(motorPin, OUTPUT);

void loop() {
  int buttonState = digitalRead(buttonPin);

  if (buttonState != lastButtonState) {
    lastDebounceTime = millis();
      if (buttonState == HIGH) {
      motorState = !motorState;
      digitalWrite(motorPin, HIGH);
      digitalWrite(motorPin, LOW);
    while (motorState = !motorState);
 if ((millis() - lastDebounceTime) > debounceDelay) {
    buttonState = buttonState;
  digitalWrite(motorPin, motorState); 
  lastButtonState = buttonState;
while (motorState = !motorState);

Is a total nonsense.
First of all you must use a double = for a logic comparison. Then a thing can never be equal to the inverse of itself. Finally there is nothing that will make the states in the while conditional change so it is either ignored or hangs the program.

Try and read code by playing at being a processor and going through the statements one by one.

The code below just gets the motor to do 3 loops

What do you mean by “loops”?

buttonState = buttonState;

Yes, you need help.

One thing I dislike doing is mixing logic with inputs. I like to figure out all the inputs at the beginning of the loop, and then solve the logic afterwards. I find it difficult to follow code like yours to figure out what it's doing.

In addition to the previous replies, you need a variable to keep a record of the number of button pushes (first or second). Pushing the button whilst the delays in the motor routine are running won’t do you any good, as the Arduino ignores everything during delay() commands. Look up Blink without delay in the IDE. Go File > Examples > 02.Digital > ‘Blinkwithoutdelay’. Also look up ‘Finite State Machine’.


Start simple

Don't skip the examples.

I'll assume 01. Basics --> Blink is simple enough for you to get.

So first try; 02. Digital --> Button - looks simple & "mickey-mouse" - but DO IT.

When you've noticed the "inconsistency" - go to 02. Digital --> Debounce

Now you've learned to use buttons; next you want to learn about "non-blocking" code - which is what happens when you use delay() so;

Go to; 02. Digital --> BlinkWithoutDelay . This is the crucial first step for most beginners, the concept of State Machines, to some it's easy to get, but if you're like me and it took awhile; have a look at this for a very good explanation;

Thanks everyone, I think I just got a little ahead of myself, but thanks for your advice I obviously really needed it!

Grumpy_Mike - by 3 loops I meant 3 rounds of

digitalWrite(motorPin, HIGH); delay(1000); digitalWrite(motorPin, LOW); delay(1000);

by 3 loops I meant 3 rounds of …


for(int i = 0; i<3; i++){
      digitalWrite(motorPin, HIGH);
      digitalWrite(motorPin, LOW);