Hi guys
I have 2 codes of arduino robot
1: - For obstacle avoidance robot
2: - For line following robot
I just want to merge these codes in this way that when i give specific data like 1 or 2 the robot activates one of these modes
Like:-
If i give 1 the robot activates obstacles avoidance mode
If i give 2 the robot activates line following mode
Plesae suggest and help me i am biggner and i only know basics of arduino programming
The basic principle would be to merge the #includes and global declarations into one file and the contents of each setup() into one setup() function. Resolve any conflicts of pin usage and variable names before you go any further
Put the code from the loop() function of your separate programs in new functions (NOT loop() !) and put them in your new program.
Now, from your new loop() function call either of the new functions when you want their code to run
Likely problems :
Assuming that you have resolved any resource conflicts such as pin numbers, variable names and memory usage
1 - your current code uses delay() for timing so will not return to the new loop() function very often
2 - your current code uses for loops which mean that it does not return to the new loop() function very often
3 - your current code uses while loops which mean that it does not return to the new loop() function very often
Why the emphasis on returning to the new loop() function ? Well, the combined program has to check for user input in order to meet this requirement
when i give specific data like 1 or 2 the robot activates one of these modes
spycatcher2k:
Make both void setup() into one, and make both void loop() in to 2 new functions. In your new void loop(), read a pin or serial or whereever you read 1 or 2, and call the new function withthe action you require.
This means i am calling functions inside functions will this work??
UKHeliBob:
The basic principle would be to merge the #includes and global declarations into one file and the contents of each setup() into one setup() function. Resolve any conflicts of pin usage and variable names before you go any further
Put the code from the loop() function of your separate programs in new functions (NOT loop() !) and put them in your new program.
Now, from your new loop() function call either of the new functions when you want their code to run
Likely problems :
Assuming that you have resolved any resource conflicts such as pin numbers, variable names and memory usage
1 - your current code uses delay() for timing so will not return to the new loop() function very often
2 - your current code uses for loops which mean that it does not return to the new loop() function very often
3 - your current code uses while loops which mean that it does not return to the new loop() function very often
Why the emphasis on returning to the new loop() function ? Well, the combined program has to check for user input in order to meet this requirement
So what i have to do is just seperate these programs into 2 functions like their statements and all their premade functions in another sepertae function??
This means i am calling functions inside functions will this work??
Presumably you call Serial.begin(), pinMode() etc from within setup() which I think answers your question.
So what i have to do is just seperate these programs into 2 functions like their statements and all their premade functions in another sepertae function??
Your code from the existing loop() functions go into new functions in your new program. Put any functions that they call into the new program as well but not inside another function
It would be much easier to give advice if you were to post the two programs here