Go Down

Topic: Wireless Communication between Button and Stepper Motor  (Read 127 times) previous topic - next topic

klk0146

Hello--
    I think I am so close, but there must be an error in my code. I have two Arduino Uno's, two xbee's and two xbee shields.

The goal is to get one full revolution of the motor (hooked up to one arduino) when the button (hooked up to the other arduino) is pushed.

I have set up the xbees to talk to each other. In fact if I connect one xbee to my computer via a dongle explorer, I can see the output from my other xbee (since it is connected to a button, it returns values of 0 when the button is NOT pressed, and 1's when the button is pressed). So communication is not the issue.

Once I take the Xbee off the dongleExplorer and put it on the other xbee shield (connected to the Arduino) is when I get trouble. Here is the code I have loaded on the arduino controlling my stepper motor:

Code: [Select]
#include <Stepper.h>

#define IN1  8
#define IN2  9
#define IN3  10
#define IN4  11

#define motorSteps 200 

Stepper myStepper(motorSteps, IN1,IN2,IN3,IN4);


void setup()
{
  myStepper.setSpeed(100);
  Serial.begin(9600);
  Serial.println("got here 1!!");
}

  void loop()
  {
    while(Serial.available() > 0);
    int data = Serial.read() - '0';
    Serial.println("got here!!");
   
  if (data == HIGH)
  {
    // Step forward 100 steps:
    Serial.println("Forward");
    myStepper.step(100);
    delay(500);
  }
  else if (data == LOW)
  {
    Serial.println("Backward");
  //  myStepper.step(-100);
  //  delay(500);
  }
  }


I realize that the button arduino is constantly sending data (either 0's or 1's). I would have expected that this would mean that Serial.available() would always be > 0, but this is not the case. This code sends the printed line "I got here 1", and then "Backward" over and over. What I would LIKE for it to do is to turn on the motor one full revolution when the button is pushed.
Any help would be so great! Thank you!

edgemoron

Is your "button Arduino" sending the value 1 or 0, or the ASCII characters '1' or '0', (49, 48)?
Post code.

PaulS

Quote
I realize that the button arduino is constantly sending data (either 0's or 1's).
Why? It should send a value ONCE when the switch becomes pressed and ONCE when the switch becomes released.

Make that happen, and I'll bet a box of donuts that the receiver starts working properly.
The art of getting good answers lies in asking good questions.

Robin2

Have a look at the examples in Serial Input Basics - simple reliable ways to receive data.

It can be useful to send the data regularly - say 5 times per second - so that the receiver can tell if the flow of data is interrupted. However sending the data more frequently will probably be counter productive.

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

klk0146

Is your "button Arduino" sending the value 1 or 0, or the ASCII characters '1' or '0', (49, 48)?
Post code.
Yes, thank you. The code for the button is here....

Code: [Select]

int switchPin = 8;

void setup()
{
 pinMode(switchPin, INPUT);
 Serial.begin(9600);
}

void loop()
{
 if (digitalRead(switchPin) == HIGH)
 {
   int val = HIGH;
   Serial.println(val);
 }
 else
 {
   int val = LOW;
   Serial.println(val);
 }
}

klk0146

Why? It should send a value ONCE when the switch becomes pressed and ONCE when the switch becomes released.

Make that happen, and I'll bet a box of donuts that the receiver starts working properly.
You make a great point, I said that it was continuously sending...I guess I am not sure if it is 'sending' it continuously, but I had lines in my code to print out a line saying send the value, and THAT was continuous.

The code is here:

Code: [Select]

int switchPin = 8;

void setup()
{
 pinMode(switchPin, INPUT);
 Serial.begin(9600);
}

void loop()
{
 if (digitalRead(switchPin) == HIGH)
 {
   int val = HIGH;
   Serial.println(val);
 }
 else
 {
   int val = LOW;
   Serial.println(val);
 }
}

PaulS

You need to look at the state change detection example. Send a value when the switch BECOMES pressed. Send a value when the switch BECOMES released. Send nothing when no change takes place.
The art of getting good answers lies in asking good questions.

gfvalvo

The code for the button is here....
As the code is written, your variable 'val' serves no useful purpose.

Go Up