Arduino strange behaviour dtostrf - arduino

I do not know what's wrong with this code:
#include<stdlib.h>
void setup() {
Serial.begin(115200);
char dataH[5];
char dataC[5];
char dataF[5];
float h = 56.00;
float c = 31.50;
float f = 88.70;
dtostrf(h,5,2,dataH);
dtostrf(c,5,2,dataC);
dtostrf(f,5,2,dataF);
Serial.println(dataH);
Serial.println(dataC);
Serial.println(dataF);
}
void loop() {
// put your main code here, to run repeatedly:
}
I am expecting these as the result:
56.00
31.50
88.70
Instead, this is what I got:
blank
blank
88.70
The first 2 lines were blank (i wrote with text blank otherwise it will skipped by stackoverflow. :(

You should declare your character arrays as
char dataH[6];
char dataC[6];
char dataF[6];
so that they have space to store '\0' at the end

Okay, this my fault. After looking to its doc, I need to create buffer.
char buff[5];
dataH = dtostrf(h,5,2,buff);
I think the buff will immediately garbage collected and will be gone if not consume in next statement.

Related

Parsing char array to integer, atoi() returns squares

I'm using an android app to send values to control servos.
Code:
char inputData[4];
char buffer[3];
void loop()
{
if(Serial.available() > 3) {
for (int i = 0; i < 4; i++){
inputData[i] = Serial.read();
}
char buffer[4];
buffer[0] = inputData[1];
buffer[1] = inputData[2];
buffer[2] = inputData[3];
buffer[3] = '\0';
int angle = atoi(buffer);
Serial.write(angle);
}
}
Issue: I'm getting the values + A-F letters to address each servo - A10, A180, B30 etc. Now the trouble is turning this to an actual integer. As you can see I've declared a character array to store the integers in and as suggested in a post on the arduino forum, I added a \0 at the end of the array. Currently, the Atoi returns random characters, mostly squares and some random numbers. I've tried even assigning them to a string and then .toInt() but same issue there, mostly squares.
Any ideas?
Thanks!
Use print or println to see the number as text. write sends it as byte and Serial Monitor shows a symbol with that ASCII code.

Reading char sent from wireless transmitter and creating switch case

I'm trying to transmit a char wirelessly and have the receiver read that char through serial in order to create a switch statement.
When trying to convert into int, I get the same int value no matter what char I type into the html text box. Serial.print(buffer); is displaying the right char. All help appreciated.
This is the char I'm inputting, and the result in the serial
Let's try this way. After convert "buffer" from "char"-->"int".You can check if h is digital or not before doing something.
//..Your code..//
//..Your code..//
//..Your code..//
int h = (int)buffer;
if (isDigit(h))
{
//Do something;
}

arduino global variable using I2C and LCD

I'm using Arduino Uno in my project : One is a Master and the other is a Slave. I send Data from the Master to the Slave using I2C. I need to send float, but because I2C can only send char, so I'm obligated to transform float into String, then send character by character, and assemble them in the Slave.
The problem I had is, I'm declaring the variable (in the Slave) that contain the Float receiving and assumbling from the Master like a global variable, and I need to use it in my code in the slave, but my problem is that it is always printed as 0, and it dosn't give the right value of it.
The code I'm using is :
#include <LCD16x2.h>
#include <Wire.h>
LCD16x2 lcd;
int buttons;
int sensorPin = A0; // select the input pin for the potentiometer
int sensorValue = 0; // variable to store the value coming from the sensor
float numOut=0; // The Global Variable
int comp=1 ;
String wordd = "";
void setup()
{
Wire.begin(8); // join i2c bus with address #8
Wire.onReceive(receiveEvent); // register event
Serial.begin(9600); // start serial for output
lcd.lcdGoToXY(1,1);
lcd.lcdClear();
lcd.lcdWrite("EG ");
lcd.lcdGoToXY(7,1);
lcd.lcdWrite(numOut,3);
}
void loop()
{
}
// function that executes whenever data is received from master
// this function is registered as an event, see setup()
void receiveEvent(int howMany)
{
wordd = "";
int x = Wire.read();
for (int i=0; i<=x; i++)
{
char c = Wire.read();
wordd += c;
}
numOut = wordd.toFloat();
Serial.println(numOut,3); // print the integer
}
I need to know how to have the result of the global variable "numOut" to use it in my code.
Thank you in advance.!!
Did you actually check if the data received is correct ? I don't know about the Arduino's strings but if your toFloat() fails it might return 0.
Check through your serial port if your wordd variable is correct, and as already mentioned global variables should be declared as volatile.
EDIT:
volatile is necessary here to ensure your data is synchronized between your different function calls (otherwise your program might keep the value inside a register although the "real" value has been updated through an interrupt).
However as someone said volatile is NOT necessary to every global variable, but you won't be using semaphores/mutex... on an Arduino so stick witch volatile for the problem you posted.
EDIT:
https://www.arduino.cc/en/Reference/StringToFloat
"If no valid conversion could be performed because the string doesn't start with a digit, a zero is returned."

Arduino passing char to function

I apologize if this is a stupid question (It probably is) but I am having a hard time getting a function to work correctly.
My code as it stands:
#define photoPin A0
char photoCode[] = "L";
void loop(void) {
analogSensor(photoPin, photoCode);
delay(5000);
}
void analogSensor(int sensorPin, char* sensorCode) {
//Poll the Photo Cell and append that to the buffer
int sensorValue=analogRead(sensorPin);
Serial.print(sensorCode);
sprintf(buf + strlen(buf), "," + sensorCode + ":%04i", sensorValue);
}
When I try to compile this, I get the following message:
In function 'void analogSensor(int, char*)':
i2c_Sensor:104: error: invalid operands of types 'const char [2]' and 'char*' to binary 'operator+'
But, if I comment out the sprintf line, it compiles fine, and ever 5 seconds, an "L" prints out on the screen. Ultimately, I am confused as all get out, and don't know where to turn at this point.
Any help is appreciated.
The last one the formatting got borked :)
I'm afriad you cant concatenate char* with the + operator :) You'd have to call sprintf or strcat :) See your local man pages.
Instead of this:
sprintf(buf + strlen(buf), "," + sensorCode + ":%04i", sensorValue);
Try this:
sprintf( buf+strlen(buf), ",%s:%04i", sensorCode, sensorValue );

Invalid conversion from char to 'uint8_t'

I'm using a shift out statement to drive a few 7 seg displays (in the end)
but I'm running into a problem.
I have used #include <avr/pgmspace.h> libary as to save space for the processing.
At the end of it I need to shiftOut a binary number to be fed into a reg then to a BCD then to my display:
strcpy_P(buffer, (char*)pgm_read_word(&(Times[big])));
shiftOut(dataPin, clockPin, MSBFIRST, buffer);
in the buffer place will go the selected value (e.g. B00100011 should display 23),
my code gives me
Invalid conversion from char to 'uint8_t'
with the shiftOut line highlighted
Any ideas?
The problem here is that shiftOut expects a byte (uint8_t) as its 4th argument (value). The passed value is a char* (presumably declared as a prog_char array).
To fix this, the declaration of the value will need to use prog_uchar, like this:
prog_uchar values[] PROGMEM = { (prog_uchar) B00100011, ... };
...
int valueIndex = ...; // Index of value in the values array
shiftOut(dataPin, clockPin, MSBFIRST, pgm_read_byte(&(values[valueIndex])));

Resources