UART Receiving keyboard input doesn't work - arduino

I am having trouble with reading data sent from my laptop to the microcontroller via UART. The transmitting part works fine - I can send data to my laptop without problems, but echoing data, or reading keyboard input data from the laptop doesn't work. I have tried to implement reading both with polling and with Rx interrupt routines, but without success. The microcontroller I am using is ATMEGA2560, and I am conntected to my laptop via FTD1232 USB to TTL serial converter.
I am using Putty as a serial comm terminal, and the Arduino IDE for programming.
I am assuming that the problem is not in the hardware based on the following - I have tried initializing different USARTs and tried to use different COM ports, as well as another USB to TTL converter, various wires, but the result is the same. I have also connected another power source to supply enough voltage to all devices.
I am trying to get at least a response by turning a LED on when Rx interrupt occurs (when I type something in the terminal).
~
#define F_CPU 16000000UL //16MHz
#define BAUD 115200UL //bps
#define BAUD_PRESCALER (((F_CPU/(BAUD*8UL)))-1) //Table 101, page 212
volatile uint8_t temp;
void UART_init(){
//Set baud rate
UBRR0H = (uint8_t)(BAUD_PRESCALER >> 8); //Higher portion of the baud rate
UBRR0L = (uint8_t)BAUD_PRESCALER; //Lower portion of the baud rate
//Enable Receiver and Transmitter
UCSR0B = (1 << RXEN0) | (1 << TXEN0);
//Set frame format: 8-bit format, 1 stop bit, no parity
UCSR0C |= (3 << UCSZ00);
UCSR0C &= ~(1 << USBS0); //clear for 1 stop bit
//Set Double Speed Asynchronous mode
UCSR0A |= (1 << U2X0);
UCSR0B |= (1 << RXCIE0); //Enables interrupt rxc
}
uint8_t UART_getchar(){
//Wait for RXC flag
while(!(UCSR0A & (1 << RXC0))){};
return UDR0;
}
//Arduino setup()
void setup(){
pinMode(8, OUTPUT);
UART_init();
sei();
}
//Arduino loop()
void loop(){
delay(500);
UART_putchar('a');
}
ISR(USART0_RX_vect){
temp = UDR0;
digitalWrite(8, HIGH);
}
It is a lenghty post, but I tried to be specific and also post the whole code. The datasheet I am using for the ATMEGA2560 can be found here.
My Putty settings:
COM Port Settings

I've just tested it on my Mega board and it's working fine without interrupts enabled (simple loopback):
#define BAUD 115200UL
#define BAUD_PRESCALER (((F_CPU/(BAUD*8UL)))-1)
volatile uint8_t temp;
void UART_init(){
//Set baud rate
UBRR0H = (uint8_t)(BAUD_PRESCALER >> 8); //Higher portion of the baud rate
UBRR0L = (uint8_t)BAUD_PRESCALER; //Lower portion of the baud rate
//Enable Receiver and Transmitter
UCSR0B = (1 << RXEN0) | (1 << TXEN0);
//Set frame format: 8-bit format, 1 stop bit, no parity
UCSR0C |= (3 << UCSZ00);
UCSR0C &= ~(1 << USBS0); //clear for 1 stop bit
//Set Double Speed Asynchronous mode
UCSR0A |= (1 << U2X0);
// UCSR0B |= (1 << RXCIE0); //Enables interrupt rxc
}
uint8_t UART_getchar(){
//Wait for RXC flag
while(!(UCSR0A & (1 << RXC0))){};
return UDR0;
}
void UART_putchar(uint8_t data){
//Do nothing until UDR is ready
while(!(UCSR0A &(1 << UDRE0))){};
UDR0 = data;
}
void UART_putstring(uint8_t *data){
while(*data != 0x00){
UART_putchar(*data);
data++;
}
}
//Arduino setup()
void setup(){
pinMode(8, OUTPUT);
UART_init();
sei();
}
//Arduino loop()
void loop(){
UART_putchar(UART_getchar());
}
/*ISR(USART0_RX_vect){
temp = UDR0;
digitalWrite(8, HIGH);
}*/
RX interrupt must be disabled, otherwise Serial interrupt vector is taking place and polled get char can't work as characters are readed by ISR

Related

ATMEGA328P Arduino Uno: Adjusting the brightness of LED with AVR Programming

int led_pin = 9;
int duty_cycle = 63; //in %
int freq_OC = 16000; //in Hz
int freq_clk = 16000000; //in Hz
int prescaler = 1;
void setup() {
//setting the port B pin (D9) as output for OC1A to override the normal port functionality
DDRB = DDRB|(1 << led_pin);
//clearing the counter/timer registers
TCCR1A = 0;
TCCR1B = 0;
//setting the prescaler 1
TCCR1B = TCCR1B|(1 << CS10);
//setting the mode 14 fast PWM
TCCR1B = TCCR1B|((1 << WGM13)|(1 << WGM12));
TCCR1A = TCCR1A|(1 << WGM11);
//setting the fast PWM in non-inverting mode
TCCR1A = TCCR1A|(1 << COM1A1);
ICR1 = (freq_clk/(freq_OC*prescaler)) - 1;
OCR1A = ICR1/(100/duty_cycle);
}
void loop() {
}
Why doesn't this code work when pin D9 is initialised as an output pin OC1A as written above? But it works when...
DDRB = DDRB|(1 << PB1);
Is there any reason for this? As far as the datasheet for ATMEGA328P is concerned, it quotes "If one or both of the COM1X1:0 bits are written to one, the OC1X overrides the normal port functionality of the I/O pin it is connected to". So does that mean the PB1 (or D9) pin no longer functions as an I/O pin and therefore I cannot initialise it with a variable name?
DDRB is an 8-bit register, it has no 9th bit in it.
You have led_pin variable set to 9, therefore operation DDRB = DDRB|(1 << led_pin) has no effect.
There is no D9 pin on the ATmega328P. But looks like you're talking about Arduino board. You can google "Arduino UNO Pinout". E.g. this
You can see D9 pin of the board is connected to PB1 pin of the MCU. Therefore you have to use DDRB |= (1 << 1) (use value 1, not 9)

Arduino USB serial connection to Raspberry Pi (Rasbian) not initializing

I have an Arduino Nano 33 iot that outputs data via Serial at 38400 baud, connected via USB. Setup starts with Serial.Begin. The Raspberry Pi 4, running Raspian buster is set up to receive the data. It can see the correct port, /dev/ttyACM0, but nothing comes in.
I even installed the correct Arduino IDE and SAMD board package on the Raspberry Pi. It still does not find it until after the IDE uploads the replacement sketch and the CPU is reset. The IDE can grab the serial number and board type though. I can then exit out of the IDE and the Arduino is still pumping out serial to the Raspberry Pi.
The only other way to make it work is by pressing the reset button on the Arduino every time a reboot is done on the Raspberry Pi. Serial was tested on the Pi using screen.
Neither of these options are convenient. What am I missing?
/*
Connects via I2C to a CMPS14, outputs NMEA0183 HDM sentences via Serial (38400 baud)
By James Henderson, 2014, adapted to output NMEA sentences by Ian Van Schaick
*/
#include <Keyboard.h>
#include <Wire.h>
#define CMPS14_ADDRESS 0x60 // Address of CMPS14 shifted right one bit for arduino wire library
#define ANGLE_8 1 // Register to read 8bit angle from
unsigned char high_byte, low_byte, angle8;
signed char pitch, roll;
float angle16;
int fine;
float bearingH; // Holds whole degrees of bearing
float bearingL; // Holds decimal digits of bearing
int bearing;
char nbsp;
char mystring[25];
char mystring2[25];
char mystring3[25];
int software;
int cal;
unsigned int _last_status;
uint8_t checksum(char *s)
{
uint8_t c = 0;
while (*s)
c ^= *s++;
return c;
}
void CMPS14_eraseProfil()
{
Wire.beginTransmission(CMPS14_ADDRESS);
Wire.write(0x00);
Wire.write(0xE0);
_last_status = Wire.endTransmission();
delay(20); // 20ms delay after each of the three bytes send
Wire.beginTransmission(CMPS14_ADDRESS);
Wire.write(0x00);
Wire.write(0xE5);
_last_status = Wire.endTransmission();
delay(20); // 20ms delay after each of the three bytes send
Wire.beginTransmission(CMPS14_ADDRESS);
Wire.write(0x00);
Wire.write(0xE2);
_last_status = Wire.endTransmission();
delay(20); // 20ms delay after each of the three bytes send
}
//Correct heading for known deviation
int DeviationCorrect(int Head)
{
return 0;
}
void setup() {
Serial.begin(38400); // Start serial port
Wire.begin();
nbsp = 32;
// CMPS14_eraseProfil();
}
void loop() {
Wire.beginTransmission(CMPS14_ADDRESS); //starts communication with CMPS14
Wire.write(ANGLE_8); //Sends the register we wish to start reading from
Wire.endTransmission();
// Request 5 bytes from the CMPS14
// this will give us the 8 bit bearing,
// both bytes of the 16 bit bearing, pitch and roll
Wire.requestFrom(CMPS14_ADDRESS, 26);
while (Wire.available() < 26); // Wait for all bytes to come back
// software = Wire.read();
// Serial.print("Version: ");
// Serial.println(software);
angle8 = Wire.read(); // Read back the 5 bytes
high_byte = Wire.read();
low_byte = Wire.read();
pitch = Wire.read();
roll = Wire.read();
// int i = 6;
// while (i <= 25) {
// Wire.read();
// i++;
// }
//
// cal = Wire.read();
// Serial.print("Cal: ");
// Serial.println(cal);
bearing = ((high_byte << 8) + low_byte) / 10;
fine = ((high_byte << 8) + low_byte) % 10;
byte data[128] = "$HCHDM,";
data[8] = bearing;
// int deviation = 0;
//DeviationCorrect(bearing);
// bearing = bearing;
//+ deviation;
//Print out NMEA 0183 string HDM
snprintf(mystring, sizeof(mystring), "$HCHDM,%d.%d,M", bearing , fine);
uint8_t crc = checksum(mystring + 1);
Serial.print(mystring);
Serial.print("*");
if (crc < 16) Serial.print("0");
Serial.println(crc, HEX);
//Print out NMEA 0183 string XDR for Pitch
snprintf(mystring2, sizeof(mystring2), "$HCXDR,A,%d,D,PITCH", pitch);
uint8_t crc2 = checksum(mystring2 + 1);
Serial.print(mystring2);
Serial.print("*");
if(crc2 < 16) Serial.print("0");
Serial.println(crc2, HEX);
//Print out NMEA 0183 string XDR for Roll/Heel
snprintf(mystring3, sizeof(mystring3), "$HCXDR,A,%d,D,ROLL", roll);
uint8_t crc3 = checksum(mystring3 + 1);
Serial.print(mystring3);
Serial.print("*");
if(crc3 < 16) Serial.print("0");
Serial.println(crc3, HEX);
delay(100);
}

ERROR SPI on ATmega328P of Arduino

I use SPI on ATmega328 with raw code, data is transferred correctly, but the value return just correct in the second time. I don't know why. I think the data buffer register doesn't update immediately, when I push the button (this is video left is yellow LED, right is red LED: https://drive.google.com/open?id=1bf2XlxRBGCaUo0j2ezB2XFxfSItUmChN and code below)
I use Arduino Uno is a master to send data and get response. Arduino Nano is a slave to receive data and response to master.
Arduino Uno (master) code:
#define DDR_SPI DDRB
#define PORT_SPI PORTB
#define MOSI 3
#define SCK 5
#define SS 2
#define red 2
#define cbi(port, bits) (port) &= ~(1 << (bits))
#define sbi(port, bits) (port) |= (1 << (bits))
volatile uint8_t Data1;
volatile uint8_t Data2;
void setup()
{
Serial.begin(9600); //debug by UART
SPCR = 0; // Reset SPCR = 0
DDR_SPI |= (1<<MOSI)|(1<<SCK)|(1<<SS);// MOSI,SCK,SS - OUTPUT , MISO - INPUT
SPCR |= (1<<SPE)|(1<<MSTR)|(1<<SPR0); // Enable spi , Set master , div/16
pinMode(red,OUTPUT); // led output at pin 2
pinMode(6,INPUT_PULLUP); // internal res input at pin6
pinMode(7,INPUT_PULLUP); // internal res input at pin7
sbi(PORT_SPI,SS); // disable slave
}
uint8_t SPI_MasterTransmit(uint8_t cData)
{
SPDR = cData; // Start transmission
delay(50);
while(bit_is_clear(SPSR,SPIF)); //Wait for transmission complete
return SPDR;
}
void loop()
{
if(bit_is_clear(PIND,6)) // if i push button at pin 6
{
while(bit_is_clear(PIND,6)); // if i still push button at pin 6
cbi(PORTB,SS); // enable slave
Data1 = SPI_MasterTransmit(1); // send value 1 to slave
Serial.print(Data1); // debug
delay(50);
if(Data1 == 2) //if slave response value = 2
digitalWrite(red,LOW); //turnOFF led at pin 2
sbi(PORT_SPI,SS); // disable slave
}
else if(bit_is_clear(PIND,7)) // if i push button at pin 7
{
while(bit_is_clear(PIND,7)); // if i still push button at pin 7
cbi(PORT_SPI,SS); // disable slave
Data2 = SPI_MasterTransmit(3); // send value 3 to slave
Serial.print(Data2); // debug
delay(50);
if(Data2==4) //if slave response value = 4
digitalWrite(red,HIGH); //turnON led at pin 2
sbi(PORT_SPI,SS); // disable slave
}
}
Arduino Nano (slave) code:
#define MISO 4
#define yellow 2
#define cbi(port, bits) (port) &= ~(1 << (bits))
#define sbi(port, bits) (port) |= (1 << (bits))
volatile uint8_t Data;
void setup()
{
SPCR = 0; // reset SPCR = 0
DDRB |= (1<<MISO); // MISO - OUTPUT , SCK,MOSI,SS - INPUT
SPCR |= (1<<SPE)|(1<<SPIE); // enable spi , spi_interrupt
pinMode(yellow ,OUTPUT); // Led output at pin2
}
void SPI_Response(uint8_t cData)
{
SPDR = cData; //Start transmission
while(bit_is_clear(SPSR,SPIF)); //Wait for transmission complete
}
void loop()
{
}
ISR(SPI_STC_vect) // Vector interrupt spi
{
Data = SPDR; // volatile Data = data receive
if(Data == 1)
{
digitalWrite(yellow ,HIGH); // i turnOn led if i receive data = 5
SPI_Response(2); // and response to master a value = 7
delay(10);
}
else if(Data == 3)
{
digitalWrite(yellow,LOW); // i turnOFF led if i receive data = 6
SPI_Response(4); //and response to master a value = 8
delay(10);
}
}
You know the four wire SPI is full duplex, right? The master and the slave are sending the data simultaneously.
The common way how to do what you want is sending control byte and then dummy byte (or bytes) to receive the data.

NRF24L01 with ATTiny and Uno not connecting

I have an ATTiny85 connected to an NRF24L01+ module using this wiring diagram: diagram. The ATTiny85 periodically goes in and out of sleep to send some value to a receiver, an Arduino Uno. If the ATTiny is running off the Arduino power supply (3.3v), everything works correctly. When I run the ATTiny off of a separate CR2032 coin cell that delivers around 3v, the Arduino never receives any data. I have a status LED hooked up to the ATTiny to ensure that the ATTiny is waking correctly, which it is. Here's the code for both:
EDIT:
Connecting it to an external 3.3v not from the Uno makes everything work - why wouldn't the coin cell's voltage work? I think everything is rated below 2.8v, the CR2032 minimum.
ATTiny Code
#include <avr/sleep.h>
#include <avr/interrupt.h>
// Routines to set and claer bits (used in the sleep code)
#ifndef cbi
#define cbi(sfr, bit) (_SFR_BYTE(sfr) &= ~_BV(bit))
#endif
#ifndef sbi
#define sbi(sfr, bit) (_SFR_BYTE(sfr) |= _BV(bit))
#endif
#define CE_PIN 3
#define CSN_PIN 3 //Since we are using 3 pin configuration we will use same pin for both CE and CSN
#include "RF24.h"
RF24 radio(CE_PIN, CSN_PIN);
byte address[11] = "SimpleNode";
unsigned long payload = 0;
void setup() {
radio.begin(); // Start up the radio
radio.setAutoAck(1); // Ensure autoACK is enabled
radio.setRetries(15,15); // Max delay between retries & number of retries
radio.openWritingPipe(address); // Write to device address 'SimpleNode'
pinMode(4, OUTPUT);
digitalWrite(4, HIGH);
delay(500);
digitalWrite(4, LOW);
delay(500);
digitalWrite(4, HIGH);
delay(500);
digitalWrite(4, LOW);
delay(500);
digitalWrite(4, HIGH);
delay(500);
digitalWrite(4, LOW);
delay(1000);
setup_watchdog(6);
}
volatile int watchdog_counter = 0;
ISR(WDT_vect) {
watchdog_counter++;
}
void loop()
{
sleep_mode(); //Go to sleep!
if(watchdog_counter >= 5)
{
digitalWrite(4, HIGH);
watchdog_counter = 0;
payload = 123456;
radio.write( &payload, sizeof(unsigned long) ); //Send data to 'Receiver' ever second
delay(1000);
digitalWrite(4, LOW);
}
}
//Sleep ATTiny85
void system_sleep() {
cbi(ADCSRA,ADEN); // switch Analog to Digitalconverter OFF
set_sleep_mode(SLEEP_MODE_PWR_DOWN); // sleep mode is set here
sleep_enable();
sleep_mode(); // System actually sleeps here
sleep_disable(); // System continues execution here when watchdog timed out
sbi(ADCSRA,ADEN); // switch Analog to Digitalconverter ON
}
// 0=16ms, 1=32ms,2=64ms,3=128ms,4=250ms,5=500ms
// 6=1 sec,7=2 sec, 8=4 sec, 9= 8sec
void setup_watchdog(int ii) {
byte bb;
int ww;
if (ii > 9 ) ii=9;
bb=ii & 7;
if (ii > 7) bb|= (1<<5);
bb|= (1<<WDCE);
ww=bb;
MCUSR &= ~(1<<WDRF);
// start timed sequence
WDTCR |= (1<<WDCE) | (1<<WDE);
// set new watchdog timeout value
WDTCR = bb;
WDTCR |= _BV(WDIE);
}
Receiver Code
#define CE_PIN 7
#define CSN_PIN 8
#include <SPI.h>
#include "RF24.h"
RF24 radio(CE_PIN, CSN_PIN);
byte address[11] = "SimpleNode";
unsigned long payload = 0;
void setup() {
while (!Serial);
Serial.begin(115200);
radio.begin(); // Start up the radio
radio.setAutoAck(1); // Ensure autoACK is enabled
radio.setRetries(15,15); // Max delay between retries & number of retries
radio.openReadingPipe(1, address); // Write to device address 'SimpleNode'
radio.startListening();
Serial.println("Did Setup");
}
void loop(void){
if (radio.available()) {
radio.read( &payload, sizeof(unsigned long) );
if(payload != 0){
Serial.print("Got Payload ");
Serial.println(payload);
}
}
}
Is the problem here that the ATTiny and Uno need to be turned on at the same time to establish a connection, or is it something to do with the battery, or something else entirely? Any help would be appreciated.
I'm experiencing the same problem when running Arduino Nano from a battery.
Nano has a 3.3V pin that I'm using for powering the NRF24L01+ module.
When the voltage from my battery-pack drops under 3.3V, the 3.3V pin voltage also drops. After few minutes, the RF module is not sending any messages.
I fixed the problem temporarily by routing the battery through a 12V step-up regulator that I bought earlier for a different project. These 12V then go to the "UN" pin on Nano which accepts 6-20V. This setup works nicely but is definitely not optimal.
Therefore I'm planning to use a 3.3V step-up regulator such as Pololu 3.3V Step-Up Voltage Regulator U1V11F3 which, according to the supplier, can efficiently generate 3.3V from input voltages as low as 0.5V.
I think this might be helpful to your project as well.

Zigbee/Xbee as Receiver and Transmitter - missing packets on receiver side

My problem statement is very simple. I have one Arduino Uno and another Arduino Mega Board. Both have got Zigbee Shield mounted on them. One of them is working as Transmitter (Uno) and another (Mega) as a receiver.
Code for Tx:
void setup() {
Serial.begin(9600);
}
void loop() {
Serial.println("High");
delay(200);
Serial.println("Low");
delay(200);
}
Code for Rx:
char msg;
const int led = 13; //led at pin 13
void setup() {
Serial.begin(9600);//Remember that the baud must be the same on both arduinos
pinMode(led,OUTPUT);
}
void loop() {
while(Serial.available() ) {
msg=Serial.read();
if(msg=='H') {
Serial.println("Message High");
}
if(msg=='L') {
Serial.println("Message Low");
}
delay(200);
}
}
On Tx side it is sending packets serially High Low
High
Low
High
Low
High
Low
High
Low
High
Low
High
However on the receiver side, I get some packets missing. It is like
Message High
Message High
Message Low
Message High
Message High
Message High
Message Low
Message Low
Message Low
Message Low
Message Low
Message Low
Message Low
Message Low
Message Low
I would expect that it should print
Message High
Message Low
Message High
Message Low
How can i receive packets synchronously and How can i aware of any packetlos on the Rx side.
Thank you for your suggestions, corrections, comments!
First off, this statement is false: "Remember that the baud must be the same on both arduinos." You can run at different baud rates on each end of the wireless link, since the XBee modules buffer your requests. The XBee baud rate just determines the line speed of the serial connection between your host and the radio module. Everything is sent "over the air" at 250kbps.
Second, the problem is that you have a delay inside your while loop on the receiver. With that delay, you can only process one character every 200ms, so you're overflowing your buffer. The other end is sending 7 characters ("HighLow") every 400ms, and you only process 2 of them in that time.
Move the delay outside of the loop and you should be fine.
The following can be interesting to beginners
Arduino Code : Sensor + Xbee Tx
//Reference: Sparkfun
// Declaration of all necessary header file
#include "Wire.h"
#include <SPI.h>
// Declarations of Parameters
int scale_ADXL337 = 3; // 3 (±3g) for ADXL337, 200 (±200g) for ADXL377
int scale_ADXL377 = 200;// 3 (±3g) for ADXL337, 200 (±200g) for ADXL377
float rawX_ADXL337, rawY_ADXL337, rawZ_ADXL337; // Raw values for each axis of Sensor ADXL337
float rawX_ADXL377, rawY_ADXL377, rawZ_ADXL377; // Raw values for each axis of Sensor ADXL377
float scaledX_ADXL337, scaledY_ADXL337, scaledZ_ADXL337; // Scaled values for each axis of Sensor ADXL337
float scaledX_ADXL377, scaledY_ADXL377, scaledZ_ADXL377; // Scaled values for each axis of Sensor ADXL377
boolean micro_is_5V = false; // Set to true if using a 5V microcontroller such as the Arduino Uno, false if using a 3.3V microcontroller, this affects the interpretation of the sensor data
void setup()
{
// Initialize serial communication at 115200 baud
Serial.begin(9600);
//Serial.print("The Accelerometer ADXL377 and ADXL337 are connected to the MEGA BOARD");
//Serial.println();
}
// Read, scale_ADXL337, and print accelerometer data
void loop()
{
// Get raw accelerometer data for each axis
rawX_ADXL377 = analogRead(A0);
rawY_ADXL377 = analogRead(A1);
rawZ_ADXL377 = analogRead(A2);
rawX_ADXL337 = analogRead(A3);
rawY_ADXL337 = analogRead(A4);
rawZ_ADXL337 = analogRead(A5);
scaledX_ADXL377 = mapf(rawX_ADXL377, 0, 1023, -scale_ADXL377, scale_ADXL377);
scaledY_ADXL377 = mapf(rawY_ADXL377, 0, 1023, -scale_ADXL377, scale_ADXL377);
scaledZ_ADXL377 = mapf(rawZ_ADXL377, 0, 1023, -scale_ADXL377, scale_ADXL377);
scaledX_ADXL337 = mapf(rawX_ADXL337, 0, 1023, -scale_ADXL337, scale_ADXL337);
scaledY_ADXL337 = mapf(rawY_ADXL337, 0, 1023, -scale_ADXL337, scale_ADXL337);
scaledZ_ADXL337 = mapf(rawZ_ADXL337, 0, 1023, -scale_ADXL337, scale_ADXL337);
Serial.println(rawX_ADXL337);Serial.println(rawY_ADXL337);Serial.println(rawZ_ADXL337);
delay(200); // Minimum delay of 2 milliseconds between sensor reads (500 Hz)
Serial.println(rawX_ADXL377);Serial.println(rawY_ADXL377);Serial.println(rawZ_ADXL377);
delay(200); // Minimum delay of 2 milliseconds between sensor reads (500 Hz)
}
// Same functionality as Arduino's standard map function, except using floats
float mapf(float x, float in_min, float in_max, float out_min, float out_max)
{
return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}
Receiver : On Aduino + XBEE Explorer (suggested by tomlogic)
long msg;
const int led = 13; //led at pin 13
void setup() {
Serial.begin(9600);//Remember that the baud must be the same on both arduinos
pinMode(led,OUTPUT);
}
void loop() {
while(Serial.available() ) {
msg=Serial.read();
//Serial.println(msg);
Serial.write(msg);
//delay(200);
}
}
Receiver on USB XBEE USB in Ubuntu
Imp : chown username /dev/ttyS1 (in my case ttyS1)
//Source : http://ubuntuforums.org/archive/index.php/t-13667.html
#include <fcntl.h>
#include <stdio.h>
#include <termios.h>
#include <stdlib.h>
#include <strings.h>
/* Change to the baud rate of the port B2400, B9600, B19200, etc */
#define SPEED B9600
/* Change to the serial port you want to use /dev/ttyUSB0, /dev/ttyS0, etc. */
#define PORT "/dev/ttyS1"
int main( ){
int fd = open( PORT, O_RDONLY | O_NOCTTY );
if (fd <0) {perror(PORT); exit(-1); }
struct termios options;
bzero(&options, sizeof(options));
options.c_cflag = SPEED | CS8 | CLOCAL | CREAD | IGNPAR;
tcflush(fd, TCIFLUSH);
tcsetattr(fd, TCSANOW, &options);
int r;
char buf[255];
while( 1 ){
r = read( fd, buf, 255 );
buf[r]=0;
printf( "%s", buf );
}
}

Resources