I've attempted to change the value of the global variable via a physical switch but no success. Is this possible? Or is there a more efficient way ??? Thanks in advance.
int batt = 3;
int firstswitch = 7;
void setup() {
if ( digitalRead(firstswitch) == LOW)
{
batt = 3.5;
}
}
void loop() {
// put your main code here, to run repeatedly:
}
int batt = 3;
int firstswitch = 7;
void setup() {
if ( digitalRead(firstswitch) == LOW)
{
batt = 4;
}
}
void loop() {
// put your main code here, to run repeatedly:
}
Looks fine to me though you'll need the switch to be connected correctly with a pull-up resistor, since you haven't used pinMode INPUT_PULLUP. What happens when you try it?
ArdUser14:
Will the two variables conflict when the switch is flipped or does the new definition override the original global variable?
Because it is defined globally there is only one VARIABLE batt. What you have done is changed the VALUE of that variable. Anything that uses it after that change sees the new value.