Call of overloaded 'println(char [4], int)' is ambiguous - arduino

I've two Arduinos and I want to make them communicate via TX/RX. Arduino one as a sender, and Arduino two as a receiver. But I have a problem with the receiver code and get this error:
call of overloaded 'println(char [4], int)' is ambiguous
Can anyone help me? Thank you.
This is my receiver code:
char str[4];
void setup(){
Serial.begin(9600);
Serial.begin(9600);
}
void loop() {
int i=0;
if (Serial.available()){
delay(100); //allows all serial sent to be received together
while(Serial.available() && i<4){
str[i++] = Serial.read();
}
str[i++]='\0';
}
if(i>0){
Serial.println(str, 4);
}
}

Why do you have two Serial.begin(9600) in the setup?
After the while loop, the i var is equal to 4. Your biggest index in str array is str[3] (0-3), so you are accesing the 4th index which doesn't exist.You should remove that str[i++] = '\0', and if you want to make communication you should do something like :
if (i > 0) {
for (int j=0; j<4; j++) {
Serial.print(str[j]);
}
Serial.println("");
}

Related

how to read char array from serial monitor and command arduino accordingly?

currently, I am working on a project to read char inputs from serial monitor and command Arduino to switch on/off specific pins. The problem I am facing is, I am unable to read the complete char array entered in the serial monitor. can anyone tell me what am I doing wrong?
#define X 13 //led pin
char txt[15];
int i;
int Status=0;
void setup() { // put your setup code here, to run once:
pinMode(X,OUTPUT);// setting the pin flow of control as output
Serial.begin(9600);
while(!Serial)
{
; //to wait for pc to connect
}
Serial.println("\nHome Automation");
dashprint();
}
void loop() { // put your main code here, to run repeatedly:
if(Serial.available()>0)
{ i=0;
while(Serial.available()>0) //if serial available
{ char inchar=Serial.read();
txt[i]=inchar; // add char to txt string
i++;// increment to where to write next
txt[i]='\0'; //null termination
}
Serial.print(txt);
check();
}
}
void dashprint() //to print dashes
{
Serial.println("-----------------------------------------------");
Serial.println("give me some command"); //ask for command
}
void check()
{ if(strncmp(txt,"ON",2)==0)
{
digitalWrite(X,HIGH);
Status=1;
}
else if(strncmp(txt,"OFF",3)==0)
{ digitalWrite(X,LOW);
Status=0;
}
else if(txt=="STATUS")
{
}
else Serial.println("ERROR");
}
output:
Home Automation
give me some command
OERROR
NERROR
ERROR
expected output:
Home Automation
give me some command
ON
Your arduino is too fast to read the text "ON" in one round.
9600 is 1 ms per character.
A simple workaround is, to add a little delay
if(Serial.available()>0) {
delay(3); // wait for the whole message
i=0;
while(Serial.available()>0) {
...
ADD:
Additionally, you obviously receive a '\n' (newline) character. Make sure it's not causing troubles.
And increase the delay or have a better approach in general, if you expect more than 3 characters ( "STATUS" )
struct MYObject {char a[256];};
//structure works well with EEPROM put and get functions.
//also lets to input large strings, more then 64 bytes, as http
void setup() {
MYObject str ={" "};
Serial.begin(115200);
while (!Serial.available()) delay (10); //wait for string
int i = 0;
int k= Serial.available();
while (k > 0){
char inchar = Serial.read(); //read one by one character
str.a[i] = inchar;
i++;
if (k < 3) delay (10); //it gives possibility to collect more characters on stream
k = Serial.available();
}
str.a[i]='\0'; //null terminator
//now lets see result
Serial.println(str.a);
//.....
}

No output from serial monitor

I'm trying to convert a sensor value to binary to be stored in a small char array (tbuf) and concatenate the small arrays into one bigger array. Ultimately, I want to send the big array (pbuf) filled with 7, 10-bit values over serial with Serial.write(). This is the code that I've tried so far:
void setup() {
Serial.begin(115200);
}
void loop() {
if (Serial.available() > 0) {
int s = 251;
char pbuf[78];
char tbuf[11];
itoa(s, tbuf, 2);
strcat(pbuf, tbuf);
Serial.println(pbuf);
}
Serial.end();
}
It doesn't output the permanent buffer (pbuf), or anything, to the serial monitor. My output to the serial monitor is nothing.
I would appreciate any suggestions on how to get an output from code similar to this.
Don't use Serial.end() in function loop(). Your code runs one time Serial.begin() and if your Serial is not available then you are calling only Serial.end().
Maybe you could try this approach:
int varA =0;
void setup() {
Serial.begin(115200);
}
void loop() {
if (Serial.available() > 0) {
varA = Serial.parseInt(); //or Serial.parseFloat();
Serial.println(varA);
}
}

Arduino Serial Communication can't read integers

I used the following code to read sequences of integers from Arduino (Arduino-Mega 2560) serial communication
but it gives me garbage (or wrong value), help me to find out the bug please...
int time=0,i;
void setup() {
pinMode(6,OUTPUT);
Serial.begin(9600);
}
void loop(){
i=0;
while(Serial.available()>0) {
time=Serial.parseInt();
}
for(i=1;i<=time;i++){
digitalWrite(6,1);
delay(150);
digitalWrite(6,0);
delay(100);
}
time=0;
}
Your code is wrong. Or at least the logic is wrong.
Try this:
void loop(){
if(Serial.available()) {
time=Serial.parseInt();
for(i=1;i<=time;i++){
digitalWrite(6,1);
delay(150);
digitalWrite(6,0);
delay(100);
}
time=0;
}
}
Your code looks like it should work, but maybe the problem is in the data you are sending.
Serial.parseInt() expects ascii characters that represent a number. Are you sending a number encoded as a string of chars? Note that Serial.write(99) will send a single byte with value 99.
Since the numbers are in the range 0 to 99, they will fit in a single byte. Why not just send a byte and read the byte on the other end?
void setup() {
pinMode(6,OUTPUT);
Serial.begin(9600);
}
void loop(){
int time=0;
if(Serial.available()>0) { // using while will read all the bytes and use the last one
time = Serial.read();
}
for(int i=1; i<=time; i++){
digitalWrite(6,1);
delay(150);
digitalWrite(6,0);
delay(100);
}
}

Arduino Xbee Data parsing

Im sorry to make a post like this but i have tried everything and i cant get this working!
I have two arduinos hooked up with xbee's.
One is connected to my computer recieving data and the other is bettery powered and has a Wii nunchuck attached.
I know im getting good data from the nunchcuck cause i tested it without the xbee.
But i want to send the data over serial and recieve on the other to use for something else but doesnt seem to be working. Here is the code:
Arduino with wii:
#include <Wire.h>
#include <Servo.h>
const int vccPin = A3;
const int gndPin = A2;
Servo servo;
const int dataLength = 6; // Number of bytes to request
static byte rawData[dataLength];
enum nunchuckItems {
JoyX, JoyY, accelX, accelY, accelZ, btnZ, btnC};
void setup()
{
pinMode(gndPin, OUTPUT);
pinMode(vccPin, OUTPUT);
digitalWrite(gndPin, LOW);
digitalWrite(vccPin, HIGH);
servo.attach(9);
delay(1000);
Serial.begin(9600);
nunchuckInit();
}
void loop()
{
nunchuckRead();
int joyX = getValue(JoyX);
int joyY = getValue(JoyY);
Serial.print(joyX);
Serial.print(",");
Serial.print(joyY);
Serial.println();
}
void nunchuckInit(){
Wire.begin();
Wire.beginTransmission(0x52);
Wire.write((byte)0x40);
Wire.write((byte)0x00);
Wire.endTransmission();
}
static void nunchuckRequest(){
Wire.beginTransmission(0x52);
Wire.write((byte)0x00);
Wire.endTransmission();
}
boolean nunchuckRead(){
int cnt = 0;
Wire.requestFrom(0x52, dataLength);
while (Wire.available()){
rawData[cnt] = nunchuckDecode(Wire.read());
cnt++;
}
nunchuckRequest();
if (cnt >= dataLength)
return true;
else
return false;
}
static char nunchuckDecode(byte x){
return (x ^ 0x17) + 0x17;
}
int getValue(int item){
if (item <= accelZ)
return (int)rawData[item];
else if (item == btnZ)
return bitRead(rawData[5], 0) ? 0: 1;
else if (item == btnC)
return bitRead(rawData[5], 1) ? 0: 1;
}
How could i recieve this data on the recieving arduino?
Please help its for my school project!
Thank you!!
When you read the terminal (without the Xbee) do you see line with X,Y appear ? Because if your arduino terminal see it, the problem comes from the Xbee.
If your terminal see the line, look at your Xbee with Xctu. You must set the panID on both Xbee to see them communicate. you must also make the SL address of the sender equal to the DL address of the receiver (and same for the SH/DH).
Can you say us which Arduino, Xbee, shield you use. It can help us to have more details

xbee pro s2b receiving random data

I have a trouble with xbee s2b.
I try to receive two joystick values using only xbee placed on sparkfun regulated board (Coordinator API) and process these data on Arduino Uno connected to other xbee s2b placed on explorer board.(Router API). I configured xbees using X-CTU properly and I adjusted DIO0 and DIO1 to ADC[2] on Router Xbee. There is no problem when working with one joystick. But when I try to receive two joystick values at the same time, they are not working correctly. When I look the incoming data on serial monitor, I see;
https://lh3.googleusercontent.com/-20vjr0EchsQ/VqyZXgq84VI/AAAAAAAAA_0/WhEtoOU61vA/s1280-Ic42/Screenshot_3.jpg
My Arduino code is:
int packet[32];
void setup()
{
Serial.begin(9600);
}
void loop(){
if (Serial.available() > 0) {
if (Serial.read() == 0x7E) {
packet[0] = 0x7E; //start delimiter
packet[1] = readByte(); //MSB1
packet[2] = readByte(); //MSB2
int dataLength = (packet[1] << 8) | packet[2]; //between the length and the checksum
printPacket(dataLength+4);
Serial.println("");
}
}
delay(1000);
}
void printPacket(int k) {
for(int i=0; i < k; i++) {
Serial.print(packet, HEX);
Serial.print(" ");
delay(1000);
}
}
int readByte() {
while (true) {
if (Serial.available() > 0) {
return Serial.read();
}
}
}
What is the point I missed? Can you help me about this issue. Thank you in advance.
The delay(1000) statements may be causing you to lose characters from your serial buffer, and might not be necessary.
The code you shared appears incomplete. Where are you reading the dataLength bytes of the packet? How does printPacket() print the bytes?
Your inbound packet buffer should be larger -- I think the XBee S2B can have network payloads of up to 255 characters, in addition to the frame header and checksum.
You've created a blocking readByte() call, which isn't good program design. Consider something like this instead:
unsigned char packet[300];
int packet_index = 0;
int packet_length;
void loop() {
while (Serial.available() > 0) {
packet[packet_index++] = Serial.read();
if (packet_index == 3) {
packet_length = (packet[1] << 8) | packet[2];
}
if (packet_index > 2 && packet_index == packet_length) {
print_packet();
packet_index = 0;
}
}
}
void print_packet() {
int i;
for (i = 0; i < packet_length; ++i) {
Serial.print(packet[i], HEX);
Serial.print(" ");
}
Serial.println("");
}
If you have too much data to print and you're overflowing your outbound serial buffer, try increasing the speed of the console's serial port to 115200bps. Or print the bytes as you receive them instead of waiting until the packet is complete.

Resources