0

So I stumbled into strange problem. I have this circuit, where I connect 2 photoresistors and bluetooth HC05 to Arduino. When one of the resistors senses bright light bluetooth sends '1' or '3' accordingly to another circuit. When dark is sensed, bluetooth sends '2'or '4'. When dark on both resistors is sensed for more than 1.5 second, then everything goes to while loop and bluetooth sends '1' and '3'. So for example, when left photoresistor senses bright light, and right darkness, then bluetooth sends '1' and '4'. At first for a couple of months everthing worked just perfectly fine and one day it just... stopped. While troubleshooting I found out, that when changing bright light/darkness mode on resistor, data, which is seen on com port, stops for a bit and goes in batches again, it does not flow continously. I changed photoresistors, everything is the same. I even changed Arduino, still nothing. Here is the code:

#include <SoftwareSerial.h>
SoftwareSerial BTserial(2, 3); // RX | TX
int fk = A0; // left photoresistor
int fd = A1; // right photoresistor
int a = 0;
int b = 0;
unsigned long dl; // time
const unsigned long p = 1500; //how many milisecs dark should be sensed

void setup()
{
    Serial.begin(9600);
    BTserial.begin(9600);
    pinMode(fk, INPUT);
    pinMode(fd, INPUT);
    dl = millis();  
}

void loop()
{
    a = analogRead (fk);
    Serial.println (a);
    b = analogRead (fd);
    Serial.println (b);
    if(a>1000)
    {
    BTserial.write('1'); // left photo senses light
    dl = millis(); // capturing last time light was sensed
    delay(10);
    }
    else
    {
    BTserial.write('2'); // left photo senses dark
    delay(10);
    }
    if (b>1000)
    {  
    BTserial.write('3'); // right photo senses light
    dl = millis();
    delay(10);  
    }
    else
    {
    BTserial.write('4'); // right photo senses dark
    delay(10);
    }
    while (a<1000 && b<1000)
    {
        if ((millis() - dl) >= p) //if dark is being sensed more than 1.5 sec
        {
        BTserial.write('1'); 
        BTserial.write('3');
    a = analogRead (fk);
    Serial.println (a);
    b = analogRead (fd);
    Serial.println (b);
    }
}
    }

And when I use only this batch of code without while loop

    a = analogRead (fk);
    Serial.println (a);
    b = analogRead (fd);
    Serial.println (b);
    if(a>1000)
    {
    BTserial.write('1'); // left photo senses light
    dl = millis(); // capturing last time light was sensed
    delay(10);
    }
    else
    {
    BTserial.write('2'); // left photo senses dark
    delay(10);
    }
    if (b>1000)
    {  
    BTserial.write('3'); // right photo senses light
    dl = millis();
    delay(10);  
    }
    else
    {
    BTserial.write('4'); // right photo senses dark
    delay(10);
    }

Everything works fine, data is flowing continously, even when light states are changing fast. But also everything works fine, when I use only while loop standalone without previous batch. So in my opinion bad things happen in transitions between these two batches of code? But I completely cannot understand what is wrong, why it suddenly stopped working, maybe time is calculated wrongly? I realized that when I soldered bluetooth to another board, but bluetooth is not the case, as I tried the same code with bluetooth data sending part removed. Data still goes with strange batches. I am completely lost.

  • your code is badly formatted ... the indentation of the code needs to be corrected .... without correct indentation, it is difficult to follow the program flow – jsotola Mar 15 at 23:19
  • the while loop is unnecessary ... change your thinking a bit .... your code checks each sensor and remembers the last time it sensed light .... all you have to do, is to remember the last time dark was sensed .... then simply determine the time interval between light and dark (or between light and now) – jsotola Mar 15 at 23:33
  • I do not understand why you need this 1.5 sec rule. If both photo resistors indicate darkness BT already sends 1 and 3 . So what is different if the darkness lasts longer than 1.5 seconds. But, there is one small difference that could explain your observations. In the if/else blocks you use a delay(10); Perhaps your serial connection/Bluethooth is not quick enough to handle the speed up. 9600 baud is not very fast. ;-) – Peter Paul Kiefer Mar 16 at 13:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.