Send data from phone to arduino to regulate a led brightness

Hello everyone. I connected my led to a pwm pin so that I could regulate its brightness thanks to a slider in my mit app. However, the trick only works when I move the slider to the position, and then it starts dropping by 1 each second, till reaching -1 (I serial.println the variable so that I could see it) I'm just going to paste everything here. The led part is at the bottom, of both arduino and mit blocks

#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 momo;

void setup()
{
       Serial.begin(9600);
       lgUpdateTime = millis();
       pinMode(5, INPUT);        
}
 static bool measure_environment( float *temperature, float *humidity )
  {
  static unsigned long measurement_timestamp = millis( );

  
  if( millis( ) - measurement_timestamp > 500ul )
  {
    if( dht_sensor.measure( temperature, humidity ) == true )
    {
      measurement_timestamp = millis( );
      return( true );
    }
  }

  return( false );
}



void loop()
{ float temperature;
  float humidity;
    
    if (digitalRead(5) == HIGH) 
           {if( measure_environment( &temperature, &humidity ) == true )
  {
    
//    Serial.print( temperature );
//    Serial.print("|");
//    Serial.print( humidity );
//    Serial.println();
 }
    momo = map(momo,0,255,0,100);
    momo = Serial.read();
    analogWrite(9,momo);       
    Serial.println(momo);
    delay(1000);               
           }
      }

Try change from:

momo = map(momo,0,255,0,100);
momo = Serial.read();
analogWrite(9,momo);
Serial.println(momo);
delay(1000);

to:

momo = Serial.read();
momo = map(momo,0,100,0,255);
analogWrite(9,momo);
Serial.println(momo);
delay(1000);

The blocks responsible for sending values cannot be seen in the screenshot because you have disabled them.

The blocks I disabled weren't part of the program I've had an issue with, I had used them because I wanted to see something but the error was still there. I initially wanted the app to send arduino values in both the button event and the slider position changing, but I noticed that it's not as efficient, because the phone told me that the app stopped working. Is there any other way I could be able to have both the button and the slider send data to arduino or should I rely only on one of these? And also, besides those disabled blocks that I didn't use, the slider thumb position block is correct? And finally, how can I keep that one value I send from the phone stable in arduino? It's as if when I send a "40" value, for example, it goes up to that value only for an instant, and then it drops right after. Sorry for the many questions

  1. Use only one clock to read temperature and humidity. After reading, split the data into "|", enter the first item from the list into the temperature label, and the second one into the humidity label.

  2. I assume your slider is set to the maximum value of 100. So why are you sending data as 4 bytes? Send as one byte.

  3. You can use the slider extension to send data only when you lift your finger off the slider. Your arduino code has a latency of 2s, so the led will not respond very quickly.

  1. You should rewrite the arduino code so that arduino sends data to android every 2s, while android data should be read without delay. Read about the multitasking and milis function in arduino.
1 Like

Hello, I've changed the code according to what you said (I also made the clock timers 20% faster because the phone would crash otherwise), and now it works a lot better, it's more responsive. However, I still have one major problem: when I set the slider to a certain value, the led will assume that brightness only till the next Serial.read(), and then it drops again to -2 (that is an odd value, why's that?). Could the slider extension I've yet to download solve this? Or is it another error in the code?

As you can see here, I've entered the value "52" from the phone (which of course turned to 132 thanks to map function), but then the value goes back to -2

The serial.read () command should be secured like this:

    // reply only when you receive data:
    if (Serial.available()) {
    // read the incoming byte:
    incomingByte = Serial.read();}
1 Like

About the extension, how should I even download it? I tried putting the url but mit app tells me that the uploaded file does not contain any component definition files

You download Aix to disk, to appinventor you import the aix file from disk.

1 Like

Hello. I want to regulate several leds by sending data from my phone to arduino and, in order to specify to arduino on which led I want to control, I've thought of creating a new variable (in both the app and on arduino) I would assign a different value for each selected item from the spinner and then I would send it to arduino. Then, thanks to the switch function, arduino would be able to distinguish which led to power up, but apparently that's not the case. Here, I will leave both arduino and app sketches.

bluetooth_automation.txt (1.4 KB)

why are you starting a new thread?
I now merged both...

Taifun

I didn't know how the forum worked, I thought that after some time I wasn't gonna be able to reply to the other thread anymore. But it's fine, sorry for the mess

Can someone help me please?

I don't totally get what you're doing, but I'm just going to describe how I would do it.
Maybe that helps.

First:
Make the android app that has output like that: a | 45
The letter ist the led we want to control the number is the brightness.
I think you know how to get both pieces of information with app inventor and there is a tool to merge them with a | .
Then I'd sent this to the Arduino. (And only send a new code over Bluetooth if the user has changed the input)

Second:
Make the Arduino read the serial Bluetooth if available.
Then it reads the received code in. There s a simple code to divide between the | .
Now you got two variables, one for the led, and one for brightness.
Now you just need two lines of code to set the led and the brightness.

I hope I was able to help you a bit.
If not feel free to ask again and I'll try to help furthermore.

1 Like

Oh okay, you got it right, by the way. However, I don't really know how to divide the values in arduino. I've tried googling it but every code uses different functions and that confuses me even more. Do you have any tips on how to write this for the program?

Oh great, now that I know the problem I'm sure we can find a solution.

Yeah, I've done it before and should be able to provide a sample code for the Arduino.
I just have to search for it on my harddrive. Gonnna do this in the evening and then send it to you.

1 Like

Thank you so much! In the meanwhile I'll keep on searching as well

Look this:
http://www.martyncurrey.com/turning-a-led-on-and-off-with-an-arduino-bluetooth-and-android-part-iii-3-leds-and-3-switches/

1 Like

Sorry I got caught up in work yesterday.

while(Serial.available() > 0){

//READ THE STRING TO THE FIRST "|" DIVIDER AND STORE AS A VARIABLE
String myDate = Serial.readStringUntil('|');
Serial.read();
//STORE THE NEXT SECTION OF STRING AS A VARIABLE
String myTime = Serial.readStringUntil('\n');
Serial.read();

That's the code I have used to dive strings on the Arduino.
If you need any further help, just let me know.

1 Like

Hello. First of all thank you for the help on that, now it works just fine! Now, though, I've got another issue: I will try to explain it here.
I had made my dht11 sensor send temperature and humidity data to my phone and display it on my phone, and that works. However, I also want to set a value like a temp_max, which in case it gets reached, my arduino will trigger the red led on and off (like an alarm, but that's not the problem here). The actual problem is that whenever I set this max value more than once (sometimes twice), a mit app inventor error gets displayed. Since it had displayed either once or twice, I strongly believe that it's got something to do with clock from mit and the measurement time from arduino. I will send both the sketches and also the error that gets displayed.

bluetooth_automation.txt (8.6 KB)

You need to guard against short lists in the circled places the same as you do in the Clock Timer.