hello all, i'm really new to arduino. My circuit are quite simple, a potentiometer to adjust the value, a push button to confirm the settings, a servo and a buzzer. What i'm trying to achieve here is when (A) i adjust the potentiometer, if there is difference between lastvalue and current value, (B) it will start the push button monitoring function which i have to press the button to set and confirm the changes to adjust the servo steps. If there is no confirmation press button after lets say 15 seconds, the buzzer will beep to remind.
I got all the individual part up and running, just that i'm not clear on how the loop function adruino works. I could not link up (A) and (B). In normal cpp programming i could have just use state machine, am i allow to do this on arduino? else could someone please advice me or point me to the correct direction on how can i do this.
Use CTRL T to format your code.
Attach your ‘complete’ sketch between code tags, use the </> icon in the posting menu. [code]Paste your sketch here[/code]
You can use the state machine technique on the Arduino.
chrischiang:
point me to the correct direction on how can i do this.
First, you need to post the program that represents your best attempt and tell us in detail what it actually does and what you want it to do that is different. It will make it much easier to focus on the parts you need help with rather than wasting time on things that you can do.
larryd:
Use CTRL T to format your code.
Attach your ‘complete’ sketch between code tags, use the </> icon in the posting menu. [code]Paste your sketch here[/code]
You can use the state machine technique on the Arduino.
Basically those are my code for now. I did not integrate it yet as i have no idea how to.. please advise further
void setup() {
// initialize digital pin LED_BUILTIN as an output.
pinMode(LED_BUILTIN, OUTPUT);
pinMode(BUTTON_PIN2, INPUT_PULLUP); //initialize onboard button
pinMode(BUZZER_PIN8, OUTPUT);
attachInterrupt(digitalPinToInterrupt(BUTTON_PIN2), stopBuzzer, LOW); //button to trigger stop on buzzer
Serial.begin(9600);
getStartupValue();
}
// the loop function runs over and over again forever
void loop() {
//onBoardButtonState();
//setDelayLED();
//blinkLed();
//delay(1000);
}
int val_2 = 0; // variable to store the value coming from BUTTON_PIN2
int pot1LastValue = 0;
int buttonPushCounter = 0; //button pressed counter
int buttonState; // set value for buttonState
int lastButtonState = LOW; // the previous reading from the input pin
unsigned long lastDebounceTime = 0; // the last time the output pin was toggled
unsigned long debounceDelay = 50; // the debounce time; increase if the output flickers
int ledState = HIGH;
int ledBlinkRate = 1000;
int beepState = 1;
bool inRange(int val, int minimum, int maximum)
{
return ((minimum <= val) && (val <= maximum));
}
int analogReadLed()
{
int count = 0;
int ret = analogRead(POT_PIN2); // read the value from the sensor
Serial.print("analog read = ");
Serial.println(ret);
return ret;
}
void onBoardButtonState()
{
val_2 = digitalRead(BUTTON_PIN2);
if (val_2 != lastButtonState)
{
lastDebounceTime = millis();
}
if ((millis() - lastDebounceTime) > debounceDelay)
{
if (val_2 != buttonState)
{
buttonState = val_2;
if (buttonState == LOW)
{
setDelayLED(); //Button Set function
buttonPushCounter++;
Serial.print("button pressed count = ");
Serial.println(buttonPushCounter);
}
}
}
lastButtonState = val_2;
}
void buzzerBeep()
{
if (beepState == 1) //TODO some place need to set to 1 after buzzer stop
{
tone(BUZZER_PIN8, 1000);
delay(200);
tone(BUZZER_PIN8, 500);
delay(200);
tone(BUZZER_PIN8, 200);
delay(200);
noTone(BUZZER_PIN8);
delay(200);
}
}
void stopBuzzer()
{
noTone(BUZZER_PIN8);
beepState = 0;
}
void setDelayLED()
{
int var = analogReadLed();
Serial.print("setdelayled pot1Lastvalue = ");
Serial.println(pot1LastValue);
Serial.print("var = ");
Serial.println(var);
if (!(inRange(var, pot1LastValue - 5, pot1LastValue + 5)))
{
pot1LastValue = var;
ledBlinkRate = var;
}
}
void blinkLed()
{
digitalWrite(LED_PIN, HIGH); // turn the ledPin on
delay(ledBlinkRate); // stop the program for some time
digitalWrite(LED_PIN, LOW); // turn the ledPin off
delay(ledBlinkRate); // stop the program for some time
}
(Typed the below while you were typing; I haven't yet looked at the code you gave.)
What should happen if you don't don't want to confirm the change? Does it sit and beep until the cows come home, or is there another button (or a further timeout) to cancel the change and set lastvalue to the (new) current value?
As described so far though, your states (probably via a switch-case) could be something like 0: waiting_for_pot_to_change, 1: waiting_for_confirmation (during which it beeps delay()lessly) 2: moving_servos.
Transition from 0 to 1 would be a change in the pot, and from 1 to 2 would be the confirm button. It would go from 2 to back 0 once the servo move was done, and set previous to current.
If you implemented a second timeout or a cancel button, that would take you from 1 to 0.
marco_c:
You are positively encouraged to use FSM in Arduino. It is a very good way to get things done.
What is 'normal' cpp coding, by the way?
Oh i meant state machine kind switch case those. I just search FSM in Arduino, i guess somehow will works.
If i have 1 state which is delay state will it block the code if i put this in loop()?
Robin2:
First, you need to post the program that represents your best attempt and tell us in detail what it actually does and what you want it to do that is different. It will make it much easier to focus on the parts you need help with rather than wasting time on things that you can do.
...R
Hi R, pls refer to my reply above, i pasted my code above. Basically what i'm trying to do here is
potentiometer to adjust the value 0%-100%,
check if the value change. If change, start button function,
Will need to press the button to set the value read from potentiometer.
if by 15 secs, button not press to confirm, ring buzzer to remind.
once button pressed, stop buzzer and set the value
for now, i setting the value as delay in between LED light on and off. (will change to servo step once my part arrive)
Based on what you have said in Reply #6 I think the code needs to be like this pseudo code
potVal = analogRead(potPin);
if (potVal != prevSetValue) {
lastPotMovedTime = millis();
buttonCheckEnabled = true;
}
if (buttonCheckEnabled == true) {
buttonState = digitalRead(buttonPin);
if (buttonState == LOW) { // assumes LOW when pressed
prevSetValue = potVal;
buttonCheckEnabled = false;
}
}
if (buttonCheckEnabled == true and millis() - lastPotMovedTime >= 15000) {
// pot was moved but button was not pressed within 15 secs
// do something
}
twinkleyan:
(Typed the below while you were typing; I haven't yet looked at the code you gave.)
What should happen if you don't don't want to confirm the change? Does it sit and beep until the cows come home, or is there another button (or a further timeout) to cancel the change and set lastvalue to the (new) current value?
As described so far though, your states (probably via a switch-case) could be something like 0: waiting_for_pot_to_change, 1: waiting_for_confirmation (during which it beeps delay()lessly) 2: moving_servos.
Transition from 0 to 1 would be a change in the pot, and from 1 to 2 would be the confirm button. It would go from 2 to back 0 once the servo move was done, and set previous to current.
If you implemented a second timeout or a cancel button, that would take you from 1 to 0.
i have not think bout that yet, probably will keep the buzzer ring every few mins repeat until someone stop it or cancel the changes.
Robin2:
Based on what you have said in Reply #6 I think the code needs to be like this pseudo code
potVal = analogRead(potPin);
if (potVal != prevSetValue) {
lastPotMovedTime = millis();
buttonCheckEnabled = true;
}
if (buttonCheckEnabled == true) {
buttonState = digitalRead(buttonPin);
if (buttonState == LOW) { // assumes LOW when pressed
prevSetValue = potVal;
buttonCheckEnabled = false;
}
}
if (buttonCheckEnabled == true and millis() - lastPotMovedTime >= 15000) {
// pot was moved but button was not pressed within 15 secs
// do something
}
...R
meanwhile i will test with R suggestion for now and see any further enhancement state that i need to add in!
chrischiang:
meanwhile i will test with R suggestion
Just in case you didn't notice, Robin2 pointed out that what he posted is pseudocode- it looks pretty close to "real-o-code" but don't expect it to compile out the box.