Hello
I have had some great help in another thread, making leds flash at a certain voltage.
https://forum.sparkfun.com/viewtopic.php?f=32&t=39084
My above project is a servo, that controls a three position hydraulic switch, and a certain color led for each position.
Currently, it operates with a DPDT switch. But to save space, I want to use a single mom push button.
I want to cycle between red and blue leds by clicking button. Switch on Green led (turn red & blue off) with a press.
If I can do this, I’m sure I will be able to use the format to control my servos.
Here is the existing code, a product of the help i received here previously…
/* Rev 1.1 of iCTD with battery monitoring, by ASF
Big thanks to Mee_n_Mac on Sparkfun
01/09/2014
Battery monitoring is commented out for clarity
*/
#include <Servo.h>
Servo myservo; // create servo object to control a servo
int pos = 0; // variable to store the servo position
int selectedPos = 2; // sets the selected servo position to middle
const int buttonPin = 2; //sets pin 2 as button
const int buttonPin2 = 3; //sets pin 3 as button
const int ledPin1 = 10; // sets pin 10 as LED for Climb Blue
const int ledPin2 = 11; // sets pin 11 as LED for Trail Green
const int ledPin3 = 12; // sets pin 12 as LED for Descend Red
int buttonState = 0; //sets button 1 as off
int buttonState2 = 0; // sets button 2 as off
//declare the constants used
const float tripVolts1 = 7.5; //above this voltage the LEDs are always on 8
const float tripVolts2 = 7.5; //above this voltage the LEDs blink at rate 1 7.5
const float tripVolts3 = 6.6; //above this voltage the LEDs blink at rate 2 6.6
const float scaleFactor = 0.595;//ratio of divided down voltage to battery voltage
const int blinkRate1 = 1; //blink rate1 in Hz (blinks/sec)
const int blinkRate2 = 2; //blink rate2 in Hz (blinks/sec)
const int blinkRate3 = 4; //blink rate4 in Hz (blinks/sec)
const int battPin = A0; //number of analog pin used to measure battery voltage
//declare the variables used
boolean blinkState = false; //variable to control LEDs on or off
unsigned long blinkPeriod = 0; //period in msec of blinking
unsigned long blinkTime = millis(); //variable to store when blink state was last changed
float battVolts = 0; //battery voltage
void setup()
{
myservo.attach(9); // attaches the servo on pin 9 to the servo object
pinMode (buttonPin, INPUT); // sets button as input
pinMode (buttonPin2, INPUT); // sets button as input
pinMode (ledPin1, OUTPUT); // sets led as output
pinMode (ledPin2, OUTPUT); // sets led as output
pinMode (ledPin3, OUTPUT); // sets led as output
digitalWrite(ledPin1, LOW);
digitalWrite(ledPin3, LOW);
digitalWrite(ledPin2, HIGH); // will probably remove this
myservo.write(90); // sets servo to mid trail position
delay(500);
myservo.detach();
}
void loop()
{
buttonState = digitalRead(buttonPin);
buttonState2 = digitalRead(buttonPin2);
if (buttonState == HIGH && buttonState2 == LOW && selectedPos != 1) //if button 1 is pushed and servo is not in position 1
{
myservo.attach(9);
digitalWrite(ledPin2, LOW);
digitalWrite(ledPin3, LOW);
digitalWrite(ledPin1, HIGH); // Comment out when using blink code below
myservo.write(45); // servo moves into climb blue
delay(250);
myservo.detach();
selectedPos = 1;
}
else if (buttonState2 == HIGH && buttonState == LOW && selectedPos != 3)//if button 2 is pushed and servo is not in the 3rd position
{
myservo.attach(9);
digitalWrite(ledPin1, LOW);
digitalWrite(ledPin2, LOW);
digitalWrite(ledPin3, HIGH); // Comment out when using blink code below
myservo.write(135); // servo moves into descend red
delay(250);
myservo.detach();
selectedPos = 3;
}
else if (buttonState == LOW && buttonState2 == LOW && selectedPos != 2)//if button is in middle position and servo is not in middle position
{
myservo.attach(9);
digitalWrite(ledPin1, LOW);
digitalWrite(ledPin3, LOW);
digitalWrite(ledPin2, HIGH); // Comment out when using blink code below
myservo.write(90); // servo moves into trail
delay(250);
myservo.detach();
selectedPos = 2;
}
}
//_________________________________________________________________________---
/* Commented out for clarity
if (selectedPos == 1) // climb
{
//measure the divided down voltage and convert back into battery voltage
battVolts = (float(analogRead(battPin)) * 5.0 / 1023.0) / scaleFactor;
if (battVolts > tripVolts1) { //check voltage above or below high trip point
blinkPeriod = 0; //voltage above, special condition for no blinking
}
//now blink the LEDs if conditions are correct
if (blinkPeriod == 0) { //special condition means no blinking
digitalWrite(ledPin1, HIGH); //turn led on
}
else {
//blink the LEDs on and off
//check is blinkTime/2 has passed since last on/off change
//if true change on to off or off to on
if (millis() - blinkTime >= (blinkPeriod / 2)) {
blinkState = ~blinkState; //change blinkState
blinkTime = millis(); //reset blinkTime
}
digitalWrite(ledPin1, blinkState); //turn led on or off per blinkState
}
delay(blinkPeriod/10);
}
//_________________________________________________________________________---
else if (selectedPos == 2 ) //trail
{
//measure the divided down voltage and convert back into battery voltage
battVolts = (float(analogRead(battPin)) * 5.0 / 1023.0) / scaleFactor;
if (battVolts > tripVolts1) { //check voltage above or below high trip point
blinkPeriod = 0; //voltage above, special condition for no blinking
}
else if (battVolts > tripVolts2) { //check voltage above or below 2nd trip point
blinkPeriod = (1000 / blinkRate1); //voltage above, blink at rate1
}
else if (battVolts > tripVolts3) { //check voltage above or below 3rd trip point
blinkPeriod = (1000 / blinkRate2); //voltage above, blink at rate2
}
else {
blinkPeriod = (1000 / blinkRate3); //voltage below, blink at rate3
}
//now blink the LEDs if conditions are correct
if (blinkPeriod == 0) { //special condition means no blinking
digitalWrite(ledPin2, HIGH); //turn led on
}
else {
//blink the LEDs on and off
//check is blinkTime/2 has passed since last on/off change
//if true change on to off or off to on
if (millis() - blinkTime >= (blinkPeriod / 2)) {
blinkState = ~blinkState; //change blinkState
blinkTime = millis(); //reset blinkTime
}
digitalWrite(ledPin2, blinkState); //turn led on or off per blinkState
}
delay(blinkPeriod/10);
}
//_________________________________________________________________________---
else if (selectedPos == 3 ) //descend
{
//measure the divided down voltage and convert back into battery voltage
battVolts = (float(analogRead(battPin)) * 5.0 / 1023.0) / scaleFactor;
if (battVolts > tripVolts1) { //check voltage above or below high trip point
blinkPeriod = 0; //voltage above, special condition for no blinking
}
//now blink the LEDs if conditions are correct
if (blinkPeriod == 0) { //special condition means no blinking
digitalWrite(ledPin3, HIGH); //turn led on
}
else {
//blink the LEDs on and off
//check is blinkTime/2 has passed since last on/off change
//if true change on to off or off to on
if (millis() - blinkTime >= (blinkPeriod / 2)) {
blinkState = ~blinkState; //change blinkState
blinkTime = millis(); //reset blinkTime
}
digitalWrite(ledPin3, blinkState); //turn led on or off per blinkState
}
delay(blinkPeriod/10);
}
}
*/
Now I have looked into some arduino tutorials online, the OneButton library (http://www.mathertel.de/Arduino/OneButtonLibrary.aspx) and the clickbutton library (https://code.google.com/p/clickbutton/)
I cant get them to work as I don’t fully understand them. I’ve looked into FSM, but all the examples look too complicated. And I think I am trying to over complicate the code anyway!
I’ve tried this as an example…
#include "OneButton.h"
/* Cycle through (alternate) red and blue leds by clicking button.
Switch on Green led (turn off red and blue) with a press
*/
// setup a new OneButton on pin 2
OneButton button(2, true);
const int ledPin1 = 9;
const int ledPin2 = 10;
const int ledPin3 = 11;
int a = 0;
int buttonPushCounter = 0; // set button click counter to 0
void setup()
{
pinMode (ledPin1, OUTPUT);
pinMode (ledPin2, OUTPUT);
pinMode (ledPin3, OUTPUT);
button.attachClick(singleClick); // link click and press functions to be called on an event
button.attachPress(singlePress);
// put your setup code here, to run once:
}
void loop()
{
// put your main code here, to run repeatedly:
button.tick(); //Monitor the button
delay(10);
}
void singleClick()
{
buttonPushCounter++;
if(buttonPushCounter ==3)
{
buttonPushCounter = 0; //there are only 2 states for the button, 3 is not allowed
}
if (buttonPushCounter == 1 ||buttonPushCounter ==0)
{
ledPin1, HIGH;
a = 0;
}
if (buttonPushCounter == 2 ||buttonPushCounter <= 2)
{
ledPin2, HIGH;
a = 1;
}
It doesn’t compile and I dont understand the errors.
Any help or direction would be very much appreciated!
Thanks