Go Down

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


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] = {
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(){

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

  if (key != NO_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
motor.setSpeed(200);     // set the speed to 200/255

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
if(key = 4){
motor.run(BACKWARD);     // the other way
if(key = 7){
motor.run(RELEASE);      // stopped


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...



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?


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

Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

via Egeo 16
Torino, 10131