hi I'm making a combination of useless box and shy box, so there's two modes and to change between the two modes, I want to use a switch button. How to write the code?
In setup, declare a pin as pinMode(x, INPUT_PULLUP);
In loop, check the pin and respond accordingly
if(digitalRead(x)
mode(1);
else
mode2();
where can I put the code?
Hello
You can use the Arduino IDE for example.
Preferably, inside the Arduino
If there are only going to be two modes, and the code is fairly basic then this kind of setup could work well:
(Note - as in the Wokwi, only two of the switch poles are used, an outer pole to ground and the middle pole to a digital pin).
#define SWITCH_PIN 13
void setup() {
pinMode(SWITCH_PIN, INPUT_PULLUP);
Serial.begin(115200);
}
void loop() {
digitalRead(SWITCH_PIN) ? doModeOneStuff() : doModeTwoStuff();
}
void doModeOneStuff() {
Serial.println("Doing Mode One Stuff");
}
void doModeTwoStuff() {
Serial.println("Doing Mode Two Stuff");
}
Of course you might need to swap the order around inside the digitalRead(SWITCH_PIN) ? doModeOneStuff() : doModeTwoStuff();
line. And all of the Serial
lines can be removed when you are finished debugging with them.
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.