Skip to content

Instantly share code, notes, and snippets.

@itarozzi
Last active October 19, 2017 22:08
Show Gist options
  • Save itarozzi/ba33372650ddc809965203f7ce4ebc26 to your computer and use it in GitHub Desktop.
Save itarozzi/ba33372650ddc809965203f7ce4ebc26 to your computer and use it in GitHub Desktop.
/*
Adattamento dello sketch tratto dal seguente link, per la trasmissione di valori multipli
http://www.ardumotive.com/how-to-use-xbee-modules-as-transmitter--receiver-en.html
~ Simple Arduino - xBee Transmitter sketch ~
Read an analog value from potentiometer, then convert it to PWM and finally send it through serial port to xBee.
The xBee serial module will send it to another xBee (resiver) and an Arduino will turn on (fade) an LED.
The sending message starts with '<' and closes with '>' symbol.
Dev: Michalis Vasilakis // Date:2/3/2016 // Info: www.ardumotive.com // Licence: CC BY-NC-SA */
#define BUFFER_RX_SIZE 6
//Variables:
int value1 ;
int value2 ;
bool started= false; //True: Message is strated
bool ended = false; //True: Message is finished
int index = 0;
/*
* Dimensionare il buffer_tx sulla base dei valori da trasmettere:
*
* '<' 1 byte
* val1 2 bytes
* val2 2 bytes
* '>' 1 byte
*
*/
char buffer_rx[BUFFER_RX_SIZE];
void setup() {
//Start the serial communication
Serial.begin(9600); //Baud rate must be the same as is on xBee module
}
void loop() {
char incomingByte ;
while (Serial.available()>0) {
//Read the incoming byte
incomingByte = Serial.read();
//Start the message when the '<' symbol is received
if(incomingByte == '<')
{
started = true;
index = 0;
memset(buffer_rx, 0x00, BUFFER_RX_SIZE);
}
//End the message when the '>' symbol is received
else if(incomingByte == '>')
{
ended = true;
break; // Done reading - exit from while loop!
}
// Read the buffer_rx
if(index < BUFFER_RX_SIZE) // Make sure there is room
{
buffer_rx[index] = incomingByte;
index++;
}
}
if(started && ended)
{
// Ho ricevuto packet start e packet end, allora posso processare i dati
memcpy(&value1, &buffer_rx[1], 2);
memcpy(&value2, &buffer_rx[3], 2);
Serial.println(value1); //Only for debugging
Serial.println(value2); //Only for debugging
index = 0;
started = false;
ended = false;
}
delay(200);
}
/*
Adattamento dello sketch tratto dal seguente link, per la trasmissione di valori multipli
http://www.ardumotive.com/how-to-use-xbee-modules-as-transmitter--receiver-en.html
~ Simple Arduino - xBee Transmitter sketch ~
Read an analog value from potentiometer, then convert it to PWM and finally send it through serial port to xBee.
The xBee serial module will send it to another xBee (resiver) and an Arduino will turn on (fade) an LED.
The sending message starts with '<' and closes with '>' symbol.
Dev: Michalis Vasilakis // Date:2/3/2016 // Info: www.ardumotive.com // Licence: CC BY-NC-SA */
//Constants:
const int potPin = A0; //Pot at Arduino A0 pin
//Variables:
int value1 ;
int value2 ;
int test_val=1;
void setup() {
//Start the serial communication
Serial.begin(9600); //Baud rate must be the same as is on xBee module
}
void loop() {
//Read the analog value from pot and store it to "value" variable
//value1 = analogRead(A0);
//value2 = analogRead(A1);
// Io non avendo nulla attaccato simulo dei valori
// ************************************
value1 = test_val*10;
value2 = test_val*100;
test_val++;
if (test_val > 10) test_val = 1;
// ************************************
/*
* Dimensionare il buffer_tx sulla base dei valori da trasmettere:
*
* '<' 1 byte
* val1 2 bytes
* val2 2 bytes
* '>' 1 byte
*
* Nota: per ottimizzare il byte 0 e 5 potrebbero essere assegnati una sola volta nel setup !!
*/
char buffer_tx[6];
buffer_tx[0] = '<';
memcpy(&buffer_tx[1], &value1 , 2);
memcpy(&buffer_tx[3], &value2 , 2);
buffer_tx[5] = '>';
//Send the message:
Serial.write(buffer_tx, 6);
delay(500);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment