Can somebody help me with this code? I will use my Z,Q,S,D keys for steering the drone and 1,2,3,4,5 and 6 for controlling the speed.
But i can't make the code. Plz give some support.
besturing_drone_v1_1.ino (2.39 KB)
Can somebody help me with this code? I will use my Z,Q,S,D keys for steering the drone and 1,2,3,4,5 and 6 for controlling the speed.
But i can't make the code. Plz give some support.
besturing_drone_v1_1.ino (2.39 KB)
MichielRoux:
Can somebody help me with this code? I will use my Z,Q,S,D keys for steering the drone and 1,2,3,4,5 and 6 for controlling the speed.
But i can't make the code. Plz give some support.
int Motor1 = 11; // Pin 11 - Motor1
int Motor2 = 10; // Pin 10 - Motor2
int Motor3 = 9; // Pin 9 - Motor3
int Motor4 = 6; // Pin 6 - Motor4
//pin 11,10,9 en 6 zijn PWM pinnen.
char val; // variabelen voor data van de seriële poort te ontvangen voor richting.
char input; // variabelen voor data van de seriële poort te ontvangen voor snelheid.
void setup() {
pinMode(Motor1, OUTPUT);
pinMode(Motor2, OUTPUT);
pinMode(Motor3, OUTPUT);
pinMode(Motor4, OUTPUT);
Serial.begin(9600); // Start seriële communicatie op 9600bps.
Serial.println("Welcome ArduDrone");
Serial.println("Controles");
Serial.println("E = Start");
Serial.println("Z = Forward");
Serial.println("S = Back");
Serial.println("Q = Left");
Serial.println("D = Right");
}
// startdrone
void start_drone(int Speed) {
analogWrite(Motor1,Speed);
analogWrite(Motor2,Speed);
analogWrite(Motor3,Speed);
analogWrite(Motor4,Speed);
}
// Fordward action
void go_forward(int Speed) {
analogWrite(Motor1,Speed);
analogWrite(Motor2,Speed);
analogWrite(Motor3,Speed);
analogWrite(Motor4,Speed);
}
// Reverse action
void go_reverse(int Speed) {
analogWrite(Motor1,Speed);
analogWrite(Motor2,Speed);
analogWrite(Motor3,Speed);
analogWrite(Motor4,Speed);
}
// Left action
void go_left(int Speed) {
analogWrite(Motor1,Speed);
analogWrite(Motor2,Speed);
analogWrite(Motor3,Speed);
analogWrite(Motor4,Speed);
}
// Right action
void go_right(int Speed) {
analogWrite(Motor1,Speed);
analogWrite(Motor2,Speed);
analogWrite(Motor3,Speed);
analogWrite(Motor4,Speed);
}
// Read serial port and perform command
void performCommand(int Speed) {
if (Serial.available()) {
val = Serial.read();
}
if (val == 'e') {
start_drone();
}
if (val == 'z') {
go_forward();
}
if (val == 's') {
go_reverse();
}
if (val == 'd') {
go_right();
}
if (val == 'q') {
go_left();
}
}
void powerCommand() {
if (Serial.available()) {
input = Serial.read();
if (input == '0'){
int Speed = 0;
}
if (input == '1'){
int Speed = 153;
}
if (input == '2'){
int Speed = 168;
}
if (input == '3'){
int Speed = 183;
}
if (input == '4'){
int Speed = 198;
}
if (input == '5'){
int Speed = 213;
}
if (input == '6'){
int Speed = 228;
}
}
}
void loop() {
powerCommand();
performCommand();
}
What happens to the drone when you run the code ?
One thing that I notice straight away is that each of the functions that I assume are supposed to make the drone move make the 4 motors run at the same speed. In any case, the code does not compile because arguments are not being passed to functions. Where did you get the code from ?
Have you got a link to the drone that you are using ?
I have made the code my self. Its prototype now i only need to know how i can youse the int speed in the voids for the motors.
You do not have any voids. What you have are functions that do not return a value, hence they are declared void in the same way as setup() and loop().
To use a function that needs parameters passed to it you put the parameters in the brackets after the function name and then define the function as receiving the parameters. Something like this
//code for a function that expects to receive an int as a parameter but does not return a value
void aFunction(int aParameter)
{
Serial.println(aParameter);
}
and the code to call the function
aFunction(123);
or
int aNumber = 456;
aFunction(aNumber);
Did you not think to test any part of your code before getting this far ?
You have two functions reading values from the serial port and testing them for specific values that represent known commands. Each function only handles a subset of the commands and you have no control over which of the functions will receive a given command, so there's a good chance it will be discarded. I suggest you write a single function to handle received bytes and use that to handle all the commands. Put in some print statements to show the commands have been received and recognised correctly before you worry about how to implement them.