Hi,
Super qik intro, 51yo Aussie guy, mechanic by trade, dabbled with electronics as a kid and thru life as needed. Into Arduino and ESP for 6 or so yrs on a casual basis, try and nag the kids about it. Lately had time to get some things done I wanted to for ages so 3d printer coming and ramped back up on eagle pcb etc (as best I can LOL).
Thought I had an account from years ago but couldn't find it so new profile today.
Also hope the code attachment goes ok and I'm in the right section
Trying to do what should be simple as part of another project.
OK
I have purely for space reasons a wemos D1 mini in basic Arduino mode, no wifi, AP etc enabled.
Using a LED capacitance switch from heltec.cn and have added a smd 1K resistor to make it momentary as per data sheet.
Hooked up simply as follows,
vcc to 5v(onboard wemos), Gnd to Gnd on Wemos, OUT to pin D1 on wemos
Power via usb.
That's the hardware side.
What should it do?
Touch switch about 100 = run option 1 (ignite pin D4, ie, onboard led), count 5secs till turn off, turn off
Touch switch for 2 secs and runs option 2, turns on D4 led, counts time,after 10secs turns off.
What does it do?
Sorta works ok,
Touch and release and D4 glows up but wont turn off, for option one or two.
I had delays and it was affecting on off times so nested another mills loop in the basic code by Scuba Steve. Tried to follow syntax from existing code. Things compile and work but now wont shut off and despite trying to look at it logically I am not experienced enough in coding to see where I went wrong.
Am I stuck in a loop?
Left plenty of comments in the code to seperate the bits, hope that's not an issue folks??
/*Using a Single Button, create mutliple options based on how long the button is pressed
I am modifying this code, original coders listed below.
Using a Wemos D1 mini in basic Arduino mode, no funky wifi
Also apologise in advance for my butchery
Created DEC 2014 by Scuba Steve
Modified JAN 2015 by Michael James
Both members of https://programmingelectronics.com
This code is in the public domain
*/
/////////Declare and Initialize Variables////////////////////////////
//We need to track how long the momentary pushbutton is held in order to execute different commands
//This value will be recorded in seconds
float pressLength_milliSeconds = 0;
// Define the *minimum* length of time, in milli-seconds, that the button must be pressed for a particular option to occur
int optionOne_milliSeconds = 100;
int optionTwo_milliSeconds = 2000;
//This MAY? count ON time
float ontimeLength_milliSeconds = 0;
//Define time it will run ON for each option
int runoptionOne_milliSeconds = 5000;
int runoptionTwo_milliSeconds = 10000;
//The Pin your button is attached to
int buttonPin = D1;
//Pin your LEDs are attached to
int ledPin_Option_1 = D4;
int ledPin_Option_2 = D4;
void setup() {
// Initialize the pushbutton pin as an input
// Here I have a capacitance led switch 3 wire vvc, gnd, out(D1)
// set in momentary mode,
// https://www.aliexpress.com/item/1-pc-Green-Color-HTTM-Series-Capacitive-Touch-Switch-Button-Module-New-Arrived/32775714370.html?spm=2114.10010108.1000013.4.7d5356d4oyrMWT&traffic_analysisId=recommend_2088_2_90158_iswistore&scm=1007.13339.90158.0&pvid=8434f947-bb6c-479d-8d91-dda1049dbcba&tpp=1
pinMode(buttonPin, INPUT);
digitalWrite(buttonPin, LOW);
//set the LEDs pins as outputs
pinMode(ledPin_Option_1, OUTPUT);
pinMode(ledPin_Option_2, OUTPUT);
// D4 is the ledPin but I thought this may stop a dbl instance of it defaulting to LOW on startup if it does want to
digitalWrite(D4, HIGH);
//Start serial communication - for debugging purposes only
Serial.begin(9600);
} // close setup
void loop() {
//Record *roughly* the tenths of seconds the button in being held down
while (digitalRead(buttonPin) == HIGH ) {
delay(100); //if you want more resolution, lower this number
pressLength_milliSeconds = pressLength_milliSeconds + 100;
//display how long button is has been held
Serial.print("ms = ");
Serial.println(pressLength_milliSeconds);
}//close while
//Different if-else conditions are triggered based on the length of the button press
//Start with the longest time option first
//Option 2 - Execute the second option if the button is held for the correct amount of time
if (pressLength_milliSeconds >= optionTwo_milliSeconds) {
digitalWrite(ledPin_Option_2, LOW);
//ONtime timer check for runoptiontwo
ontimeLength_milliSeconds = ontimeLength_milliSeconds + 100;
// Has it been on for the time needed in this option?
if (ontimeLength_milliSeconds >= runoptionTwo_milliSeconds) {
// Hopefully turn off after time dictated in runoptionTwo
digitalWrite(ledPin_Option_2, HIGH);
}
}
//option 1 - Execute the first option if the button is held for the correct amount of time
else if (pressLength_milliSeconds >= optionOne_milliSeconds) {
digitalWrite(ledPin_Option_1, LOW);
//ONtime timer check for runoptiontwo
ontimeLength_milliSeconds = ontimeLength_milliSeconds + 100;
// Has it been on for the time needed in this option?
if (ontimeLength_milliSeconds >= runoptionOne_milliSeconds) {
// Hopefully turn off after time dictated in runoptionOne
digitalWrite(ledPin_Option_2, HIGH);
}
}//close if options
//every time through the loop, we need to reset the pressLength_Seconds counter
pressLength_milliSeconds = 0;
// Since we need to reset that one I guess we have to do the same with ontimeLength_Seconds counter ??
ontimeLength_milliSeconds = 0;
} // close void loop