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.
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
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
press Ctrl-T for autoformatting your code
do a rightclick with the mouse and choose "copy for forum"
paste clipboard into write-window of a posting
So take a look at the linked tutorial and try it.
best regards Stefan