Issue With Mode Selection By Tactile Button

Hello everyone,

I'm currently trying to test some concepts while waiting for my MOSFETs to come in. As well, I'm new to coding for hardware. This will be part of a bigger idea in the end and this is currently just some scratch so disregard the disorganization, my apologizes.

At the moment I am simply trying to switch between two functions in my code (modes) with a tactile switch. When the switch is open I have pull down 10K ohm resistor on pin two and when closed is shorted to 5v. Pin one is an on board led as I am using a digikey.

I have confirmed that both functions work correctly by setting the default (modeState) to either 0 or 1. My problem is that no matter what my simple mind can see wrong in the code I cannot get the (modeChanger) function to work. I.E. no response what so ever from my button. I have done plenty of forum surfing and tinkering with the code but I think if I spend even five more minutes in here my lady is going to kill me.

If anyone sees what mistake I have made help would be much appreciated!

const int ledPin = 1;
const int buttonPin = 2;

void setup() {
  pinMode(ledPin, OUTPUT);
  pinMode(buttonPin, INPUT);
}

int buttonState = 0;
int lastButtonState = 0;
int modeState = 0;

void modeChanger() {
  buttonState = digitalRead(buttonPin);
  if (lastButtonState != buttonState && buttonState == HIGH) {
    modeState + 1;
  }
  buttonState = lastButtonState;
  if (modeState > 2) {
    modeState = 0;
  }
}

unsigned long previousMillis = 0;
unsigned long currentMillis = 0;
int flasherState = LOW;

void ledFlasher() {
  if (currentMillis >= previousMillis + 1000) {
    previousMillis = currentMillis;
    if (flasherState == LOW) {
      flasherState = HIGH;
    } else {
      flasherState = LOW;
  }
  digitalWrite(ledPin, flasherState);
  }
}

int ledBrightness = 30;
int fadeAmount = 5;

void ledFade() {
  if (currentMillis >= previousMillis + 30) {
    previousMillis = currentMillis;
    analogWrite(ledPin, ledBrightness);
    ledBrightness = ledBrightness + fadeAmount;
    if (ledBrightness <= 30 || ledBrightness >= 255) {
      fadeAmount = -fadeAmount;
    }
  }
}

void loop() {
  currentMillis = millis();
  modeChanger();
  if (modeState == 0) {
    ledFlasher();
  }
  if (modeState == 1) {
    ledFade();
  }
}
    modeState + 1;

That basically does nothing. You need to assign the result to a variable, e.g. modeState. Below three ways to achieve what you want; pick one :wink:

modeState = modeState +1;
modeState += 1;
modeState++;

Next your modeState can have 3 values, 0 or 1 or 2. But you only test for 2 of them (0 and 1).

Next, pin 1 on most (or all?) Arduinos does not support analogWrite. Which board are you using?

Lastly, pin 1 on most (or all?) Arduinos is used for serial communication and provides means for debugging via e.g. the Serial Monitor. It's advisable only to use that if you really run out of pins.

buttonState = lastButtonState;

Should be reversed:

lastButtonState = buttonState;

Put it inside code block following the buttonstate change detection

How IS the switch wired? Why? Using the internal pullup resistor makes for much simpler wiring, without the need for an external resistor.

sterretje:

    modeState + 1;

That basically does nothing. You need to assign the result to a variable, e.g. modeState. Below three ways to achieve what you want; pick one :wink:

modeState = modeState +1;

modeState += 1;
modeState++;




Next your modeState can have 3 values, 0 or 1 or 2. But you only test for 2 of them (0 and 1).

Next, pin 1 on most (or all?) Arduinos does not support analogWrite. Which board are you using?

Lastly, pin 1 on most (or all?) Arduinos is used for serial communication and provides means for debugging via e.g. the Serial Monitor. It's advisable only to use that if you really run out of pins.

Thank you! This definitely fixed my problem! I figured it was something simple such as that.

As for modeState having 3 values it was more of a testing thing, essentially an off. It wasn't that way until I stated having issues. I was trying to see if it could have been a bounce issue so I threw in another "mode" to try and eliminate that variable.

For the pins being odd, I am using a DigiSpark with the attiny85 chip. Only 6 I/O's, pins 3 and 4 are for serial communication. As well pin 1 is the on board LED. I thought I said this in my original post although I did not, I said DigiKey, can thank the ADD and beer combo for that.

Dan95:
buttonState = lastButtonState;

Should be reversed:

lastButtonState = buttonState;

Put it inside code block following the buttonstate change detection

Also, thank you for this! This was also incorrect and something I missed.

PaulS:
How IS the switch wired? Why? Using the internal pullup resistor makes for much simpler wiring, without the need for an external resistor.

I was using an external pulldown resistor. Although, I saw that this could be done on another forum while doing some digging last night, I'll need to look more in to doing this. As I said I haven't been working with these for very long. Thank you for the idea.

Although, I saw that this could be done on another forum while doing some digging last night, I'll need to look more in to doing this.

Set the mode of the pin to INPUT_PULLUP. Connect one leg of the switch to ground. Connect the other leg to the digital pin. The whole process could not be simpler to explain, draw, or implement.

Well, that is quite simple. Thank you. Can pins be internally pulled down as well?

That depends on the microcontroller; some seem to have that functionality. But with a 328 based board definitely not.

Time to read the datasheet of the microcontroller :wink: