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();
}
}