Hi all!
i want to use 2 functions for a momentary button, turn on/off my bicycle headlight with a long click, and change headlight mode with short clicks.
The goal is to store the mode and when it turns on, directly go to selected mode, instead of having to click thru lowest brightness -> -> -> highest brightness.
So the hardware for this part of my project has 2 outputs and one input.
Outputs are MCP23007 pin 2 (headlight power) and mcp pin 3 (mode select)
input is a simple momentary switch connected to ground.
Headlight power will be a transistor that acts as a switch between headlight and battery while
mode select pin is controlling a transistor thats soldered in place of the mode select button of said headlight.
The headlight button cycles from off to low output and then 1 or 2 medium steps, then to high and then back to off.
To simplify things i figure i cut power for about 50ms to reset the headlight mode, then trigger the button transistor with the "mode" number of flashes.
I have barely a clue what im doing when coding, so please dont spank me..
and this is the part im fiddling with, so it does not have all the defining bits and bobs..
But as im reading this incomprehensible jungle of words, the thought strikes me, is there a easier way to do this?
..my code is not quite working just yet, i do get incremental mode feedback (that keeps repeating while the button is pressed) from serial monitor, but there´s not much from the troubleshooting LEDs connected to the output pins.
Also, i seem to be missing the read button_up so to speak, to get correct button down-time.
After 14+ hours of non-stop coding its definitely time for some shuteye and hope for some wize replies in the morning..
So if there is a easier way to do this, then i wont have to try to get this house of cards working..
//headlight high beam code, WITH mode select memory
HighBeamVal = digitalRead(ModeSwitch); // read modeswitch pin and store in highbeamval
if ((HighBeamVal == LOW) && (previousHighBeamVal == HIGH)){ // if button is pressed but was not, set timestamp and previousval
previousHighBeamVal = HighBeamVal; //sets the current status as the old status in preparation for the next check
HighBeamMillis = currentMillis; //sets a time stamp at the current time
}
if ((HighBeamVal == HIGH) && (previousHighBeamVal == LOW)){
HeadLightConfirm = 0;
}
if (currentMillis - HighBeamMillis < interval){ // if the button is released when it was pressed and if button press time is less than interval value, turn ON/OFF High Beam
if (HighBeam == 0){HighBeam = 1;} // turn on if off, else turn off.
else {HighBeam = 0;}
if (SerialTroubleshoot == 1){ // If Serial troubleshoot is active, report status.
Serial.print("High/Low beam mode switch ");
Serial.print(HighBeam);
Serial.println(" ");}
}
if ((HighBeamVal == LOW) && (currentMillis - HighBeamMillis > interval)){ // if button press time is more than interval value, select next High Beam mode.
if (HeadLightConfirm == 0){
HeadLightConfirm = 1;
HeadLightMode ++;
if ((SerialTroubleshoot == 1) && (HeadLightConfirm == 0)){ // If Serial troubleshoot is active, report status.
Serial.print("HeadLightMode increased, now mode ");
Serial.print(HeadLightMode);
Serial.println(" Selected");}
if (HeadLightMode >= 4){HeadLightMode = 0;}
}
if ((HighBeam == 1) && (LightStatus == 1)){ // and if highbeam active
mcp.digitalWrite(1, HIGH);
delay(wait);
mcp.digitalWrite(1, LOW);
for (int i=0; i <= HeadLightMode; i++){
mcp.digitalWrite(2, HIGH);
delay(wait);
mcp.digitalWrite(2, LOW);
delay(wait);}
}
}
if (HighBeam == 0){mcp.digitalWrite(1, LOW);}