Toggle switch problem. Please help

Hi. I was wondering maybe someone could help me...
I'm trying to adjust a toggle switch sketch that will work with a button from a phone app (Bluetooth Electronics app)
After upload it shows only one state (1) and i need it to switch and maintain the current state until the button is pressed again. The button sends "Y"/"y" when pressed.
Here is my sketch:

int ledPin = 13;  // the number of the pushbutton pin
char reading;
char lastreading;
int state = 0; // variable for first status

 

 void setup() {
pinMode (ledPin,OUTPUT);  // initialize the LED pin as an output:
Serial.begin(9600);}

 
void loop(){

  if (Serial.available()){
    reading = Serial.read();
    Serial.println(reading);
     if (reading != lastreading)

    {

        lastreading = reading;

        if (lastreading == "Y"){state = !state;}

            if (lastreading =="y"){state = 0;}

    }

    if (state){

        Serial.println(state);

        //auto_mode();

        Serial.println("Auto Mode");}

        if (!state){

           Serial.println(!state);

           //manual_mode();

           Serial.println("Manual Mode"); }

  }

}

(Code tags added by Moderator)

What Arduino board? Is there a Bluetooth module connected to the mystery Arduino? If so, which one and how is the module connected?

So the button is part of the Bluetooth Electronics app? What value is sent when the button is pressed and not pressed?

It's an Arduino Uno board.
HC-5 Bluetooth module, connected via RX/TX pins
The button is one of the buttons provided by the Bluetooth Electronics app. It sends "Y" when pressed and "y" when released.

First, I recommend that you put the HC05 on a software serial port and save the hardware serial port (USB pins 0 and 1) for program upload and program output and debug. Having 2 things (HC05 and serial monitor) connected to one port often causes problems.

Your variables, reading and lastreading, are a char data type.

"Y" is a string data type. 'Y' is a char data type.

I modified your code to use SoftwareSerial and fixed the data type problems. Now when you press and hold the button in Bluetooth Electronic the serial monitor displays Auto Mode ans when you release the button you get Manual Mode. Is that what you want?

#include <SoftwareSerial.h>

SoftwareSerial bt(4,7); // pin 4 (RX) to HC05 TX, 
                        // pin 7 (TX) to HC05 RX through voltage divider
                        
int ledPin = 13; // the number of the pushbutton pin
char reading;
char lastreading;
int state = 0; // variable for first status

void setup()
{
   pinMode (ledPin, OUTPUT); // initialize the LED pin as an output:
   Serial.begin(9600);
   bt.begin(9600);  // HC05 default baud rate
}

void loop()
{
   if (bt.available())
   {
      reading = bt.read();
      Serial.println(reading);
      if (reading != lastreading)
      {
         lastreading = reading;
         if (reading == 'Y')
         {
            state = !state;
         }

         if (lastreading == 'y')
         {
            state = 0;
         }
      }

      if (state)
      {
         Serial.println(state);
         //auto_mode();
         Serial.println("Auto Mode");
      }

      if (!state)
      {
         Serial.println(!state);
         //manual_mode();
         Serial.println("Manual Mode");
      }
   }
}

Thank you for your effort!...However I ned the button to , for example turn a light ON when you press it, and turn it Off when you press it again...
About the Bluetooth module... I am a complete "0" in this matter...:slight_smile: ..and i have no clue about other kinds of connecting it.
I'm trying to improvise based on steps given at a lecture...

How about this. This will toggle the modes on each button (in the app) press. Tested on my Uno, HC05 module and Bluetooth Electronics app on my Android tablet.

#include <SoftwareSerial.h>

SoftwareSerial bt(4, 7); // pin 4 (RX) to HC05 TX,
// pin 7 (TX) to HC05 RX through voltage divider

int ledPin = 13; // the number of the pushbutton pin
char reading;
char lastreading;
bool state = 0; // variable for first status

void setup()
{
   pinMode (ledPin, OUTPUT); // initialize the LED pin as an output:
   Serial.begin(9600);
   bt.begin(9600);  // HC05 default baud rate
}

void loop()
{
   if (bt.available())
   {
      reading = bt.read();
      //Serial.println(reading);
      if (reading != lastreading)
      {
         if (reading == 'Y')
         {
            state = !state;

            if (state)
            {
               //Serial.println(state);
               //auto_mode();
               Serial.println("Auto Mode");
            }

            else
            {
               //Serial.println(!state);
               //manual_mode();
               Serial.println("Manual Mode");
            }
         }
         lastreading = reading;
      }
   }
}

@groundFungus , just checking if I got you write....When connecting HC-5 Bt module ...
I should connect
5v on Arduino to 5v on the module,
GND on Arduino to GND on the module,
pin4(RX) on Arduino to TX on the module,
pin7(TX) on Arduino to RX via a 1K_|_2K voltage divider?
..And al data that i want to sent to the app should go trough "bt.print();?

  static byte butLst = HIGH;

  btyte but = digitalRead(buttonPin);

  if (buLst != but)  {
      butLst = but;

      if (LOW == but)  {
        digitalWrite (ledPin, ! digitalRead (ledPin));
      }
      delay (10);     // debounce
  }

Yes, that is right. You read the data from the app with bt.read();. You would use bt.print(Serial.read()); to send from serial monitor to the app, for instance. What is it that you would send? The SoftwareSerial reference.

HC05 baud rate should be 9600 (default is 9600).

