#define ButtonRight 2
#define ButtonLeft 0
#define potentiometer 4
#define DC_Motor 16
#define LED 5
int currentPWM;
int deviceState = 1;
int LED_pwm;
int DC_Motor_pwm;
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
Serial.println("Hello, ESP32!");
pinMode(ButtonRight, INPUT_PULLUP);
pinMode(ButtonLeft, INPUT_PULLUP);
pinMode(potentiometer, INPUT);
pinMode(DC_Motor, OUTPUT);
pinMode(LED, OUTPUT);
}
void loop() {
//Read Potentiometer
currentPWM = analogRead(potentiometer);
currentPWM = map(currentPWM, 0,1023,0,255);
//Which device is controlled? last pressed
//if button 1, control DC Motor
if(digitalRead(ButtonRight) == LOW){
deviceState = 2;
}
//if button 2, Control LED
if(digitalRead(ButtonLeft) == LOW){
deviceState = 1;
}
//save current pwm for specific device
if(deviceState == 1){
LED_pwm = currentPWM;
}
if(deviceState == 2){
DC_Motor_pwm = currentPWM;
}
analogWrite(LED, LED_pwm);
analogWrite(DC_Motor, DC_Motor_pwm);
delay(1000);
//a debugging feature.
Serial.println("device : pwm ");
String toPrint;
toPrint += deviceState;
toPrint += " : ";
toPrint += DC_Motor_pwm;
Serial.println(toPrint);
delay(10); // this speeds up the simulation
}