Go Down

Topic: Keypad through xbee communication to dc motor question (Read 463 times) previous topic - next topic

tyler_james

I want to create code that will send characters from the keypad through the xbee to the other xbee.  Then the ardiuno will take in the signal and control a DC motor.  Now i am not sure if the way i programmed this will work.  I don't know if the character i send will be received as a character.  I have not tested the code yet because I am waiting for my USB explorer to give the xbees the ID's in order for them to communicate.

This is my sending code:

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

const byte ROWS = 4; //four rows
const byte COLS = 3; //three columns
char keys[ROWS][COLS] = {
  {'1','2','3'},
  {'4','5','6'},
  {'7','8','9'},
  {'#','0','*'}
};
byte rowPins[ROWS] = {5, 4, 3, 2}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {8, 7, 6}; //connect to the column pinouts of the keypad

Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );

void setup(){
  Serial.begin(9600);
}

void loop(){
  char key = keypad.getKey();

  if (key != NO_KEY){
    Serial.println(key);
  }
}



This is my receiver code:

Code: [Select]
// This program receives information from a MaxStream XBee radio.
// serial out is on port 1
// serial in is on port 0
#include <AFMotor.h>

AF_DCMotor motor(4); // define motor on channel 4 with 1KHz default PWM

char key = 0;

void setup () {
// start up the serial connection
Serial.begin(9600); 
motor.setSpeed(200);     // set the speed to 200/255
motor.run(RELEASE);
}

void loop () {
// get any incoming data:
if (Serial.available() > 1) {
// read a byte
key = Serial.read();

if(key = 1){
motor.run(FORWARD);      // turn it on going forward
delay(1000);
}
if(key = 4){
motor.run(BACKWARD);     // the other way
delay(1000);
}
if(key = 7){
motor.run(RELEASE);      // stopped
delay(1000);
}
}
}

PaulS

Quote
I don't know if the character i send will be received as a character.

Well, it won't be received as a float, a double, a banana, or a bazooka.

You are not sending A character, by the way, you are sending 3 characters - the one of interest, a carriage return, and a line feed (because of the println()).

Code: [Select]
if(key = 1){
Assigning a value to key seems pointless...

Nice
job
on
the
format
of
the
code.

tyler_james

I have the if(key = 1) part because I want the 1 on the keypad to have the DC motor go forward, the 4 to reverse, and the 7 to stop.  I'm assuming the way I have it now will not do that?

PaulS

Quote
I'm assuming the way I have it now will not do that?

A quick peak at any C book will reveal that = and == do very different things. One is an assignment operator. The other is an equality operator.

Go Up