So I’m basically just in the beginning stages of the project I’m trying to build. It’s going to be an ignition switch for a vehicle but right now I’m just using a segmented switch to turn on lights on my breadboard (if I can send voltage to lights I can send voltage to various items in my car). It’s a BLE setup. I want to use the Blynk app to cycle through the 4 options (which works fine), but I also want to incorporate a push button. For an example, when I’m in the Off position and the button is pressed it will switch the button state to the On position. Then once the button is pressed again it will go to the Run position and so on.
As of now, I half way have it working. I’ve been trying a couple different methods but my latest revision (the one in this post) will do one switch. If I’m in the Off position it will switch it to the On position, but then another button press doesn’t do anything. I can’t continue pressing the button to get to the next states. I don’t think I’m writing to the virtual pin properly or I’m not looping things correctly. I’m still a little green to all this coding. I’ve been learning by reading forum.
Also, when I am able to get the one switch for like Off to On the segmented switch changes in the Blynk app from Off to On, but the light I have set up doesn’t change it stays in the off position on the bread board
I hope this made sense. Any ideas?
#define BLYNK_PRINT Serial
#include <SoftwareSerial.h>
SoftwareSerial SerialBLE(10, 11); // RX, TX
#include <BlynkSimpleSerialBLE.h>
BlynkTimer timer;
void checkPhysicalButton();
// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "iLrMxefNzOW0F0cx3KjX_ShR_6Tm8nmE";
const int offMode = 4;
const int onMode = 5;
const int runMode = 6;
const int startMode = 7;
const int buttonPin = 2;
int buttonState = 0;
int value;
BLYNK_WRITE(V1) {
value = param.asInt();
switch (param.asInt())
{
case 1: // Item 1
Serial.println("Off selected");
digitalWrite(offMode, HIGH);
digitalWrite(onMode, LOW);
digitalWrite(runMode, LOW);
digitalWrite(startMode, LOW);
break;
case 2: // Item 2
Serial.println("On selected");
digitalWrite(offMode, LOW);
digitalWrite(onMode, HIGH);
digitalWrite(runMode, LOW);
digitalWrite(startMode, LOW);
break;
case 3: // Item 3
Serial.println("Run selected");
digitalWrite(offMode, LOW);
digitalWrite(onMode, LOW);
digitalWrite(runMode, HIGH);
digitalWrite(startMode, LOW);
break;
case 4: // Item 4
Serial.println("Start selected");
digitalWrite(offMode, LOW);
digitalWrite(onMode, LOW);
digitalWrite(runMode, LOW);
digitalWrite(startMode, HIGH);
break;
default:
Serial.println("Unknown item selected");
}
}
void checkPhysicalButton(){
buttonState = digitalRead(buttonPin);
if (value == 1 && (buttonState == HIGH)){
Blynk.virtualWrite(value,2);}
if (value == 2 && (buttonState == HIGH)){
Blynk.virtualWrite(value,3);}
if (value == 3 && (buttonState == HIGH)){
Blynk.virtualWrite(value,4);}
Blynk.syncVirtual(value);
}
void setup()
{
// Debug console
Serial.begin(9600);
SerialBLE.begin(9600);
Blynk.begin(SerialBLE, auth);
Serial.println("Waiting for connections...");
pinMode(offMode, OUTPUT);
pinMode(onMode, OUTPUT);
pinMode(runMode, OUTPUT);
pinMode(startMode, OUTPUT);
pinMode(buttonPin, INPUT);
timer.setInterval(1000L, checkPhysicalButton); //timer will run every sec
}
void loop()
{
Blynk.run();
timer.run(); // run timer every second
}