@groundFungus , hi! thank you for your help... means a lot...as it is my firs project
The sketch that you've corrected works great.
It is a part of a project. I suppose to write a program that will read data from an LDR and a thermistor( don't know the actual types as it came in a kit, the most basic ones I suppose).
The sketch should contain two main functions:

  1. Automatic Dawn to Dusk Light with manual override.
  2. Room temperature controller with manual override and high temperature alarm.
    The sketch should work out data and display it in the app:
    LDR ->light levels in Lux
    Thermistor -> temperature in Centigrade
    It should work in auto mode but allow switching it to manual mode
    I've created functions for every operation and separately each works fine, but all together...
    Al functions send data via serial ports ...
    sprintf(Str, "D%d\n", lux);
    Serial.write(Str);
    Now that I've "piled" up all in one prgram..it doesn't work again....phhh... NUB, what can i say...
    Here is the sketch:

#include <math.h>

#include <SoftwareSerial.h>

//Variables:

int whiteLED = 13;

int blueLED = 7;

int greenLED = 6;

int redLED = 5;

int buzzer = 4;

int thermistor_Data = A1;

int LDR_Data = A0;

char BLData; //Variable to receive data

char Str[20];//Variable to send data

SoftwareSerial bt(9, 8); // pin 9 (RX) to HC05 TX,

// pin 8 (TX) to HC05 RX through voltage divider

//LDR functions

double light_LUX();

void light_manual();

int light_auto();

//Thermistor functions

float tempC();

int tempC_auto();

void tempC_manual();

void setup(){

pinMode(whiteLED, OUTPUT);// pin 13 as output

pinMode(blueLED, OUTPUT);// pin 7 as output

pinMode(greenLED, OUTPUT);// pin 6 as output

pinMode(redLED, OUTPUT);// pin 5 as an output

pinMode(buzzer, OUTPUT);//pin 4 as an output

pinMode(thermistor_Data, INPUT);

pinMode(LDR_Data, INPUT);

Serial.begin(9600);//Start serial communication

Serial.println("beginning...");

bt.begin(9600); // HC05 serial communication default baud rate

}

void loop(){

int lux = light_LUX();

int temp = tempC();

char reading;

char lastreading;

bool state = 0; // variable for first status

sprintf(Str, "D%d\n", lux); //the block that displays light measurement and temperature

Serial.write(Str); //inthe app

sprintf(Str, "G%d\n", temp);

Serial.write(Str);

delay(1000);

if (bt.available())//the block that allow to toggle modes auto/manual

{

  if (reading != lastreading)

  {

     if (lastreading == 'Y')

     {

        state = !state;

        if (state)

        {

           

           tempC_manual();

           light_manual();

          // bt.print("Auto Mode "); bt.println(state);

        }

        else

        {

           

           tempC_auto();

           light_auto();

          // bt.print("Manual Mode "); bt.println(!state);

        }

     }

     lastreading = reading;

  }

}

}

please post code using the </> icone

    char reading;
    char lastreading;
    bool state = 0; // variable for first status
    sprintf(Str, "D%d\n", lux); //the block that displays light measurement and temperature
    Serial.write(Str); //inthe app
    sprintf(Str, "G%d\n", temp);
    Serial.write(Str);
    delay(1000);
    if (bt.available())//the block that allow to toggle modes auto/manual
    {
        if (reading != lastreading)
        {
            if (lastreading == 'Y')

in the code above, where does "reading" get set? and why check "lastreading" instead of "reading"?

@gcjr , sorry..as I said, complete NUB! ... First program, first post.....

   #include <math.h>
   #include <SoftwareSerial.h>

   //Variables:
int whiteLED = 13;
int blueLED = 7;
int greenLED = 6;
int redLED = 5;
int buzzer = 4;
int thermistor_Data = A1;
int LDR_Data = A0;

char BLData; //Variable to receive data
char Str[20];//Variable to send data

SoftwareSerial bt(9, 8); // pin 9 (RX) to HC05 TX,
// pin 8 (TX) to HC05 RX through voltage divider


   
   
//LDR functions
double light_LUX();
void light_manual();
int light_auto();

//Thermistor functions
float tempC();
int tempC_auto();
void tempC_manual();



void setup(){
	pinMode(whiteLED, OUTPUT);// pin 13 as output
	pinMode(blueLED, OUTPUT);// pin 7 as output
	pinMode(greenLED, OUTPUT);// pin 6 as output
	pinMode(redLED, OUTPUT);// pin 5 as an output
	pinMode(buzzer, OUTPUT);//pin 4 as an output
	pinMode(thermistor_Data, INPUT);
	pinMode(LDR_Data, INPUT);
	//Serial.begin(9600);//Start serial communication
  //Serial.println("beginning...");
  bt.begin(9600);  // HC05 serial communication default baud rate
}

void loop(){
  int lux = light_LUX();
  int temp = tempC();
  char reading;
  char lastreading;
  bool state = 0; // variable for first status

  sprintf(Str, "*D%d*\n", lux); //the block that displays light measurement and temperature
  bt.write(Str);            //inthe app
  sprintf(Str, "*G%d*\n", temp);
  bt.write(Str);
  delay(1000);
 
  if (bt.available())//the block that allow to toggle modes auto/manual
   {
      if (reading != lastreading)
      {
         if (lastreading == 'Y')
         {
            state = !state;

            if (state)
            {
               
               tempC_manual();
               light_manual();
               sprintf(Str, "*MManual*\n");
               bt.write(Str);           
            }

            else
            {
               
               tempC_auto();
               light_auto();
               sprintf(Str, "*MAuto*\n");
               bt.write(Str); 
            }
         }
         lastreading = reading;
      }
   }
}

   


 




  
  
   
 
	

same issue?

In your first code you had this which sets the value 'reading'. In your latest code you have

The bt equivalent of Serial.read() has gone missing.

Steve

HI. Sorry, this code is like living on its own....
changing it continuously, ...no luck dough.
But I will definitely scream "It's alive!...("Frankenstein") when it get it write.