So im controlling a water pump with an arduino, and i want to press a button the RC transmitter to turn on the pump for a certain amount of time. Ive heard that using the switch function to turn something on for a time interval wont work. Can someone elaborate on this for me? what can the switch function do and cant do?
Hello
You may take a view here:
Have a nice day and enjoy coding in C++.
to time events use delay() or millis()
the C/C++ switch() statement is a decision control statement
Switch is like a more efficient version of if{} that only works in certain circumstances.
You were either listening to an idiot or you misunderstood what they were saying. One command will turn something on and you need another command to turn it off after a certain time has elapsed.
you just use switch typically when a variable can take many different values and it would be too complicated to have tons of nested if to decide what to do.
a typical example with 2 values (named here DEVICE_IS_OFF, DEVICE_IS_ON) that represent the state of some device
enum {DEVICE_IS_OFF, DEVICE_IS_ON} state = DEVICE_IS_OFF;
void setup() {
...
}
void loop() {
switch (state) {
case DEVICE_IS_OFF:
if (condition_for_ON) {
// turn device ON
...
state = DEVICE_IS_ON;
}
break;
case DEVICE_IS_ON:
if (condition_for_OFF) {
// turn device OFF
...
state = DEVICE_IS_OFF;
}
break;
}
}
Here's a simple finite state machine built with switch/case which uses millis() timing.
Many other tutorials and examples around this site and the web.
switch (value)
{
case 1:
do_case_1();
break;
default:
do_default();
case 17:
do_case_17();
break;
}
is the equivalent of:
if (value == 1)
do_case_1();
else if (value == 17)
do_case_17();
else
{
do_default();
do_case_17();
}
It is for those times where the value of an integer expression determines what you do next.
ahh i believe im seeing it now, so basically for each case i have to assign a certain logic function, for example, for case one, i can write the condition for the button on the RC remote being pressed, to turn on the pump, then for the second case, i can write the conditon for the button being pressed again, but to turn off the pump? heres an example of my code below, also i want to control a stepper motor as well to turn it a certain number of steps.
#include <IBusBM.h>
#include <Servo.h>
#include <Stepper.h)
const int stepsperRevolution = 200; //defines the # of steps on the stepper motor
Stepper myStepper(stepsperRevolution, 8,9,10,11) //pin numbers are arbitrary to whatever the motor driver pins are on the arduino
IBusBM IBus; // IBus object
Servo myservo; // create servo object to control a servo
int buttonPress = 0;//set variables for the button press and the arduino will keep track of how many times
int numPress = 0; // the button has been pressed, to adjust for each hole
void setup() {
myStepper.setSpeed(5);
Serial.begin(9600);
IBus.begin(Serial); // iBUS object connected to serial0 RX pin
myservo.attach(9); // attaches the servo on pin 9 to the servo object
}
void loop() {
int val;
val = IBus.readChannel(0); // get latest value for servo channel 1
myservo.writeMicroseconds(val); // sets the servo position
delay(20);
numPress = numPress++
switch(numPress)
{
case 1:
//write logic for stepper motor for 20 steps
//turn on pump for "x" amount of seconds
//rotate back 10 steps to discharge hole
//turn on pump for "y" amount of time to remove excess
//rotate back 10 steps to starting point
}
{
case 2:
//write logic for stepper motor for 30 steps
//turn on pump for "x" amount of time
//rotate back 20 steps to discharge hole
//turn on pump for "y" amount of time
//rotate back 10 steps to starting point
}
{
case 3:
//write logic for stepper motor for 40 steps
//turn on pump for "x" amount of time
//rotate back 30 steps to dischrage hole
//turn on pump for "y" amount of time
//rotate back 10 steps to starting point
}
{
case 4:
//write logic for stepper motor for 50 steps
//turn on pump for "x" amount of time
//rotate back 40 steps to discharge hole
// turn on pump for "y" amount of time
//rotate back 10 steps to starting point
}
{
case 5:
//write logic for stepper motor for 60 steps
//turn on pump for "x" amount of time
//rotate back 50 steps to dischage hole
//turn on pump for "y" amount of time
//rotate back 10 steps to starting point
}
{
case 6:
//write logic for stepper motor for 70 steps
//turn on pump for "x" amount of time
//rotate back 60 steps to discharge hole
//turn on pump for "y" amount of time
//rotate back 10 steps to starting point
}
{
case 7:
//write logic for stepper motor for 80 steps
//turn on pump for "x" amount of time
//rotate back 70 steps to discharge hole
//turn on pump for "y" amount of time
//rotate back 10 steps to starting point
}
{
case 8:
//write logic for stepper motor for 90 steps
//turn on pump for "x" amount of time
//rotate back 80 steps to discharge hole
//turn on pump for "y" amount of time
//rotate back 10 steps to starting point
}
You should re-edit your posting so your code apears as a code-section
kind of but read the documentation to see where to put the {} and how to use break
please use code tags when posting code.
well done formating the code
The "logic" you have written has a pattern
//write logic for stepper motor for (numpress+ 1) * 10 steps
//turn on pump for "x" amount of seconds
//rotate back numpress * 10 steps to discharge hole
//turn on pump for "y" amount of time to remove excess
//rotate back 10 steps to starting point
for using switch case
if each case shall be mutually exclusive there has to be a "break;" as the last statement in each "case"
I did it for him
for this time. I was in a good day
here is a demo-code for switch case with explanation
#define dbg(myFixedText, variableName) \
Serial.print( F(#myFixedText " " #variableName"=") ); \
Serial.println(variableName);
// usage: dbg("1:my fixed text",myVariable);
// myVariable can be any variable or expression that is defined in scope
#define dbgi(myFixedText, variableName,timeInterval) \
do { \
static unsigned long intervalStartTime; \
if ( millis() - intervalStartTime >= timeInterval ){ \
intervalStartTime = millis(); \
Serial.print( F(#myFixedText " " #variableName"=") ); \
Serial.println(variableName); \
} \
} while (false);
unsigned long myCounter;
enum myStates {
sayHello,
startCounting,
countTo10,
sayGoodbye,
wait5seconds
};
enum myStates myStateVar;
unsigned long waitingTime = 5000;
unsigned long StartWaiting;
unsigned long currentMillis;
unsigned long oneSecondStart;
void setup() {
Serial.begin(115200);
Serial.print( F("\n Setup-Start \n") );
myCounter = 0;
myStateVar = sayHello;
}
boolean oneSecondIsOver(unsigned long StartTime, unsigned long rightNow) {
if (rightNow - StartTime >= 1000) {
return true;
}
else {
return false;
}
}
void myStepChain() {
currentMillis = millis();
switch (myStateVar) {
case sayHello:
Serial.println();
Serial.println();
Serial.println();
dbg("entering case sayHello",myStateVar);
Serial.println("Hello User!");
myStateVar = startCounting;
dbg("leaving case sayHello",myStateVar);
Serial.println();
break;
case startCounting:
dbg("entering case startCounting",myStateVar);
myCounter = 0;
Serial.println( F("I start counting new") );
myStateVar = countTo10;
dbg("leaving case startCounting",myStateVar);
Serial.println();
break;
case countTo10:
dbg("entering case countTo10",myStateVar) ;
myCounter++;
Serial.print( F("I'm counting up counter="));
Serial.println(myCounter);
if (myCounter == 10) {
myStateVar = sayGoodbye;
}
dbg("leaving case countTo10",myStateVar);
break;
case sayGoodbye:
Serial.println();
dbg("entering case sayGoodbye",myStateVar);
Serial.println( F("goodbye see you next round in 5 seconds") );
StartWaiting = currentMillis;
myStateVar = wait5seconds;
dbg("leaving case sayGoodbye",myStateVar);
break;
case wait5seconds:
if (currentMillis - StartWaiting >= waitingTime) {
Serial.print( F("seconds waited ") );
Serial.print( (currentMillis - StartWaiting) / 1000 );
Serial.println();
Serial.println();
dbg("case wait5seconds ",myStateVar);
myStateVar = sayHello;
dbg("case wait5seconds ",myStateVar);
}
myCounter++;
if (oneSecondIsOver(oneSecondStart,currentMillis) ) {
Serial.print( F("one second over") );
Serial.print( F(" counting very fast myCounter=") );
Serial.println(myCounter);
oneSecondStart = currentMillis;
}
break;
}
//delay(100);
}
void loop() {
myStepChain();
}
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.