So yah lots of pieces to put together. I figured lets start in smaller steps. I have a program that works, the real live assembly works, and it is soon to be put into its 3D printed housing. Then sent out open source to the health community that will utilize structured water.
The only difference is a single tactile button, to turn on ambient light, motor, uv light, then fourth click turns it all off.
I took a moment to make it way more cool by adding in the "knight Rider" [not dating myself] led timing and the use of array for pins. Again baby steps.
Now it kinda works (small hurrray!!!) but again kinda.....in the simulation the ambient knight riders turn on with the 1 click and they vibe back and forth (winning), but on second "click" (to turn on motor) nothing happens, 3rd click nothing, 4th click nothing, 5th click the knight rider turns off (clicks 2-4 do not register in monitor). Then we are back in sequence with the UV lights coming on and then last click turns everything off.
So I have a structure issue in the loop. I also // dashed out the digitalWrite ledPin in the last loop block (click #4) to turn everything off as I have issues making that work with the knight rider addition. So I know enough to be dangerous, but don't understand well enough to get this code back to safety.
// this constant won't change:
const int buttonPin = 2; // the pin that the pushbutton is attached to
const int ledPins [] = { 10, 11, 12}; // the pins that the ambient LEDs is attached to
const int motorPin = 3; //the pin the motor is attached to
const int uvPin = 4; // the pin that the UV LED's attached to
const int timer = 100; // timer for knight rider ambient light
// Variables will change:
int buttonPushCounter = 0; // counter for the number of button presses
int buttonState = 0; // current state of the button
int lastButtonState = 0; // previous state of the button
void setup() {
// initialize the button pin as a input:
pinMode(buttonPin, INPUT_PULLUP);
// initialize the LED's and motor as an output:
//pinMode(ledPin, OUTPUT);
for (int thisPin = 10; thisPin < 13; thisPin++) {
pinMode(thisPin, OUTPUT);}
pinMode(motorPin, OUTPUT);
pinMode(uvPin, OUTPUT);
// initialize serial communication:
Serial.begin(9600);
}
void loop() {
// read the pushbutton input pin:
buttonState = digitalRead(buttonPin);
// compare the buttonState to its previous state
if (buttonState != lastButtonState) {
// if the state has changed, increment the counter
if (buttonState == HIGH) {
// if the current state is HIGH then the button went from off to on:
buttonPushCounter++;
Serial.println("on");
Serial.print("number of button pushes: ");
Serial.println(buttonPushCounter);
} else {
// if the current state is LOW then the button went from on to off:
Serial.println("off");
}
// Delay a little bit to avoid bouncing
delay(50);
}
// save the current state as the last state, for next time through the loop
lastButtonState = buttonState;
// Controlling the Clicks
if (buttonPushCounter == 1) {
// digitalWrite(ledPin, HIGH); //one click turns on the ambient LED
for (int thisPin = 10; thisPin < 13; thisPin ++){
//turn pin on
digitalWrite(thisPin, HIGH);
delay(timer);
//turn pin off
digitalWrite(thisPin, LOW);
}
for (int thisPin = 12; thisPin >= 10; thisPin --) {
// turn pin on
digitalWrite(thisPin, HIGH);
delay(timer);
digitalWrite(thisPin, LOW);
}
}
if (buttonPushCounter == 2){
digitalWrite(motorPin, HIGH); //two cliks turns on the motor while ambient LED stays on
}
if (buttonPushCounter == 3){
digitalWrite(uvPin, HIGH); //three clicks turns on the UV LED's while motor and ambient LED stay on
}
if (buttonPushCounter == 4){
// digitalWrite(ledPins[3] {10, 11, 12}, LOW);
digitalWrite(motorPin, LOW);
digitalWrite(uvPin, LOW); //four clicks turns all LED's and motor off
}
if(buttonPushCounter == 4)buttonPushCounter = 0;
}
So with this code and this project that has meaning to me, i feel i can learn the logic with a little more intent. The lessons here I think could then carry over to arrays on the 4 button touch sensor as it is really the pins that matter not so much the peripheral device.
I thank you in advance for taking time out of your life to school a stranger.
Happy regards,
Included is the unit that will be powered by this code for context.