Hi can somebody help me? I have led lamp that works with serial monitor when i write 101 it turn on color red and when I write 1-100 it change brightness from 1%-100% but i have problem when sending data from my mobile app to serial port it doesnt show '101' but 1 0 1 or in asci code how can i convert it?Is it possible or i should re-write my program?
Yes, it is possible.
You should re-write your program. Unless it already works perfectly.
https://www.arduino.cc/reference/en/language/functions/communication/serial/parseint/
how do you do that ?
the Serial monitor will send 101 in ASCII. So you likely deal with ASCII already.
do you mean there are spaces in between the digits?
Please post your program, in code tags. Of course we need to see it. Wouldn't you?
Im using app inventor that send 1 byte number to arduino
i can use ASCII if i put on app inventor sendtext
and 1 0 1 if i put send1bytenumber
and i need to look like this:
currentStatus = 105
temp = 1
buff = 1
temp =
#include <Adafruit_NeoPixel.h>
#define LED_PIN 5 //PIN PRE LEDKY
#define LED_COUNT 47 //POČET LEDIEK
enum {
RAINBOW = 101,
GREEN,//102
RED,//103
BLUE,//104
WHITE,//105
YELLOW,//106
MAGENTA,//107
CYAN,//108
OFF//109
};
Adafruit_NeoPixel strip(LED_COUNT, LED_PIN, NEO_GRB + NEO_KHZ800);
//int k;
//int val;
int currentStatus; // urcuje aktualnu farbu - hodnota prijata zo serioveho portu
String buff; // prijate byty a ulozi String
int output = 0; // pomocna premenna, ktora uchovava v sebe hodnotu pred pridelenim do hlavnej (brightness alebo currentStatus)
int brightness = 255; // jas
long hue = 0; // hodnota aktualnej farby - pre rainbow
int currentLed = 0; // sleduje poziciu LED, s ktorou sa prave pracuje
bool resetLoop = false; // uchovava informaciu, ci sa "cyklus" ma zacat od znova
void rainbow_new(int wait);
void colorWipe_new(uint32_t color, int wait);
void setup()
{
Serial.begin(9600);
/*
#if defined(__AVR_ATtiny85__) && (F_CPU == 16000000)
clock_prescale_set(clock_div_1);
#endif
*/
strip.begin();
strip.show();
strip.setBrightness(brightness); //JAS
//colorWipe(strip.Color(0, 255, 0), 50);
}
void loop() //CYKLUS
{
while (Serial.available() > 0)
{
if (resetLoop)
{
buff = "";
currentStatus = 0;
}
//val = Serial.read(); //PREČITANIE SÉRIOVEHO MONITORU
char temp = Serial.read();
Serial.print("temp = ");
Serial.println(temp);
if (temp != 10)
{
buff += temp;
Serial.print("buff = ");
Serial.println(buff);
continue;
}
output = buff.toInt();
if (output >= 0 && output <= 100)
{
//strip.setBrightness(brightness);
brightness = output;
}
else if (output >= 101 && output <= 109)
{
currentStatus = output;
}
//currentStatus = buff.toInt();
buff = "";
Serial.print("currentStatus = ");
Serial.println(currentStatus);
}
strip.setBrightness(brightness);
switch (currentStatus)
{
case RAINBOW:
rainbow_new(20);
break;
case RED:
colorWipe_new(strip.Color(255, 0, 0), 50);
break;
case GREEN:
colorWipe_new(strip.Color(0, 255, 0), 50);
break;
case BLUE:
colorWipe_new(strip.Color(0, 0, 255), 50);
break;
case WHITE:
colorWipe_new(strip.Color(255, 255, 255), 50);
break;
case YELLOW:
colorWipe_new(strip.Color(255, 255, 0), 50);
break;
case MAGENTA:
colorWipe_new(strip.Color(255, 0, 255), 50);
break;
case CYAN:
colorWipe_new(strip.Color(0, 255, 255), 50);
break;
case OFF:
colorWipe_new(strip.Color(0, 0, 0), 50);
break;
default:
break;
}
}
void rainbow_new(int wait) {
if ((hue >= 5 * 65536) || (resetLoop))
{
hue = 0;
}
if ((currentLed == strip.numPixels()) || (resetLoop))
{
currentLed = 0;
}
int pixelHue = hue + (currentLed / strip.numPixels());
strip.setPixelColor(currentLed, strip.gamma32(strip.ColorHSV(pixelHue)));
hue += 256;
currentLed++;
resetLoop = false;
strip.show();
delay(wait);
Serial.println(hue/5);
}
void colorWipe_new(uint32_t color, int wait) {
if ((currentLed == strip.numPixels()) || (resetLoop))
{
currentLed = 0;
}
strip.setPixelColor(currentLed, color);
resetLoop = false;
currentLed++;
strip.show();
delay(wait);
}
In your Mobile's Bluetooth Terminal check that the transmission mode is ASCII and not HEX.
Why are you using the 'continue' keyword there?
im using it for current status continue changing value
if i delete it my currentstatus dont change and stay for 0 value and doesnt change color or brightness, i can also replace it with break but there is no change between
after deleting continue:
I have no idea what you mean by that.
This fails. The expression 5 * 65536 overflows the default capacity of a literal integer expression.
if you do something like this
you are sending 102 as a value stored in 1 byte. So 1 byte will make it to the other side and you'll receive in hex 0x66 (which is the same as 102 in decimal). This won't be ASCII. but you could directly use this value in the code as a number, no need to parse it.
show the app inventor program (good resolution screen grab)
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.