Hello everyone,
I am new to the forum and just as new to Arduino (2 months). I have a technical background but nothing regarding programming.
I look forward to being able to contribute to this forum but for the immediate future I am afraid I will be more of a drain, so please bear with me!
So... I should start by describing the part of my circuit involved by the question.
I have a +5v source connected to A1 and digital 2 (interrupt). I have connected both through a voltage divider so that the voltage sensed at both pins will change from +5v while no buttons are pressed, to anywhere below that when a particular button was pressed (buttons ground out PS1 - PS4 on the terminal block). See below.
I have created an ISR that is "falling" triggered. here is the main code. Obviously I am missing something about this and I have been trying very hard to solve it on my own.
The objective is to have TWO counters set up using 3 buttons:
counter one is "buttonCount' and uses 2 buttons, one to increase and one to decrease.
counter two is "idleCount" and it is only going to be 1 or 0 so I just use ++ and code to go back to zero.
Here is the code.
#include <PID_v1.h>
#include <Servo.h> // include servo library
#include <LiquidCrystal.h>
LiquidCrystal lcd(9, 8, 7, 6, 5, 4); // Set to GENboardV1 pins
volatile float buttonCount = 0; // store buttonCount in RAM, decimal format
volatile int idleCount = 0; // store idle setting in RAM, whole numbers
volatile int manualSpeed = 0; // store manual setting in RAM, whole numbers
float speedSetting = 0;
Servo myservo;
double Setpoint, Input, Output; // defines input types
PID myPID(&Input, &Output, &Setpoint, 2, 5, 1, DIRECT);
void setup()
{
pinMode(10, Output); // sets output of servo to pin 10
pinMode(3, INPUT); // sets digital pin 3 to paddlewheel input
myservo.attach(10);
attachInterrupt(0, switchISR, FALLING); // digital pin 2. interrupt waits for voltage drop
pinMode(2, INPUT); //interrupt pin
// pinMode(A3, INPUT); // manual mode button
// digitalWrite(A3, HIGH); // enable pull-up resistor for manual button
myPID.SetOutputLimits(0, 22); //Limits output to match servo limits (0- 90 degrees)
myPID.SetMode(AUTOMATIC); // turns on PID controls
Serial.begin(9600);
lcd.begin(16, 2);
}
void loop()
{
speedSetting = buttonCount / 10;
//int manualButton = A3; // button to activate manual setting
int paddlewheel = 3; // sets 3 to paddlewheel input
int servodrive = 10; // sets digital pin 10 to servo output
int paddlesignal = (((1 / ((pulseIn(3,LOW) * .000001) / 2) / 5.7) * 1.15)); // sets input to read paddlewheel
/*while(idleCount == 1) { // if idlecount is at 1, servo stays at idle)
myservo.write(0); //Servo to 0 degrees
lcd.setCursor(0, 0);
lcd.print("IDLE");
}
*/
// manualSpeed = 0;
//while(digitalRead(manualButton) == LOW){
// myservo.write(manualSpeed);
//}
Setpoint = speedSetting;
Input = paddlesignal; // makes the PID read the signal from the paddlewheel
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Setting:");
lcd.setCursor(10, 0);
lcd.print(speedSetting);
Serial.println(paddlesignal);
lcd.setCursor(0, 1);
lcd.print("Actual:");
lcd.setCursor(10, 1);
lcd.print(paddlesignal);
Serial.println(Setpoint);
Serial.println(idleCount);
myPID.Compute(); // computes
myservo.write(Output); // output is written to the servo (in degrees)
Serial.println(Output);
}
void switchISR(){
static unsigned long last_interrupt_time = 0;
unsigned long interrupt_time = millis();
// If interrupts come faster than 200ms, assume it's a bounce and ignore
if (interrupt_time - last_interrupt_time > 200);
{
int buttonState = map((analogRead(A1)), 1083, 0, 0, 4);
switch (buttonState){
case 1:
idleCount++;
break;
case 2:
buttonCount--;
break;
case 3:
buttonCount++;
break;
/*if (buttonState == 3) buttonCount++;
//if(manualSpeed > ) manualSpeed = 100; };
if (buttonState == 2) buttonCount--;
//if (buttonState == 1) idleCount++;
//if (analogRead(A1) > 120 && analogRead(A1) < 135) buttonCount++;
// {if(manualSpeed > 1) manualSpeed = 1;}
/*if(speedSetting > 5){
speedSetting = 5;
lcd.setCursor(0, 0);
lcd.println("5 / MAX SPD");
}*/
/*if(speedSetting < 0.5){
speedSetting = 0.5;
lcd.setCursor(0, 0);
lcd.println("0.5 / MIN SPD");
}
*/
if(idleCount > 1) idleCount = 0;
last_interrupt_time = interrupt_time;
}
}
}
The first counter, "buttonCount", works, it increments up and down. The second one "idleCount" will not.
This is trying switch/ case, and I tried a bunch of if statements previously,
(ie: "if (analogRead(A1) > 800) && (analogRead(A1) < 850) buttonCount++;)
but had similar results, I can get the first counter to increment but not the second.
So then I tried to make a simple code to test the switch/ case setup, incase I screwed it up so I wrote simple code with the switch/ case set up out of my ISR and it works just fine, increments both counters perfectly. So the issue seems to be with how I have designed the ISR.....
Can anyone help me?
Roto.
