If's & else if's without void loop

Hi there, I am trying to write a sketch with two infra red detectors. I would like to use 'if', 'else if' and 'else' but not in the 'void loop' because my goal is that my Boe-bot executes 3 different series of maneuvers with 3 different conditions but only once each. Could anyone help me please?

Say more. Look at some example code.

This kind of thing is done alla time inside the loop() function.

Unless you literally mean once and only once each then… nothing.


1 Like

Probably not. The loop() is designed to perform all processing after everything is initialized and configured in the setup(). If you literally only want to perform something one time then you can put it at the end of setup() and have an empty loop(). But that is uncommon.

Are you saying that after these maneuvers are performed then nothing else should happen until the Boe-bot is powered up again?

Okay I see.thanks for the answer :smile: My idea was that the robot would do a certain maneuver when only the left sensor detects an object. When only the right sensor detects an object it would do a different maneuver. And when both detect an object it would do an even different maneuver. I'd want it to execute the maneuver after the sensors detected the object(s). And then come back to the first place where an other object would be, etc. It would do it 3 times.

yes pretty much

There is no reason you can't do that in loop(). Take your best shot at writing the code and try to formulate a better description of the movements. Then we can help you further.

sounds good, thanks a lot for your help!

Hi. Have you programmed using a state machine?. I advice you to look into this way of approach. It uses a switch statement which handles the possible states of the machine. In your case when the robot senses an obstacle the code must set a state corresponding to the event. Lets say:

left sensor sets up left_sensor_state
right sensor sets up right_sensor_state
both sensors sets up both_sensor_state

then your loop() has a function called check_state();

and check_state() has your:

switch (state)

case left_sensor_state
do this

like that with other 2 or 3 cases, or as many as you want.

I've coded using several state machines at the same time and its pretty stable. It feels like pseudomultitasking,.

Remember to always have a none_state kind of state, where your code "waits" for action like nothing occurs during the none_state. Here an example for the definition:

typedef enum
} states;

states current_state = STATE_START; // current state-machine state

And also remember always after performing the action / maneuver to return the state to STATE_NONE, usually right before the break.




Sry, still wondering about this... programmers can be quite literal, so when you say it is supposed to do something three times we can take that to mean only three times.

Then nothing.

This is prolly not what you mean. If you could be very carefully thorough about what you want to end up doing it will help.

Like "Watch the sensors. Do one thing for left, another for right and a third action if both sensors trigger. Rinse and repeat."

But again - how close do the two sensors have to be on at the same time to be considered simultaneous? How long do you wait after one sensors fires to see if the other one is gonna soon enough? How long before you can do that again or right away?

What are these alleged maneuvers? How are they to be executed? Do you need to read the sensors during the action?

And so forth. The more you know before you or we write anything the better.


My project is this : I have my boe-bot and three objects with different shapes. The first shape is only detected by the left ir sensor, the second shape is only detected by the right sensor and the third shape is detected by both sensors. The objects will be placed one at a time (in a random order) on a surface that is the same. The boe-bot will then push the object to a certain place depending on the shape of the object. Then it will go back to the first location (the surface where I place the shapes). I need my robot to define the shape of the object and then execute the maneuvers. Only when the maneuvers are done it should define the shape of the next object. When no object is detected it should stop moving. Thank you very much for your answer it means a lot.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.