Pages: [1]   Go Down
Author Topic: Serial and button actuated stepper  (Read 584 times)
0 Members and 1 Guest are viewing this topic.
Arizona
Offline Offline
Jr. Member
**
Karma: 0
Posts: 63
Just another duck on the pond.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi,

I have my stepper motor working via serial commands, but, not with pushbuttons as well. Any ideas where I am wrong? Thanks!

Code:
#include <Stepper.h>

#define motorSteps 200     // change this depending on the number of
                           // steps per revolution of your motor
#define motorPin1 9
#define motorPin2 10
#define motorPin3 11
#define motorPin4 12
#define ledPin 13
const int buttonPin1 = 3;   // Switch connected to digital pin 3
const int buttonPin2 = 4;   // Switch connected to digital pin 4


char up = 'u';  // ascii motor up
char down = 'd';  // ascii motor down

int myStepperState;
// variables will change:
int buttonState1 = 0;         // variable for reading the pushbutton status
int buttonState2 = 0;


char nextChar;

// initialize of the Stepper library:
Stepper myStepper(motorSteps, motorPin1,motorPin2,motorPin3,motorPin4);

void setup()

{
   myStepper.setSpeed(80);   // set the motor speed at 60 RPMS:
 

  Serial.begin(9600);   // Initialize the Serial port:
   
  pinMode(buttonPin1, INPUT);    // sets the digital pin as input to read switch
  pinMode(buttonPin2, INPUT);    // sets the digital pin as input to read switch
 
 
}

void loop()

{
  if (Serial.available()) 
  {
       nextChar = Serial.read();  // read just one character
       buttonState1 = digitalRead(buttonPin1);   // read the state of the pushbutton value:
       buttonState2 = digitalRead(buttonPin2);   // read the state of the pushbutton value:

       if (nextChar == up)  // check for 'u'
       {
         myStepper.step(300);  // Step forward 300 steps:
         Serial.println("Serial Up");
       }
       else if (nextChar == down)  // check for 'd'
       { 
         myStepper.step(-300);  // Step backward 100 steps:
         Serial.println("Serial Down");
       }
       else if (buttonState1 == HIGH)   // check if the button is pressed
       {
         myStepper.step(300);  // Step forward 300 steps:
         Serial.println("Switch Up");
       }
       else if (buttonState2 == HIGH)   // check if the button is pressed
       { 
         myStepper.step(-300);  // Step backward 100 steps:
         Serial.println("Switch Down");
       }
    }
}
Logged

0
Offline Offline
Faraday Member
**
Karma: 16
Posts: 2857
ruggedcircuits.com
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

A couple of things. First, your buttons will do nothing unless someone presses a character because you've got the whole loop surrounded by a "if (Serial.available())" test.

Second, how do you have your buttons wired up? Are they connected to 0V? 5V? It's common to connect pushbuttons from the digital input to 0V, then enable the pullup resistor on the digital input. Then, the digital input reads high when NOT pressed, and low when pressed.

--
The Arduino Drum Machine: 14-track MIDI drum machine sequencer / groove-box
Logged

Arizona
Offline Offline
Jr. Member
**
Karma: 0
Posts: 63
Just another duck on the pond.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

OK.

1- I see where the if serial , but, how to modify to also read buttonstates?

2- Switches are hooked up to 5v
Logged

0
Offline Offline
Faraday Member
**
Karma: 16
Posts: 2857
ruggedcircuits.com
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Try something like this:
Code:
void loop() {
  buttonState1 = digitalRead(buttonPin1);   // read the state of the pushbutton value:
  buttonState2 = digitalRead(buttonPin2);   // read the state of the pushbutton value:

  if (buttonState1 == HIGH) {  // check if the button is pressed
         myStepper.step(300);  // Step forward 300 steps:
         Serial.println("Switch Up");
  } else if (buttonState2 == HIGH) {  // check if the button is pressed
         myStepper.step(-300);  // Step backward 100 steps:
         Serial.println("Switch Down");
  }

  if (Serial.available())  {
       nextChar = Serial.read();  // read just one character

       if (nextChar == up)  {// check for 'u'
         myStepper.step(300);  // Step forward 300 steps:
         Serial.println("Serial Up");
       } else if (nextChar == down) { // check for 'd'
         myStepper.step(-300);  // Step backward 100 steps:
         Serial.println("Serial Down");
       }
    }
}

But again, I think you're going to have trouble with your switches. If you push it, it will be connected to 5V and read HIGH. If you don't push it....what is it connected to? Nothing? Then it will be floating and may continue to read HIGH all the time. If this is your scheme then at least connect a 10k resistor from the digital input to ground to keep it from floating when the switch is not pushed.

--
The Gadget Shield: accelerometer, RGB LED, IR transmit/receive, speaker, microphone, light sensor, potentiometer, pushbuttons
Logged

Arizona
Offline Offline
Jr. Member
**
Karma: 0
Posts: 63
Just another duck on the pond.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Well done!

Both changes were required.
1- sent buttonStates above Serial.
2- put pulldown res on switches.

Thank you very much.

Have you worked with XBees at all?

I am next going to hook up an XBee to the Arduino and then have another XBee with 2 switches on it. I have v1 XBees currently using DigiMesh firmware.

Logged

Pages: [1]   Go Up
Jump to: