Hello every one , im making object avoiding car
also im trying to make another function which is line following
what are the ways to switch between them ?
i mean using only avoiding function then turning off avoiding and turn on line following
is it related to programming where i can upload both codes together or its something related to electrics and wiring to make the switch .
i will appreciate it if someone can guide me for the right answer
im using Arduino uno + L298N motor shield
thanks to everyone who is helping us getting better using arduino .
Just depends on how much code is involved for each.
I'd install a switch and track the state of the switch.
When the switch changed state, I'd call an init function for the corresponding mode and in loop I'd keep all the code split by an if/else dividing it between the two modes.
Finally, I'd be sure to do practically nothing in the setup function as it is only called once.
thanks for helping out
its just 2 codes, i think its hard for me even imagining merging 2 codes together for 2 separated functions ,because im a very beginner in arduino and coding especially. so i might do some electrical switching and i might keep uploading the codes separately when i need to use the other function.
thanks again for reaching out ,very appreciated
.
It is not possible to do 2 things at exactly the same time, but because the Arduino is quite fast you can do things one after the other, and it will appear as they are happening simultaneously.
You put this code in loop() and it will keep processing both functions. For example...
void loop()
{
if (checkForCollision())
avoidCollisionAction();
if (checkIfMovedOffLine())
moveBackToLineAction();
}
I just whipped this up and I did not test it, but this will basically let you run two programs on the same Arduino based on the state of a switch.
byte mode_switch_state;
byte mode_switch_pin = 2; // Change 2 to the correct pin number, connect the switch the pin and to ground
void switch_setup() {
mode_switch_state = digitalRead(mode_switch_pin);
if (mode_switch_state == HIGH){
collision_setup();
} else {
follower_setup();
}
}
void collision_setup() {
// Do the setup for your collision mode here
}
void follower_setup() {
// Do the setup for your follower mode here
}
void collision_loop() {
// The loop function for collision mode
}
void follower_loop() {
// The loop function for follower mode
}
void setup() {
pinMode(mode_switch_pin, INPUT_PULLUP);
switch_setup();
}
void loop() {
if (mode_switch_state != digitalRead(mode_switch_pin)) {
// The switch has just changed position
// So the new mode's setup needs to be called
switch_setup();
}
if (mode_switch_state == HIGH){
collision_loop();
} else {
follower_loop();
}
}
you mean like setting a timer for the first function then switch to the other ? or giving If order like its going on the line then when sensor detects something it switches for the second code
Put these checks in loop to run all the time. If they detect something that is a problem then you call the appropriate routine... then you go straight back to loop and keep checking.