Ok,
I have learned some things, but it still isn't working.
I have mod'ed the code to only 1 input pin and it sort of works.
When I press the button I get/see a result on the screen scrolling past and the values look right.
But when the value is 0, the LED doesn't turn off.
Help!
Yes there is a lot of remarked code. That shows how much I am stuck.
If you see things wrong, let me know.
But it is supposed to turn the LED off, loop looking for the switch to be pressed.
When it does, it toggles the LED.
Yeah, the turn off is in the main loop. Hang on.
The LED never turns off.
It is off at initial loading of the code, but after a couple of seconds it is on and always on.
If/when I get it working, I shall change the structure a bit - which is why I left in the other code.
It may be problematic.
But I may get to it when I get past this problem, but am still looking for help.
/*
This is my routine to control 4 power points for timed periods.
There are 4 buttons used. Each has multiple functions.
There are:
5 output pins.
and 4 input pins.
I also need to change the pins later when I add a display.
For now I am just using pins for the sake of testing and getting my head around the code.
The fancy stuff comes later.
*/
#include "ClickButton.h"
#define Main_Power 6
#define Output1 7
#define Output2 8
#define Output3 9
#define Output4 10
#define B1P 2
#define B2P 3
#define B3P 4
#define B4P 5
#define timeout 5 // number of seconds for timeout of unit.
int LED_State = 0;
int button1_function = 0;
int button2_function = 0;
int button3_function = 0;
int button4_function = 0;
long currentTime;
//const int buttonPin1 = B1P;
ClickButton button1(2, LOW, CLICKBTN_PULLUP);
//const int buttonPin2 = B2P;
//ClickButton button2(B2P, LOW, CLICKBTN_PULLUP);
//const int buttonPin3 = B3P;
//ClickButton button3(B3P, LOW, CLICKBTN_PULLUP);
//const int buttonPin4 = B4P;
//ClickButton button4(B4P, LOW, CLICKBTN_PULLUP);
void setup()
{
// put your setup code here, to run once:
Serial.begin(9600);
// This is the pins and what they do.
// Not sure if I need the first 4 lines for the inputs.
pinMode(Main_Power,OUTPUT);
pinMode(Output1,OUTPUT);
pinMode(Output2,OUTPUT);
pinMode(Output3,OUTPUT);
pinMode(Output4,OUTPUT);
// Setup button timers (all in milliseconds / ms)
// (These are default if not set, but changeable for convenience)
button1.maxPresses = 3; // max button multiclick count
button1.debounceTime = 20; // Debounce timer in ms
button1.multiclickTime = 250; // Time limit for multi clicks
button1.heldDownTime = 1000; // time until "held-down clicks" register
/*
// Setup button timers (all in milliseconds / ms)
// (These are default if not set, but changeable for convenience)
button2.maxPresses = 3; // max button multiclick count
button2.debounceTime = 20; // Debounce timer in ms
button2.multiclickTime = 250; // Time limit for multi clicks
button2.heldDownTime = 1000; // time until "held-down clicks" register
// Setup button timers (all in milliseconds / ms)
// (These are default if not set, but changeable for convenience)
button3.maxPresses = 3; // max button multiclick count
button3.debounceTime = 20; // Debounce timer in ms
button3.multiclickTime = 250; // Time limit for multi clicks
button3.heldDownTime = 1000; // time until "held-down clicks" register
// Setup button timers (all in milliseconds / ms)
// (These are default if not set, but changeable for convenience)
button4.maxPresses = 3; // max button multiclick count
button4.debounceTime = 20; // Debounce timer in ms
button4.multiclickTime = 250; // Time limit for multi clicks
button4.heldDownTime = 1000; // time until "held-down clicks" register
*/
bootup(1); // Turn on power to the system.
}
void loop()
{
// put your main code here, to run repeatedly:
delay(100);
Serial.println("Looping");
int time_left; // How many seconds left before shut down
int active_timer; // Returns 1 if any of the 4 outputs are active
int shut_down; // Returns 1 if system to be shut down.
int button_pressed; // Says a button is pressed.
digitalWrite(13,0);
Serial.println(" ");
Serial.println("**");
//
//
// Need to check if any timers are running and if they are, skip this function.
shut_down = timer_reset();
if (shut_down == 1)
{
bootup(0);
}
//
// button_pressed = check_buttons();
// This routine checks if any button has been pressed.
button1.Update();
if (button1.click != 0) button_pressed = button1.click;
if (button1.click == 1) button_pressed = 1;
if (button1_function == 2) button_pressed = 5;
Serial.println(button_pressed);
switch (button_pressed)
{
case 0:
break;
case 1:
outlet_1();
break;
case 2:
outlet_2();
break;
case 3:
outlet_3();
break;
case 4:
outlet_4();
break;
case 5:
edit(1);
break;
case 6:
edit(2);
break;
case 7:
edit(3);
break;
case 8:
edit(4);
break;
}
}
//========================================================================
void bootup(int action)
{
//
if (action == 1)
digitalWrite(Main_Power,HIGH);
else
if (action == 0)
digitalWrite(Main_Power,LOW);
//
}
//========================================================================
int timer_reset()
{
currentTime = (long)millis();
return (0); // for now.
}
//========================================================================
int check_buttons()
{
// This routine checks if any button has been pressed.
button1.Update();
if (button1.click != 0) button1_function = button1.click;
if (button1.click == 1) return (1);
if (button1_function == 2) return (5);
}
/*
{
//
button1_function = button1.click;
Serial.println("Boo!");
// if (button1.click == 1)
if (button1.click == 1)
{
button_value = 1;
return (button_value);
}
if (button1.click == 2)
button_value = 5;
}
button2.Update();
if (button2.click != 0)
{
if (button2.click == 1)
button_value = 2;
if (button2.click == 2)
button_value = 6;
}
button3.Update();
if (button3.click != 0)
{
if (button3.click == 1)
button_value = 3;
if (button2.click == 2)
button_value = 7;
}
button4.Update();
if (button4.click != 0)
{
if (button4.click == 1)
button_value = 4;
if (button2.click == 2)
button_value = 8;
*/
// }
// return (button_value);
//}
//========================================================================
void outlet_1()
{
//
LED_State = !LED_State;
Serial.println(" ");
Serial.println(" ");
Serial.println("Button 1 pressed");
Serial.println(LED_State);
Serial.println(" ");
Serial.println(" ");
digitalWrite(13,LED_State);
}
//========================================================================
void outlet_2()
{
//
}
//========================================================================
void outlet_3()
{
//
}
//========================================================================
void outlet_4()
{
//
}
//========================================================================
int edit(int outlet)
{
//
}
// below is code I got from the example "click_button" function to show me how to make a countdown timer with no RTC and no delay.
/*
currentTime = (long)millis();
button1.Update();
if (button1.click != 0) function = button1.click;
// Toggle LED on single clicks
if(button1.click == 1) ledState = !ledState;
// fade if button is held down during single-click
if(function == -1 && button1.depressed == true)
{
ledState = true; // force lights on, since we want to fade it up or down
if (oldFadeUp == fadeUp) fadeUp = !fadeUp; // Switch direction
if ( currentTime - adjustFaderTime > fadeDelay)
{
adjustFaderTime = currentTime + fadeDelay;
if (fadeUp) fadeValue++; else fadeValue--;
// Some boundary checking
// Using signed ints, we can check for below 0 and above 255 (byte limit)
if (fadeValue > 255) fadeValue = 255;
if (fadeValue < 0) fadeValue = 0;
}
} else {
// Save old fade direction for next time
oldFadeUp = fadeUp;
// Reset function
function = 0;
}
*/