Hello, I've been making an app on MIT App Inventor that basically involves controlling several leds' brightnesses simply by sliding your finger from one side to the other (that's a graphic feature coming from the app) and each thumb position will send a value to arduino that will regulate the led's brightness (by using the pwm pin) and so far so good, it worked. But now that I have to put more leds, I also have to make sure arduino knows which led I want to control, so I've thought about creating a new variable in my program that, when sent together with the led value, it would recognize which led I want to use. The only problem is that I can't divide the 2 values read on the serial monitor. Here, I will send the arduino code below! (I can't send the file so I'll have to paste it, don't mind the first part in the loop since that's another part of the program)
#include <dht_nonblocking.h>
#define DHT_SENSOR_TYPE DHT_TYPE_11
#define LED_BLUE 9
static const int DHT_SENSOR_PIN = 2;
DHT_nonblocking dht_sensor( DHT_SENSOR_PIN, DHT_SENSOR_TYPE );
unsigned long lgUpdateTime;
int led_value;
int led_color;
String led_color_str;
String led_value_str;
void setup()
{
Serial.begin(9600);
lgUpdateTime = millis();
pinMode(4, INPUT);
}
static bool measure_environment( float *temperature, float *humidity )
{
static unsigned long measurement_timestamp = millis( );
if( millis( ) - measurement_timestamp > 1000ul )
{
if( dht_sensor.measure( temperature, humidity ) == true )
{
measurement_timestamp = millis( );
return( true );
}
}
return( false );
}
void loop()
{ float temperature;
float humidity;
if (digitalRead(4) == HIGH)
{if( measure_environment( &temperature, &humidity ) == true )
{
Serial.print( temperature );
Serial.print("|");
Serial.print( humidity );
Serial.println();
}
}
if (Serial.available() > 0) {
led_color_str = Serial.readStringUntil(';');
int led_color = led_color_str.toInt();
Serial.println(led_color);
switch(led_color){
case 1:
int led_value = Serial.read();
led_value = map(led_value,0,100,0,255);
Serial.println(led_value);
analogWrite(9,led_value);
break;
}
}
}