Arduino code for push button

Thread Starter

cmjb13

Joined Mar 18, 2015
35
I purchased a button seen here to connect to the arduino uno

http://www.miniinthebox.com/octopus...rks-with-official-arduino-boards_p824623.html



Once the button is installed, I want to press the button once and have the program (stepper motor spin) cycle through. I don't want to have to hold the button in order for the program cycle to complete.

I have the basic sample of the code, but not sure what to enter

C:
void loop() {
  if(doing_stuff) {
  do my loop thing;
  if(my loop thing is finished) {
  doing_stuff = false;
  }
  }
  else {
  if(button is down) {
  doing_stuff = true;
  }
  }
}
Thanks in advance for any help.

Moderators note : Used code tags for C
 
Last edited by a moderator:

mcgyvr

Joined Oct 15, 2009
5,394
From the button tutorial on the arduino website (and modified to make it tailored to you)
https://www.arduino.cc/en/Tutorial/Button
C:
const int buttonPin = 2;     // the number of the pushbutton pin

// variables will change:
int buttonState = 0;         // variable for reading the pushbutton status

void setup() {
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT);
}

void loop() {
  // read the state of the pushbutton value:
  buttonState = digitalRead(buttonPin);

  // check if the pushbutton is pressed.
  // if it is, the buttonState is HIGH:
  if (buttonState == HIGH) {
    // run the stepper motor function:
    cmjb13steppermotorfunction();
  } else {
     delay(50);//do nothing and give some delay just because
  }
}

void cmjbsteppermotorfunction() {
//write code for stepper motor cycle here
}
Moderators note : Changed code tags for C
 
Last edited by a moderator:

Thread Starter

cmjb13

Joined Mar 18, 2015
35
I added the stepper motor code. Does this look good to you?

C:
const int buttonPin = 2;  // the number of the pushbutton pin

// variables will change:
int buttonState = 0;  // variable for reading the pushbutton status

void setup() {
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT);
}

void loop() {
  // read the state of the pushbutton value:
  buttonState = digitalRead(buttonPin);

  // check if the pushbutton is pressed.
  // if it is, the buttonState is HIGH:
  if (buttonState == HIGH) {
  // run the stepper motor function:
  steppermotorfunction();
  } else {
  delay(50);//do nothing and give some delay just because
  }
}

void steppermotorfunction() {
//write code for stepper motor cycle here
}

#include <Stepper.h>
int x = 0;
const int stepsPerRevolution = 200;  // change this to fit the number of steps per revolution
// for your motor
// initialize the stepper library on pins 8 through 11:
Stepper myStepper(stepsPerRevolution, 8, 9, 10, 11);
  // set the speed at 60 rpm:
  myStepper.setSpeed(63);
  // initialize the serial port:
  Serial.begin(4800);
}
if (x <= 10)
{ // step one revolution in one direction:
  Serial.println("clockwise");
  myStepper.step(stepsPerRevolution);
  // delay(500);
x++;
}
  // step one revolution in the other direction:
  // Serial.println("counterclockwise");
// myStepper.step(-stepsPerRevolution);
// delay(500);
  Serial.write(0x01);
}
Moderators note : changed code tags for C
 
Last edited by a moderator:

mcgyvr

Joined Oct 15, 2009
5,394
I added the stepper motor code. Does this look good to you?
No
it looks like you just thought you could skip the actual "learning" part and jump right into copying and pasting others code without even understanding the most basic principles. ;)
Are you even using the Arduino IDE and attempting to compile the program?
Surely it didn't compile without errors? (I know it didn't)

Please.. No seriously.. Put down whatever project you think you are going to do and actually attempt to learn the basics first and understand a bit about what you are actually doing..
https://www.arduino.cc/en/Tutorial/BuiltInExamples
Those tutorials are excellent and teaching the basics (start with Blink and keep going)..
And you MUST know the basics.. The internet won't hand you everything..

Come back and try again.. (Strike 1 :p )
 

Thread Starter

cmjb13

Joined Mar 18, 2015
35
No
it looks like you just thought you could skip the actual "learning" part and jump right into copying and pasting others code without even understanding the most basic principles. ;)
Are you even using the Arduino IDE and attempting to compile the program?
Surely it didn't compile without errors? (I know it didn't)

Please.. No seriously.. Put down whatever project you think you are going to do and actually attempt to learn the basics first and understand a bit about what you are actually doing..
https://www.arduino.cc/en/Tutorial/BuiltInExamples
Those tutorials are excellent and teaching the basics (start with Blink and keep going)..
And you MUST know the basics.. The internet won't hand you everything..

Come back and try again.. (Strike 1 :p )
I have the arduino software on my laptop and with the uno connected, it does turn the stepper motor. I'm still waiting for the push button and 3.5mm female/male audio cable. My intention was to get an idea if the changes above appear to be valid, so once the parts arrive, I can dive right in. The code I posted was of the working stepper motor + yet to be tested audio + push button.
 

mcgyvr

Joined Oct 15, 2009
5,394
Point is.. you haven't learned the basics..
So your code doesn't work properly and won't work with the button at all..
And as pasted it won't compile properly
 

mcgyvr

Joined Oct 15, 2009
5,394
C:
#include <Stepper.h>

//define our variables
const int buttonPin = 2;  // the number of the pushbutton pin
const int stepsPerRevolution = 200;  // change this to fit the number of steps per revolution
int buttonState = 0;  // variable for reading the pushbutton status
Stepper myStepper(stepsPerRevolution, 8, 9, 10, 11); //set stepper pins

void setup() { //this code will only run once
  pinMode(buttonPin, INPUT); // initialize the pushbutton pin as an input:
  myStepper.setSpeed(63); //set stepper speed
  Serial.begin(4800);  // initialize the SLOW serial port
} //end of setup function

void loop() {
  buttonState = digitalRead(buttonPin); // check if the pushbutton is pressed.
  if (buttonState == HIGH) { //if button is pressed
      steppermotorfunction();// run the stepper motor function:
  }
  else
  {
  delay(10);//do nothing waiting for button press
  }
} //end of loop function

void steppermotorfunction() {
  //This code runs as soon as the button is pressed

  for (int i=0; i < 10; i++) { //turn the stepper 10 times
    Serial.println("clockwise"); //print that we are running clockwise
    Serial.print("Turn #");
    Serial.println(i);
    myStepper.step(stepsPerRevolution);
  }

} //end of steppermotorfunction
Moderators note : changed code tags for C again
 
Last edited by a moderator:
Top