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:
#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:
// 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);
}
}
}