I am trying to modify a sketch I found online. (to send a servo command via 433Mhz)
My question only pertains to the Receive sketch. The program is working as it was originally intended to by the person who posted it. It does an analog read at the transmitter side and converts the 3 or 4 digit integer value to characters and sends those one at a time . They are received and printed to the serial
monitor as ASCII values (ie:31 30 30 35 = 1005, 39 39 39 = 999 , 39 37 35 = 975.
What I am trying to do is reconstruct the integer to integer type at the receive end and use it as an input
for a servo routine that moves an RC servo to track the position of a poteniometer knob. (Called 'Knob" in the Servo Library Examples folder)
I have tried CAST and that doesn't seem to work.
Attached is the serial monitor terminal capture file which shows the original code successfully
printing "Got: 31 30 30 35" (ASCII characters that represent analog Read value 1005)
This part:
480
4800
-8536
n = -3256
is the output of my added code in an unsuccessful attempt to obtain the integer value. I know my approach is probable not even in the ballpark of what it should be which is why I am posting.
Here is the receive sketch:
#include <VirtualWire.h>
#include <stdio.h>
#include <stdlib.h>
#undef int
#undef abs
#undef double
#undef float
#undef round
void setup()
{
Serial.begin(9600); // Debugging only
Serial.println("setup");
// Initialise the IO and ISR
vw_set_ptt_inverted(true); // Required for DR3100
vw_setup(2000); // Bits per sec
vw_rx_start(); // Start the receiver PLL running
}
void loop()
{
uint8_t buf[VW_MAX_MESSAGE_LEN];
uint8_t buflen = VW_MAX_MESSAGE_LEN;
if (vw_get_message(buf, &buflen)) // Non-blocking
{
int i;
digitalWrite(13, HIGH); // Flash a light to show received good message
delay(500);
// Message with a good checksum received, dump it.
Serial.print("Got: ");
for (i = 0; i < buflen; i++)
{
Serial.print(buf[i], HEX);
Serial.print(" " );
// sendstatus;
}
Serial.println();
for (i = 0; i < buflen; i++)
{
byte a;
byte b;
byte c;
byte d;
int w;
int x;
int y;
int z;
int n;
if (i=0)
{
a = (byte) buf[i],HEX;;
w = (int) a ;
Serial.print(w);
delay(500);
Serial.println();
}
if (i=1)
{
b = (byte) buf[i],HEX;
x = (int) b;
x = x * 10;
Serial.print(x);
delay(500);
Serial.println();
}
if (i=2)
{
c = (byte) buf[i],HEX;
y = (int) c;
y = y * 100;
Serial.print(y);
delay(500);
Serial.println();
}
if (i=3)
{
d = (byte) buf[i],HEX;
z = (int) d;
z = z * 1000;
Serial.println(z);
delay(500);
Serial.println();
}
n = w + x + y + z;
Serial.print("n = " );
Serial.println(n );
delay(500);
}
Serial.println();
Serial.println("");
digitalWrite(13, LOW);
delay(500);
}
}
void sendstatus()
{
digitalWrite(13, HIGH); // Flash a light to show received good message
delay(500);
digitalWrite(13, LOW);
delay(500);
}
This is the part I added (which doesn't work)
The output result is the same if I use "char" instead of "byte"
for (i = 0; i < buflen; i++)
{
byte a;
byte b;
byte c;
byte d;
int w;
int x;
int y;
int z;
int n;
if (i=0)
{
a = (byte) buf[i],HEX;;
w = (int) a ;
Serial.print(w);
delay(500);
Serial.println();
}
if (i=1)
{
b = (byte) buf[i],HEX;
x = (int) b;
x = x * 10;
Serial.print(x);
delay(500);
Serial.println();
}
if (i=2)
{
c = (byte) buf[i],HEX;
y = (int) c;
y = y * 100;
Serial.print(y);
delay(500);
Serial.println();
}
if (i=3)
{
d = (byte) buf[i],HEX;
z = (int) d;
z = z * 1000;
Serial.println(z);
delay(500);
Serial.println();
}
n = w + x + y + z;
Serial.print("n = " );
Serial.println(n );
delay(500);
}
What I need to do is take the two to four characters (it varies with the analog read integer value) and
reconstruct the integer to a usable integer that can be used for decision making at the receive end.
Just FYI, (in case anyone wants to know),
here is the transmitter sketch:
/*
Sensor Transmitter
By Markus Ulfberg 2012-07-06
Takes a sensor reading 0-1023
converts it to a char array and sends
to RF receiver unit via VirtualWire
*/
#include <VirtualWire.h>
// LED's
const int ledPin = 13;
// Sensors
const int Sensor1Pin = A2;
// const int Sensor2Pin = 3;
int Sensor1Data;
//int Sensor2Data;
char Sensor1CharMsg[4];
void setup() {
// PinModes
// LED
pinMode(ledPin,OUTPUT);
// Sensor(s)
pinMode(Sensor1Pin,INPUT);
// for debugging
Serial.begin(9600);
// VirtualWire setup
vw_setup(2000); // Bits per sec
}
void loop() {
// Read and store Sensor 1 data
Sensor1Data = analogRead(Sensor1Pin);
// Convert integer data to Char array directly
itoa(Sensor1Data,Sensor1CharMsg,10);
// DEBUG
Serial.print("Sensor1 Integer: ");
Serial.print(Sensor1Data);
Serial.print(" Sensor1 CharMsg: ");
Serial.print(Sensor1CharMsg);
Serial.println(" ");
delay(1000);
// END DEBUG
digitalWrite(13, true); // Turn on a light to show transmitting
vw_send((uint8_t *)Sensor1CharMsg, strlen(Sensor1CharMsg));
vw_wait_tx(); // Wait until the whole message is gone
digitalWrite(13, false); // Turn off a light after transmission
delay(200);
} // END void loop..
I only have one FTDI module and I'm using standalone ATmega328s. If I move the cable to the Tx, I can
see the integer value (see attached Tx capture file) This is how I compare the result at the receiver end
with the original transmitted value.
If anyone can tell me how to do this (convert the received characters to a single data type integer) OR
modify the TX sketch to send something different that is easier to convert to an integer at the receive end. The end result is I turn the knob at the transmitter end and the servo tracks the position at the receiver end.
Send_servo_command_ClearTerminal_Capture__02.15.2015_16.32.14.txt (950 Bytes)
Send_servo_command_Tx_ClearTerminal_Capture__02.15.2015_17.05.47.txt (2.47 KB)

