Hi all
Could you please help me with the issue I am having with my code.
I am having an issue with sprintf and sscanf on the transmit and/or receive side of my link.
Without these two my kepresses transmitted work fine
Once I enable the use of sprintf and sscanf the keypress number becomes 609 and none of the
variables fill up correctly on the remote side.
I have been sitting up until the early hours of the morning for weeks and have learned so much
with this hobby project now and I am finally at wits end and think I need a little nudge so I can
continue on.
I would really appreciate and help guidance solutions and comments.
Den
Please find the pseudo code here.
Psuedo code
At the transmitter :
Enter code or codes via keypad which accepts multiple keypresses (some are pre-populated currently
untill the keypad menu is complete) these codes are visible and should be visible on LCD and
serial monitor.
Codes variables are:
hcode (Should hold 4 or less characters - pre-populated for now)
devcode (fetched from keypad - (type in 123#) currently set to 4 characters but could/would like
to be changed in future.)
pcode (2 characters alphanumeric W7 Pre-populated for now)
oocode (3 or less digits)
allcode (should holds all codes for sending and either wirelessly or via serial)
Currently using :
4x4 matrix keypad (working)
virtual wire (working)
Passing the variables (not working)
At the receiver:
Receive code (allcode)
Parse allcode and split individual codes namely:
hcode
devcode
pcode
oocode
Codes would be displaed on serial monitor and/or LCD
Use If or Switch case statement to perform action based on codes
(was working ..currently commented out/disabled)
Here's the code I have so far.
I have entered a lot of serial.print for debugging
The transmitter
// include the library code:
//#include <LiquidCrystal.h>
// initialize the library with the numbers of the interface pins
//LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
//include virtual wire library
#include <VirtualWire.h>
//include keypad library
#include <Keypad.h>
//declare the format of the VARIABLES and the VARIABLES
char key; //holds single key
char buffer[4]; //holds keys pressed
int i; //counter
//declare "code-holding" vars
char hcode;
char devcode [4];
char pcode;
char oocode;
char allcode[13];
/*
keypad map
*/
const byte ROWS = 4; //four rows
const byte COLS = 4; //three columns
char keys[ROWS][COLS] = {
{'1','2','3','A'},
{'4','5','6','B'},
{'7','8','9','C'},
{'*','0','#','D'}
};
byte rowPins[ROWS] = {8, 9, 10, 13}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {A0, A2, A1, A3}; //connect to the column pinouts of the keypad
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
void setup(){
// Initialise the IO and ISR --radio
vw_setup(2000); // Bits per sec
vw_set_tx_pin(7);
//Serial Section
Serial.begin(9600);
/*
//LCD Section
// set up the LCD's number of columns and rows:
lcd.begin(16, 4);
//Print a message to the LCD.
lcd.print("hello, world!");
lcd.clear();
delay(1000);
*/
}
void loop(){
i = 0;
buffer[0] = '\0'; //NULL the array
while( i < sizeof(buffer)) // removed this last one - 1)
{
key = keypad.getKey();
if (key == '#')
{
Serial.print("Sent");
Serial.write(buffer[i]);
lcd.print("Sent");
delay(1000);
lcd.clear();
devcode[i] = atoi((char*) buffer);
devcode[i]= buffer[i];
hcode=45;
pcode=1;
oocode=1;
//for debugging
Serial.println();
Serial.print("this is buffer[i]");
Serial.write(buffer[i]);
Serial.println();
Serial.print("This is devcode[i]");
Serial.write(devcode);
Serial.println();
Serial.print("This is hcode ");
Serial.write(hcode);
Serial.println();
Serial.print("This is pcode ");
Serial.write(pcode);
Serial.println();
Serial.print("This is oocode ");
Serial.write(oocode);
Serial.println();
Serial.print("This is buffer ");
Serial.write(buffer);
Serial.println();
Serial.print("This is devcode ");
Serial.write(devcode);
Serial.println();
//prepare the message and format the message as allcode
sprintf(allcode, "%d,%d,%d,%d", hcode, oocode, pcode, devcode);
//send it
vw_send((uint8_t *)allcode, strlen(allcode));
//for debugging
Serial.print("This is allcode ");
Serial.print(allcode);
Serial.println();
//vw_send((uint8_t *)buffer,8);
vw_wait_tx(); // Wait until the whole message is gone
break;
}
else if ( key != NO_KEY) //if(key) is (or is it the same as) if(key != NO_KEY)
{
//Serial.print("This is key ");
//Serial.print(key);
lcd.print(key);
buffer[i] = key;
i++;
buffer[i]= '\0';
}
}
//Serial.print("Sending ");
Serial.print(buffer);
}
/* left around for testing
char getChar()
{
char key = keypad.getKey();
if (key != NO_KEY)
{
return key;
}
else {
return NO_KEY;
}
}
*/
The receiver
/*Using
SimpleReceive
This sketch displays text strings received using VirtualWire
Connect the Receiver data pin to Arduino pin 11
*/
#include <VirtualWire.h>
byte message[VW_MAX_MESSAGE_LEN]; // a buffer to hold the incoming messages
byte msgLength = VW_MAX_MESSAGE_LEN; // the size of the message
int code;
byte allcode; //should hold all codes
int hcode; // 4 digit code (can it containd alpha numerics ABCD?)
int devcode; //4 digits or lesss - exactly what you enter on keypad
int pcode; //a two digit code - could contain aplpha numerics eg A1
int oocode; // up to 3 digits - numbers only 0 to 255
// Pin 13 has an LED connected on most Arduino boards.
// give it a name:
int led = 13;
void setup()
{
Serial.begin(9600);
Serial.println("Ready"); //for debugging
// Initialize the IO and ISR
vw_setup(2000); // Bits per sec
vw_rx_start(); // Start the receiver
vw_set_rx_pin(3); //receive pin set to 3
// initialize the digital pin as an output.
pinMode(led, OUTPUT); // set led pin 13 as output
}
void loop()
{
if (vw_get_message(message, &msgLength)) // Non-blocking
{
Serial.print("Got: "); //for debugging
for (int i = 0; i < msgLength; i++) //counter loop for received message length
{
allcode = atoi((char*) message);
//allcode=message;
//sscanf((char*)allcode, "%s,%s,%s,%s",&hcode,&oocode,&pcode,&devcode); // Converts a string to an array
//wasnt working with allcode variable so pointed sscanf to message to break it down
sscanf((char*)message, "%s,%s,%s,%s",&hcode,&oocode,&pcode,&devcode); // Converts a string to an array
// added this to convert message charcters to an int
code = atoi((char*) message);
//for debugging
//Serial.print("this is message inside loop");
Serial.print("this is message[i] in loop ");
Serial.write(message[i]);
Serial.println();
Serial.print("this is inside loop code value ");
Serial.write(code);
Serial.println();
Serial.print("this is allcode ");
Serial.write(allcode);
Serial.println();
//Serial.print("this is allcode[i] ");
//Serial.write(allcode[i]);
}
Serial.println();
Serial.print("this is outside of loop ");
Serial.println();
Serial.print("this is code ");
Serial.write(code);
Serial.println();
// for debugging
Serial.print("this is hcode");
Serial.write(hcode);
Serial.println();
Serial.print("this is devcode");
Serial.write(devcode);
Serial.println();
Serial.print("this is pcode");
Serial.write(pcode);
Serial.println();
Serial.print("this is oocode");
Serial.write(oocode);
//for eventual code matching
//code matching
//if (code == 145)
//{
// Serial.print("code MATCH!!");
// digitalWrite(13, !digitalRead(13)); // toggle pin state
//digitalWrite(led, HIGH); // turn the LED on (HIGH is the voltage level)
//}
//null the arrays
hcode= '\0';
devcode= '\0';
pcode= '\0';
oocode= '\0';
allcode='\0';
code= '\0';
//possible code below to clear array - had issues
//memset( allcode, 0, sizeof( allcode));// This line is for reset the allcode
}
}