i have a value q that is int and can arrive only in range of 0 - 9.
and i have the sending function that needs a char value to work.
i need to convert the q to char value and send it.
the code im using there:
int q = 5;
//need to convert q to char c
//typicaly like so 'q'
//
Write(c,'!');
if im using something like that c[1] = '\(q)'; i getting error from arduino app:
invalid conversion from 'char*' to 'char' [-fpermissive]
so how can i solve it?
Try this:
char c;
int q = 5;
c = q + '0'; // convert the number to a character corresponding to it
Write((char)q, '!'). Sounds like very simple question or maybe I have not understood it.
Related
I have a few question, how do I convert Float to String?
Because my OLED display require 'String' and cannot print Float
Here is my coding
WindSpeed = WIND_SPEED_20_PULSE_SECOND / ONE_ROTATION_SENSOR * (float) Rotations;
float SpeedMPH = ((WindSpeed * 3600) / CONVERTMPH_FORMULA);
String WindSpeedMS = WindSpeed;
if((millis() - Start_Read_Timer) >= READ_TIME)
{
cli();
WindSpeedStatus();
sei();
Rotations = 0;
Start_Read_Timer = millis();
}
display.setFont(ArialMT_Plain_24);
display.drawString(0, 20, WindSpeedMS);
display.display();
delay(500);
The error I got is:
Compilation error: conversion from 'float' to non-scalar type 'String'
requested
Thanks!
Try
float SpeedMPH = ((WindSpeed * 3600) / CONVERTMPH_FORMULA);
String WindSpeedMS = String(SpeedMPH,0); // 2nd param is decimal digits
refer to Arduino String library as they stated that :
Syntax
String(val)
String(val, base)
String(val, decimalPlaces)
Parameters
val: a variable to format as a String. Allowed data types: string,
char, byte, int, long, unsigned int, unsigned long, float, double.
base: (optional) the base in which to format an integral value.
decimalPlaces: only if val is float or double. The desired decimal
places.
so instead of writing :
String WindSpeedMS = WindSpeed;
you should write :
String WindSpeedMS = String(WindSpeed, 5);
where 5 represents the number of decimal places desired, so if WindSpeed = 12.54545451 then WindSpeedMS = "12.54545"
I am trying to get user input from the serial monitor to turn a stepper motor according to the input. However my code returns the ASCII value rather than the original input.
#include <Stepper.h>
Stepper small_stepper(steps_per_motor_revolution, 8, 10, 9, 11);
void setup() {
// Put your setup code here, to run once:
Serial.begin(9600);
Serial.println("Ready");
}
void loop() {
// Put your main code here, to run repeatedly:
int Steps2Take = Serial.read();
Serial.println(Steps2Take); // Printing
if (Steps2Take == -1)
Steps2Take = 0;
else {
small_stepper.setSpeed(1000); // Setting speed
if (Steps2Take > 0)
small_stepper.step(Steps2Take * 32);
else
small_stepper.step(-Steps2Take * 32);
delay(2);
}
}
Just use the .toInt() function.
You should read the string from your serial and after that convert it to integer.
Serial.print(Serial.readString().toInt());
You could do this three ways! Notice, if the number is greater than 65535 then you have to use a long variable. With decimals use float variable.
You can use the toInt(), or toFloat() which require a String type variable. Heads up as the toFloat() is very time consuming.
// CODE:
String _int = "00254";
String _float = "002.54";
int value1 = _int.toInt();
float value2 = _float.toFloat();
Serial.println(value1);
Serial.println(value2);
// OUTPUT:
254
2.54
You could use the atoi. The function accepts a character array and then converts it to an integer.
// CODE:
// For some reason you have to have +1 your final size; if you don't you will get zeros.
char output[5] = {'1', '.', '2', '3'};
int value1 = atoi(output);
float value2 = atof(output);
Serial.print(value1);
Serial.print(value2);
// OUTPUT:
1
1.23
If you had a Character Array and and wanted to convert it to a string because you didn't know the length...like a message buffer or something, I dunno. You could use this below to change it to a string and then implement the toInt() or toFloat().
// CODE:
char _int[8];
String data = "";
for(int i = 0; i < 8; i++){
data += (char)_int[i];
}
char buf[data.length()+1];
data.toCharArray(buf, data.length()+1);
If it is just a "type-conversion" problem, you can use something like this:
int a_as_int = (int)'a';
or
#include <stdlib.h>
int num = atoi("23"); //atoi = ascii to integer
as it was point out here.
Does it solve the problem?
I'm trying to figure out this program; it is an averaging program and it requires user input of:
p 4 p 7 p 2 n 1 e sum 12 average: 4
The user enters whether he was a positive number or negative.
We are asked to use int real_number(int* value) and make value a pointer to where the input value will be stored.
So far I have:
#include <stdio.h>
int real_number(int* value);
int real_number(int* value)
{
char *n = "negative";
char *p = "positive";
char *e = "end";
int *sum = 0;
int *avg = 0;
while(sum = 0)
{
printf(" \n");
scanf("%d", &sum);
}
}
int main()
{
}
I know it is not much, but I'm lost; any ideas?
Firstly you have to read characters while your character is different "e". Secondly you have an infinite cicle. In while loop modify the condition with == .
You need to have a counter to count how many numbers you entered. While you read numbers you must add these numbers to sum and count one.
Finally, in the output you write sum/counter
I have int A, B, C. And A is in range 0-9999, B is 0-99, C is 0-99.
Because the function must return only one double, I think of putting them all into one number. Otherwise I need to call function three times.
But I cannot write an efficient code to do this. This will be called millions times, so it should be quite effective, but no ASM.
I need a function double pack3int_to_double(int A, int B, int C) {}
Couldn't you just store A + 1000B + 100000C?
For example, if you wanted to store A = 1234, B = 6, and C = 89, you'd just store
89061234
CCBAAAA
You can then extract the numbers by casting the double to an int and using standard integer division and modulus tricks to recover the individual values.
Hope this helps!
If A<10,000 and B & C <100, A can be expressed with 14 bits, and B & C with 8 bits. Thus you need 30 bits in total.
You could therefore pack/unpack the integers by shifting it to the right place:
int packed = A + B<<14 + C<<22;
A = packed & 0x3FFF; B = (packed >> 14) & 0xFF; C = (packed >> 22) & 0xFF;
Bit shifting is of course MUCH faster than multiply/divide, and you can cast the int to a double and vice versa.
This is technically not legal C code, so you would use this at your own risk:
typedef union {
double x;
struct {
unsigned a : 14;
unsigned b : 7;
unsigned c : 7;
} y;
} result_t;
The C standard doesn't allow using a union member to write a value and a different one to read it out, but I am not aware of a compiler that does the static analysis to diagnose such a problem (it doesn't mean one won't do so in the future). Also, using certain int values may result in a trap representation for a double. But, if you know your system will not generate any trap representations, you can consider using this.
double pack3int_to_double(int A, int B, int C) {
result_t r;
r.y.a = A;
r.y.b = B;
r.y.c = C;
return r.x;
}
void unpack3int_from_double (double X, int *A, int *B, int *C) {
result_t r = { X };
*A = r.y.a;
*B = r.y.b;
*C = r.y.c;
}
You can use out parameters in function call and retrieve all 3 int variables.
You could return a NaN double with the data stored in the mantissa. That gives you 53 bits to utilize. Should be plenty.
http://en.m.wikipedia.org/wiki/NaN
Inspired by your answers, this is what I come up so far. This should be quite efficient, and only 32 bits are used, so the exponent of the double is not touched.
struct pack_abc {
unsigned short a;
unsigned char b, c;
int safety;
};
double pack3int_to_double(int A, int B, int C) {
struct pack_abc R = {A, B, C, 0}; // or 0 could be replaced with something smater, like NaN?
return *(double*)&R;
}
void main() {
int w = 1234, a = 56, d = 78;
int W, A, D, i;
double p = pack3int_to_double(w, a, d);
// we got the data packed into 'p', now let's unpack it
struct pack_abc *R = (struct pack_abc*) & p;
printf("%i %i %i\n", (int)R->a, (int)R->b, (int)R->c);
}
I'm using the rainbowduino and it has some methods that take individual r g b values as unsigned chars, and some that take a 24bit rgb colour code.
I want to convert r g b values into this 24bit colour code of type uint32_t (so that all my code only has to use r g b values.
Any ideas?
I have already tried uint32_t result = r << 16 + g << 8 + b;
r = 100 g =200 b=0 gave green, but r=0 g=200 b=0 gave nothing
Rb.setPixelXY(unsigned char x, unsigned char y, unsigned char colorR, unsigned char colorG, unsigned char colorB)
This sets the pixel(x,y)by specifying each channel(color) with 8bit number.
Rb.setPixelXY(unsigned char x, unsigned char y, unit32_t colorRGB)
This sets the pixel(x,y)by specifying a 24bit RGB color code.
The drivers code is:
void Rainbowduino::setPixelXY(unsigned char x, unsigned char y, uint32_t colorRGB /*24-bit RGB Color*/)
{
if(x > 7 || y > 7)
{
// Do nothing.
// This check is used to avoid writing to out-of-bound pixels by graphics function.
// But this might slow down setting pixels (remove this check if fast disply is desired)
}
else
{
colorRGB = (colorRGB & 0x00FFFFFF);
frameBuffer[0][x][y]=(colorRGB & 0x0000FF); //channel Blue
colorRGB = (colorRGB >> 8);
frameBuffer[1][x][y]=(colorRGB & 0x0000FF); //channel Green
colorRGB = (colorRGB >> 8);
frameBuffer[2][x][y]=(colorRGB & 0x0000FF); //channel Red
}
}
So I would think similar to the above :
uint8_t x,y,r,b,g;
uint32_t result = (r << 16) | (g << 8) | b;
Rb.setPixelXY(x, y, result);
should work. It I think the above likely needs the parenthesis, to ensure proper ordering, as "+" is higher than "<<". Also likely won't hurt but the "|" is better, as not to prevent undesired carry's.
P.S. Remember when shifting to be unsigned, unless you want arithmetic shift versus logical.
and on that note I don't like shifts as they are often messed up and inefficient. Rather a union is simple and efficient.
union rgb {
uint32_t word;
uint8_t byte[3];
struct {
uint8_t blue;
uint8_t green;
uint8_t red;
} color ;
}rgb ;
// one way to assign by discrete names.
rbg.color.blue = b;
rbg.color.green = g;
rbg.color.red = r;
//or assign using array
rgb.byte[0] = b;
rgb.byte[1] = g;
rgb.byte[2] = r;
// then interchangeably use the whole integer word when desired.
Rb.setPixelXY(x, y, rgb.word);
no messing with keeping track of shifts.
One way to approach this would be to shift the bits to the left...
uint32_t result = r << 16 + g << 8 + b;