How do i change back to control with slider after using code to control the same pin - Arduino

Hello,

I’m working on my exam project using an arduino + wifi-shield with Blynk, and have a few problems with the coding.

So i have a circuit that can regulate a 12V LED with a transistor, sending a PWM wave into the gate/base pin by using a slider in blynk, and it works as intended. But at the same time, im trying to implement a button in blynk, that sends a HIGH/LOW signal right into an analog pin. When the analog pin then reads a high signal input, it will instead run a code that automaticly regulates the LED with data it gets from some sensors. The code itself works, also when changing from getting to control the PWM signal from blynk to the automatic code, but then getting back from that, so it turns that off and you again can control from blynk, I don’t exactly know hot to.

Thanks beforehand! :slight_smile:

EDIT:
The problem is not how to make it run the different codes when its HIGH/LOW, but rather what to put in the “do something” part in the if () statement, for the one when it’s LOW (button off) and the slider should be the one controlling the PWM signal, where it had been running a code to change the PWM signal in the previous state, where it was HIGH (button on).

Well one thing you could do is using this code:

int digitalpin = x;

pinMode(digitalpin);
Serial.print(“what”);
digitalRead(digitalpin);
Serial.print(“if”);
if(digitalRead(digitalpin) == 1){
do something
}
Serial.print(“i”);
Serial.print(“didnt”)
;Serial.println(“sucked”);
if(digitalRead(digitalpin) == 0){
do something else
}

remember to open serial monitor to see your output

The problem is not how to make it run the different codes when its HIGH/LOW, but rather what to put in the “do something” part in the if () statement, for the one when it’s LOW (button off) and the slider should be the one controlling the PWM signal, where it had been running a code to change the PWM signal in the previous state, where it was HIGH (button on).

You could give it a state variable which says it should be controlled by high/low or PWM. The switch() statement would be my choice to implement this. https://www.arduino.cc/en/Reference/SwitchCase

It’s really easy to accomplish several modes of turning on/off lighting. I use it myself too for my home lights.