Arduino code for push button

Discussion in 'Embedded Systems and Microcontrollers' started by cmjb13, Nov 30, 2015.

  1. cmjb13

    Thread Starter Member

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

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

    [​IMG]

    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

    Code (C):
    1.  
    2. void loop() {
    3.   if(doing_stuff) {
    4.   do my loop thing;
    5.   if(my loop thing is finished) {
    6.   doing_stuff = false;
    7.   }
    8.   }
    9.   else {
    10.   if(button is down) {
    11.   doing_stuff = true;
    12.   }
    13.   }
    14. }
    15.  
    Thanks in advance for any help.

    Moderators note : Used code tags for C
     
    Last edited by a moderator: Dec 1, 2015
  2. MikeML

    AAC Fanatic!

    Oct 2, 2009
    5,450
    1,066
  3. mcgyvr

    AAC Fanatic!

    Oct 15, 2009
    4,771
    971
    From the button tutorial on the arduino website (and modified to make it tailored to you)
    https://www.arduino.cc/en/Tutorial/Button
    Code (C):
    1.  
    2. const int buttonPin = 2;     // the number of the pushbutton pin
    3.  
    4. // variables will change:
    5. int buttonState = 0;         // variable for reading the pushbutton status
    6.  
    7. void setup() {
    8.   // initialize the pushbutton pin as an input:
    9.   pinMode(buttonPin, INPUT);
    10. }
    11.  
    12. void loop() {
    13.   // read the state of the pushbutton value:
    14.   buttonState = digitalRead(buttonPin);
    15.  
    16.   // check if the pushbutton is pressed.
    17.   // if it is, the buttonState is HIGH:
    18.   if (buttonState == HIGH) {
    19.     // run the stepper motor function:
    20.     cmjb13steppermotorfunction();
    21.   } else {
    22.      delay(50);//do nothing and give some delay just because
    23.   }
    24. }
    25.  
    26. void cmjbsteppermotorfunction() {
    27. //write code for stepper motor cycle here
    28. }
    29.  
    30.  
    Moderators note : Changed code tags for C
     
    Last edited by a moderator: Dec 1, 2015
  4. cmjb13

    Thread Starter Member

    Mar 18, 2015
    35
    0
    Thanks
     
  5. cmjb13

    Thread Starter Member

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

    Code (C):
    1.  
    2. const int buttonPin = 2;  // the number of the pushbutton pin
    3.  
    4. // variables will change:
    5. int buttonState = 0;  // variable for reading the pushbutton status
    6.  
    7. void setup() {
    8.   // initialize the pushbutton pin as an input:
    9.   pinMode(buttonPin, INPUT);
    10. }
    11.  
    12. void loop() {
    13.   // read the state of the pushbutton value:
    14.   buttonState = digitalRead(buttonPin);
    15.  
    16.   // check if the pushbutton is pressed.
    17.   // if it is, the buttonState is HIGH:
    18.   if (buttonState == HIGH) {
    19.   // run the stepper motor function:
    20.   steppermotorfunction();
    21.   } else {
    22.   delay(50);//do nothing and give some delay just because
    23.   }
    24. }
    25.  
    26. void steppermotorfunction() {
    27. //write code for stepper motor cycle here
    28. }
    29.  
    30. #include <Stepper.h>
    31. int x = 0;
    32. const int stepsPerRevolution = 200;  // change this to fit the number of steps per revolution
    33. // for your motor
    34. // initialize the stepper library on pins 8 through 11:
    35. Stepper myStepper(stepsPerRevolution, 8, 9, 10, 11);
    36.   // set the speed at 60 rpm:
    37.   myStepper.setSpeed(63);
    38.   // initialize the serial port:
    39.   Serial.begin(4800);
    40. }
    41. if (x <= 10)
    42. { // step one revolution in one direction:
    43.   Serial.println("clockwise");
    44.   myStepper.step(stepsPerRevolution);
    45.   // delay(500);
    46. x++;
    47. }
    48.   // step one revolution in the other direction:
    49.   // Serial.println("counterclockwise");
    50. // myStepper.step(-stepsPerRevolution);
    51. // delay(500);
    52.   Serial.write(0x01);
    53. }
    Moderators note : changed code tags for C
     
    Last edited by a moderator: Dec 1, 2015
  6. mcgyvr

    AAC Fanatic!

    Oct 15, 2009
    4,771
    971
    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 )
     
  7. cmjb13

    Thread Starter Member

    Mar 18, 2015
    35
    0
    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.
     
  8. mcgyvr

    AAC Fanatic!

    Oct 15, 2009
    4,771
    971
    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
     
  9. mcgyvr

    AAC Fanatic!

    Oct 15, 2009
    4,771
    971
    Code (C):
    1. #include <Stepper.h>
    2.  
    3. //define our variables
    4. const int buttonPin = 2;  // the number of the pushbutton pin
    5. const int stepsPerRevolution = 200;  // change this to fit the number of steps per revolution
    6. int buttonState = 0;  // variable for reading the pushbutton status
    7. Stepper myStepper(stepsPerRevolution, 8, 9, 10, 11); //set stepper pins
    8.  
    9. void setup() { //this code will only run once
    10.   pinMode(buttonPin, INPUT); // initialize the pushbutton pin as an input:
    11.   myStepper.setSpeed(63); //set stepper speed
    12.   Serial.begin(4800);  // initialize the SLOW serial port
    13. } //end of setup function
    14.  
    15. void loop() {
    16.   buttonState = digitalRead(buttonPin); // check if the pushbutton is pressed.
    17.   if (buttonState == HIGH) { //if button is pressed
    18.       steppermotorfunction();// run the stepper motor function:
    19.   }
    20.   else
    21.   {
    22.   delay(10);//do nothing waiting for button press
    23.   }
    24. } //end of loop function
    25.  
    26. void steppermotorfunction() {
    27.   //This code runs as soon as the button is pressed
    28.  
    29.   for (int i=0; i < 10; i++) { //turn the stepper 10 times
    30.     Serial.println("clockwise"); //print that we are running clockwise
    31.     Serial.print("Turn #");
    32.     Serial.println(i);
    33.     myStepper.step(stepsPerRevolution);
    34.   }
    35.  
    36. } //end of steppermotorfunction
    37.  
    Moderators note : changed code tags for C again
     
    Last edited by a moderator: Dec 1, 2015
Loading...