How to program arduino uno with code, connected with bluetooth

Hi!
I need to program arduino uno board using ldr and led, using Bluetooth electronics app to do following things:

  • have one button to decide if led goes on manually with the app or automatic with ldr - when is dark led goes on
  • if choose manually I should have another button to choose it on or off
  • if choose automatic the led state should depend on light around

I have a code in the beginning but not sure how to manage the on and off buttons? Tried some while loops or if statements but I guess not the best as without result.

many thanks.

Post your latest code what you have tried.

You should post code by using code-tags
There is an automatic function for doing this in the Arduino-IDE
just three steps

  1. press Ctrl-T for autoformatting your code
  2. do a rightclick with the mouse and choose "copy for forum"
  3. paste clipboard into write-window of a posting

and add a specific question

best regards Stefan

1 Like

This is the last thing I've tried and I gave up



int whiteLED = 10; //white led on pin number 10
int LDR = 0; //analog input 0 for LDR

int threshold = 270; //our threshold of light
char BLData; //data coming from the bluetooth

void setup() {
  pinMode(whiteLED, OUTPUT); //initialise LED as output
  pinMode(LDR, INPUT); //initialise LDR as input
  Serial.begin(9600); //serial communication on baud 9600
}

void loop() {
  int ldr_value = analogRead(LDR); //reads value from photoresistor
  Serial.available();
  Serial.println(ldr_value); //prints our ldr value - light level to the monitor on android phone
  BLData = Serial.read();
  if (BLData == 'Y') //if button number 2 says y
    digitalWrite(whiteLED, HIGH);
  if (BLData == 'N') //if button number 2 says n
    digitalWrite(whiteLED, LOW);
  if (BLData == 'A') { //if automatic do not use BLData and use automatic ldr function
    if (ldr_value < threshold)
      digitalWrite(whiteLED, HIGH);
    if (ldr_value > threshold)
      digitalWrite(whiteLED, LOW);

  }

so I am trying to use two buttons on the Bluetooth app

  • one is for on manual mode( with letters 'A' and 'M')
  • second button should on and off led (letters 'Y' or 'N') if first button says M
  • if first button says A it should just take data from LDR and on and off LED depending on the light

Really appreciate help.
Many thanks!!

OK. Very good that you posted your code. With this information I can give hints to move forward in steps:

Next step is to post:

  • what is the exact type of microcontroller that you are using?
  • what is the type of bluetooth-receiver that you are using?

The actual code-version uses the serial-connection over the USB-cable.
There is nothing about really making use of bluetooth in your code yet.

And what you have to add inside your code to make it work depends on the answers to the questions I wrote above.

best regards Stefan

1 Like

It is

  • ATmega328
  • bluetooth HC-05 zs-04

I never knew this will be important when programming, thank you for that.

Hi myilaaa,
you answered my question very exactly: I asked for the microcontrollertype and you answered
ATmega328

My question was unprecise. Of course the microcontroller-type is an important information.
Additionally the microcontroller-board is important too.
Are you using an Arduino Uno, or a selfmade PCB with just a pure ATmega328-Chip on it?
Or something else?

Best thing is to post a link to where you have bought the µC.

Then I was quoogling for "Arduino bluetooth HC-05 zs-04" expecting that google will come up with a lot of well suited tutorials. Nope. A lot of videos but I don't like videos because you can't cross-read-them in one minute.

Most of the non-video-tutorials are from bad or medium quality.

Finally I found a tutorial which seems to be quite good:

I choose this one because it uses a second serial-connection for the blue-tooth-modul which keeps the feature of beeing able to send debug-output to the serial monitor.

Most other tutorials use IO-pins 0 and 1 which occupies the serial-port that is used for programming the arduino. This would mean for each and every new version of your software
bad tutorials do it this way:

  • unplug bluetooth module
  • plug to computer
  • choose comport in the Arduino-IDE
  • upload software
  • unplug computer
  • re-plug bluetooth module

With a second serial connection like shown in the chosen tutorial everything can stay conected all the time.
I'm very sure that you will have additional questions just post them here always including the complete related code-version by using this method

You should post code by using code-tags
There is an automatic function for doing this in the Arduino-IDE
just three steps

  1. press Ctrl-T for autoformatting your code
  2. do a rightclick with the mouse and choose "copy for forum"
  3. paste clipboard into write-window of a posting

So take a look at the linked tutorial and try it.
best regards Stefan

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.