Hello, i am trying to feed a servo using 433Mhz Modules and two arduinos, for that a potentiometer is being used on one and then the servo is on the other. I choose VirtualWire to work from, the problem am i facing is that the message received on the receiver arduino is stored in a array called 'buf', my idea was to convert the array into a integer to use the value to feed the servo. But i don't know how to check if there are bytes in that array, then if there is, convert the bytes to another variable..
But i don't know how to check if there are bytes in that array, then if there is, convert the bytes to another variable..
Virtual will let you know if there is data available if (vw_get_message(buf, &buflen))
What you do on the receiving end the the received buffer depends upon how you sent the data. If you sent a number represented by ascii characters then you will use the atoi() function at the receiving end to create an integer from the ascii characters.
If you broke the integer into bytes for transmission and sent a high and low byte, then you can put them back together with the word() function or bit shifting.
Post your code if you want more assistance.
I am really new to all this, so i'll upload the code i'm using, that is not finished ofc. The idea was to create a string with the value from the pot and then send that to the receiver so i could transform back into a integer and then feed the servo. It's the first code i write, i just used ready-to-go codes until now.
receiver_code_test.ino (608 Bytes)
transmitter.ino (678 Bytes)
Please read "How to use the Forum" and post your code, using code tags ("</>" button), so that it looks like this.
Transmitter
#include <VirtualWire.h>
int potanalogvalue;
String potstringvalue;
char potcharvalue[27];
int potpin = 0; // analog pin used to connect the potentiometer
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
vw_setup(2000);
vw_set_tx_pin(7);
}
void loop() {
// put your main code here, to run repeatedly:
potanalogvalue = analogRead(potpin);
potanalogvalue = map(potanalogvalue, 0, 1023, 0, 180);
potstringvalue = "";
potstringvalue += potanalogvalue;
potstringvalue.toCharArray(potcharvalue, 27);
potstringvalue = "";
Serial.print(potcharvalue);
vw_send((uint8_t *)potcharvalue, strlen(potcharvalue));
vw_wait_tx();
}
Receiver
#include <VirtualWire.h>
#include <ServoTimer2.h> // the servo library
#define servo1Pin 2 // the pin the servo is attached to
ServoTimer2 servo1; // declare variables for the servos
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
servo1.attach(servo1Pin);
vw_setup(2000);
vw_set_rx_pin(7);
vw_rx_start();
}
void loop() {
// put your main code here, to run repeatedly:
uint8_t buf[VW_MAX_MESSAGE_LEN];
uint8_t buflen = VW_MAX_MESSAGE_LEN;
if(vw_get_message(buf, &buflen))
{
//no idea about what to put here :)))))))
}
servo1.write(i);
}
Just send an integer, and receive an integer. You can replace all this (which has many errors and won't work anyway):
potanalogvalue = analogRead(potpin);
potanalogvalue = map(potanalogvalue, 0, 1023, 0, 180);
potstringvalue = "";
potstringvalue += potanalogvalue;
potstringvalue.toCharArray(potcharvalue, 27);
potstringvalue = "";
Serial.print(potcharvalue);
vw_send((uint8_t *)potcharvalue, strlen(potcharvalue));
with this:
potanalogvalue = analogRead(potpin);
Serial.print(potanalogvalue);
vw_send((uint8_t *) &potanalogvalue, 2);
Likewise at the receiving end, receive two bytes as an integer:
int potval = 0;
buflen=2; //max two bytes expected
if (vw_get_message((uint8_t *) &potval, &buflen) {
Serial.println(potval);
}
The "(uint8_t *) &" tells the compiler that you are sending a two byte array instead of an integer (which it already is).
Thanks, i'll insert and see how it goes, like i said that was the first thing i ever wrote, so i expected many mistakes, as i don't know the functions( don't know c or c++ arguments or anything).
It worked, i am receiving perfectly the pot value on the receiver, but when i try to map and then write the value to control the servo position, it just makes noise like it's trying to move ![]()
Okay, i think a new problem appeared, as i'm using servotimer2, the command is in microseconds not degrees, so i don't know how to write, does map cover this ?
does map cover this ?
I can. If you use the proper to range in the call.