Hello everyone, first post here.
I am trying to control the FlightModes with Arduino using a potentiometer by giving a PWM signal to CH5 of the APM2.6 @488 Hz. So it works fine up to here. Here is the simple Code
void setup()
{
TCCR1B = TCCR1B & 0b11111000 | 0x03; // 488 Hz PWM on pin 9
Serial.begin(9600); // Start Serial monitor
pinMode(9,OUTPUT); // PWM - Ch5 output pin
}
void loop()
{
pot_data = analogRead(A7);
pot_data = map(pot_data,0,1023,0,255);
analogWrite(9,pot_data);
Serial.println(pot_data);
}
But, without a potentiometer, I am trying to do it with simple buttons. There is 2. The first one called "CNY" and Second one called "Button" in the code below. So, when I press the button CNY, according to code, it gives high Duty Cycle PWM that change the 6th flight mode in APM. However, when I press the button "BUTTON", Arduino gives almost 0 ( not 0 ) Duty Cycle PWM but the APM does not change the flight mode. How is that possible.
Complete Code Here :
const int cny_in = 12;
int cny_data;
const int button_in = 13;
int button_data;
int pot_data;
void setup()
{
TCCR1B = TCCR1B & 0b11111000 | 0x03; // 488 Hz PWM on pin 9
Serial.begin(9600); // Start Serial monitor
pinMode(9,OUTPUT); // PWM - Ch5 output pin
pinMode(12,INPUT); // CNY State Pin
pinMode(13,INPUT); // Button State Pin
}
void loop()
{
/* pot_data = analogRead(A7);
pot_data = map(pot_data,0,1023,0,255);
analogWrite(9,pot_data);
Serial.println(pot_data); */
cny_data = digitalRead(cny_in);
Serial.print(cny_data);
Serial.print(" ");
button_data = digitalRead(button_in);
Serial.println(button_data);
if(cny_data == 1){
Serial.println("Paket Received");
delay(1000);
Serial.println("1");
delay(1000);
Serial.println("2");
delay(1000);
Serial.println("3");
delay(1000);
Serial.println("4");
delay(1000);
Serial.println("RTL Active - Warning");
delay(5000);
Serial.println("RTL Active - ON");
analogWrite(9,253);
}
if(button_data == 1){
Serial.println("Reset Activated");
delay(1000);
Serial.println("1");
delay(1000);
Serial.println("2");
delay(1000);
Serial.println("Ready");
analogWrite(9,5);
}
delay(150);
}
Replies