piesoup:
/* Rev 1.0 of iCTD with battery monitoring, by ASF
01/09/2014
*/
#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 = 8; //above this voltage the LEDs are always on
const float tripVolts2 = 7.0; //above this voltage the LEDs blink at rate 1
const float tripVolts3 = 6.6; //above this voltage the LEDs blink at rate 2
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
}
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
{
digitalWrite(ledPin2, LOW);
digitalWrite(ledPin3, LOW);
// digitalWrite(ledPin1, HIGH);
myservo.write(45); // servo moves into climb blue
selectedPos = 1;
}
else if (buttonState2 == HIGH && buttonState == LOW && selectedPos != 3)//if button 2 is pushed and servo is not in the 3rd position
{
digitalWrite(ledPin1, LOW);
digitalWrite(ledPin2, LOW);
// digitalWrite(ledPin3, HIGH);
myservo.write(135); // servo moves into descend red
selectedPos = 3;
}
else if (buttonState == LOW && buttonState2 == LOW && selectedPos != 2)//if button is in middle position and servo is not in middle position
{
digitalWrite(ledPin1, LOW);
digitalWrite(ledPin3, LOW);
// digitalWrite(ledPin2, HIGH);
myservo.write(90); // servo moves into trail
selectedPos = 2;
}
//_________________________________________________________________________—
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
}
// 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(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
}
// 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(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);
}
}
So here it is. This works, not sure if its the correct way of going about things though!
I'll have to look at this tomorrow. Right now my brain is much mush. Simple quantum physics questions I can handle, code ... not so much. :mrgreen: