Hello everyone, so I am making a track robot that is going to drive himself automatically with sensor. I already did the driving part, but my problem is coding the logic. So what i want is having many sensor (SR04) all around the robot to detect the distance with object. So i was like: ok so before that, i need to make my script as future proof as possible. What i want is having a variable (like a byte) storing a number and depending on the number, the robot has to do one of these action: move forward, move backward, turn left, turn right, stop, urgent stop (like if the battery is to low, that part is done). but how do i script that? using a if-else if? or a switch? and it has to do the action base on the variable and at the same time sensor check + logic about navigation. how can i make the robot do all these thing at the same time? i tried to do functions (that do the action of moving) that get call by a switch thing in the Loop(). so yeah, i am pretty lost. and also, the robot as to be able to call other board with sensor to do sensor reading (but thats not for now, i just have to take that in consideration in my scripting)
Please show your most advanced code.
sounds like a sequencer. could just be a list of commands in an array with an index to indicatest the current or next command.
there can be an END command that terminates the sequence. A sequence can be started by simply setting the index to the next command to execute. Each command could be for some amount of time or until some left/center/right obstacle is detected, at which point the index is incremented or maybe the index to set to a different sequence depending detecting a left/center/right obstacle
not sure how you want to use the ultra-sound sensors, is there an obstacle avoidance command where it turns slightly to the left if it detects an object to the right.
here is my code. i didnt included the part about voltage as it's not made by me (sorry its made by Chatgpt, i didnt wanted to make somethings that wont work and explode) `#include <Servo.h>
#include <Wire.h>
#include <Adafruit_INA219.h>
Adafruit_INA219 ina219;
const int ledVoltage = 30;
Servo moteurD;
Servo moteurG;
byte direction;
void setup() {
pinMode(ledVoltage, OUTPUT);
digitalWrite(ledVoltage, LOW);
moteurD.attach(5); // RC1 sur pin 5
moteurG.attach(6); // RC2 sur pin 6
direction = 0;
delay(5000);
//temporairy
direction = 1;
delay(5000);
direction = 2;
delay(5000);
direction = 3;
delay(5000);
direction = 4;
delay(5000);
direction = 0;
delay(5000);
direction = 1;
delay(5000);
direction = 32;
delay(5000);
}
//actions des directions
void stop() {
moteurD.writeMicroseconds(1500);
moteurG.writeMicroseconds(1500);
}
void forward() {
moteurD.writeMicroseconds(2000);
moteurG.writeMicroseconds(2000);
}
void left() {
moteurD.writeMicroseconds(1700);
moteurG.writeMicroseconds(1300);
}
void right() {
moteurD.writeMicroseconds(1300);
moteurG.writeMicroseconds(1700);
}
void backward() {
moteurD.writeMicroseconds(1200);
moteurG.writeMicroseconds(1200);
}
void urgentStop() {
moteurD.writeMicroseconds(1500);
moteurG.writeMicroseconds(1500);
}
void bug() {
moteurD.writeMicroseconds(1500);
moteurG.writeMicroseconds(1500);
}
void loop() {
switch (direction) {
case 0:
stop();
break;
case 1:
forward();
break;
case 2:
left();
break;
case 3:
right();
break;
case 4:
backward();
break;
case 5:
urgentStop();
break;
default:
bug();
}
`
i am using the void Setup to do a combination of action to try to test the action. maybe its that that make all the script dont work
The two things:
- show the full code, including the part from ChatGPT
- put the code inside a code tags
made all by myself:
#include <Servo.h> // On utilise la lib Servo pour générer les signaux RC
#include <Wire.h>
#include <Adafruit_INA219.h>
Adafruit_INA219 ina219;
const int ledVoltage = 30;
Servo moteurD;
Servo moteurG;
byte direction;
void setup() {
pinMode(ledVoltage, OUTPUT);
digitalWrite(ledVoltage, LOW);
moteurD.attach(5); // RC1 sur pin 5
moteurG.attach(6); // RC2 sur pin 6
direction = 0;
delay(5000); //laisse du temps pour le motor driver de se connecter
//temporaire
direction = 1;
delay(5000);
direction = 2;
delay(5000);
direction = 3;
delay(5000);
direction = 4;
delay(5000);
direction = 0;
delay(5000);
direction = 1;
delay(5000);
direction = 32;
delay(5000);
}
//actions des directions
void stop() {
moteurD.writeMicroseconds(1500);
moteurG.writeMicroseconds(1500);
}
void forward() {
moteurD.writeMicroseconds(2000);
moteurG.writeMicroseconds(2000);
}
void left() {
moteurD.writeMicroseconds(1700);
moteurG.writeMicroseconds(1300);
}
void right() {
moteurD.writeMicroseconds(1300);
moteurG.writeMicroseconds(1700);
}
void backward() {
moteurD.writeMicroseconds(1200);
moteurG.writeMicroseconds(1200);
}
void urgentStop() {
moteurD.writeMicroseconds(1500);
moteurG.writeMicroseconds(1500);
}
void bug() {
moteurD.writeMicroseconds(1500);
moteurG.writeMicroseconds(1500);
}
void loop() {
//enclenche les directions
switch (direction) {
case 0:
stop();
break;
case 1:
forward();
break;
case 2:
left();
break;
case 3:
right();
break;
case 4:
backward();
break;
case 5:
urgentStop();
break;
default:
bug();
}
chatgpt part (i know i shouldnt have done that, sorry): is right after the last code:
//sécutité voltage
float voltage = ina219.getBusVoltage_V(); // Tension du bus (V)
if (voltage < 6.0) {
digitalWrite(ledVoltage, HIGH);
direction = 5;
}
}
Take it easy, there is no strict rule to not use a ChatGpt on this forum.
Thank you for using a code tags.
Now please describe, what wrong with your sketch.
What is the purpose of these lines? Do you understand that all this are perfectly useless?
It seems that your code will wait and do nothing for 35 seconds, then enter the first loop with a value of 32 for the direction variable, which will cause bug() to be executed. It will then continue to do repeatedly execute bug() in every iteration of loop(), until the voltage value drops below 6.0; from that point forward, each iteration of the loop will cause urgentStop() to be executed instead.
Is that what you intended? If not, perhaps you can provide a description of what you wish to happen.
yeah so that is to test the driving function. before, it was directly the motor output that were there. these line are suppose to make the robot move for now. when i will add sensor, these line will be deleted. the problem i have with this code is that, when i power on the robot, the robot never move. it maybe its because the Arduino try to do the thing in the Setup() and then, go to the loop(). if that is the problem, well i dont know how to make the robot move for now.
are you using servo to control a wheeled robot?
Yes, it's true.
So, because you change direction in the Setup, but do things in the Loop() - nothing happened.
Move the working with direction variable to the loop
well, no, but my DC motor driver use RC signal. its suppose to be use with a radio transmitter and an emitter. because of that, my Arduino have to send those type of signal. using the servo method work pretty well, maybe not the most optimised, but work ![]()
oh ok, i will try as soon as possible
Your code contains nothing about radio
yes exactly, but i have to write some PWM lenght signal, i dont know what it is. i got this solution on Discord a few days ago.
You are not running DC motors. You are running 360 servos. You need 360 servo commands that can be borrowed from 180 servo library.
ok so after putting the list of instuction at the right place just before the Switch:
void loop() {
direction = 1;
delay(5000);
direction = 2;
delay(5000);
direction = 3;
delay(5000);
direction = 4;
delay(5000);
direction = 0;
delay(5000);
direction = 1;
delay(5000);
direction = 32;
delay(5000);
it still doesnt work. there is 3 possibility i think. Either my switch part doesnt work (i couldnt hope my first switch thing will work), my function part doesnt work (maybe i should put the code in the function directly in the switch) or the Arduino isnt able to read all the code while something is happening (maybe the delay in the instuction that i placed in the loop()?)
What i know for sure, is that with a more basic script with no future possibility, the robot was able to do the 5 basic actions. so that part works 100%
what im doing is sending PWM lenght signal things to a DC motor driver that read those things on 2 channels (i have to send 2 different PWM thing, one by track) and then drive the motor accordingly
Your code says you are driving 360 servos.