Hi,
I'm attempting to teach myself electronics and programing, and am having a great time. I've decided to try and build an r/c controlled robot and have set about trying to learn what is needed.
Through cobbling together bits of code and trial and error I have managed to put together a sketch that uses interrupts to interpret the receiver. This all works OK.
I then used the map function to change the signal from the transmitter to an int that can be used to anologWrite PWM. I'm currently This is where it falls down. Serial.Print shows the values are changing correctly however when doing the analog.write the LED is either fully on or off (On halfway above the range, off below). Checking with a multimeter confirms 0 volts then 4.8 with nothing in between. Some research suggests this is the interrupts causing problems with the timers for PWM however they're a bit too technical for me at this point.
Am I aproaching this in the right way. Have I tried to run before I can walk. All help greatly appreciated. Here's the code:
/*
Code will setup 6 interupts to monitor pwm inputs sent by 6 channel R/C reciever
right horisontal int 0 pin 2
right vertical int 1 pin 3
left horisontal int 2 pin 21
left vertical int 3 pin 20
swb int 4 pin 19
swa int 5 pin 18*/
//volatile variables will be updated by interupts
volatile int pwm_value0 = 0; /*the pwm data will be written to these 6 */
volatile int pwm_value1 = 0;
volatile int pwm_value2 = 0;
volatile int pwm_value3 = 0;
volatile int pwm_value4 = 0;
volatile int pwm_value5 = 0;
volatile int prev_time = 0;
int LED0 = 25;
int LED1 = 52;
int LED2 = 51;
int LED3 = 50;
int LED4 = 49;
int LED5 = 48;
void setup() {
Serial.begin(9600);
// when interupt detected call function
attachInterrupt(0, rising0, RISING);
attachInterrupt(1, rising1, RISING);
attachInterrupt(2, rising2, RISING);
attachInterrupt(3, rising3, RISING);
attachInterrupt(4, rising4, RISING);
attachInterrupt(5, rising5, RISING);
pinMode (LED0, OUTPUT);
pinMode (LED1, OUTPUT);
pinMode (LED2, OUTPUT);
pinMode (LED3, OUTPUT);
pinMode (LED4, OUTPUT);
pinMode (LED5, OUTPUT);
}
void loop() {
volatile int LED0Brightness = map (pwm_value0, 1000, 2000, 0, 255);
Serial.print ("LED0 Level:");
Serial.println (LED0Brightness);
Serial.print ("pwm_value0:");
Serial.println (pwm_value0);
analogWrite (LED0, LED0Brightness);
}
void rising0() {
attachInterrupt(0, falling0, FALLING);
prev_time = micros();
}
void falling0() {
attachInterrupt(0, rising0, RISING);
pwm_value0 = micros()-prev_time;
// Serial.println(pwm_value0);
}
void rising1() {
attachInterrupt(1, falling1, FALLING);
prev_time = micros();
}
void falling1() {
attachInterrupt(1, rising1, RISING);
pwm_value1 = micros()-prev_time;
// Serial.println(pwm_value1);
}
void rising2() {
attachInterrupt(2, falling2, FALLING);
// prev_time = micros();
}
void falling2() {
attachInterrupt(2, rising2, RISING);
pwm_value2 = micros()-prev_time;
// Serial.println(pwm_value2);
}
void rising3() {
attachInterrupt(3, falling3, FALLING);
// prev_time = micros();
}
void falling3() {
attachInterrupt(3, rising3, RISING);
pwm_value2 = micros()-prev_time;
// Serial.println(pwm_value3);
}
void rising4() {
attachInterrupt(4, falling4, FALLING);
// prev_time = micros();
}
void falling4() {
attachInterrupt(4, rising4, RISING);
pwm_value2 = micros()-prev_time;
// Serial.println(pwm_value4);
}
void rising5() {
attachInterrupt(5, falling5, FALLING);
// prev_time = micros();
}
void falling5() {
attachInterrupt(5, rising5, RISING);
pwm_value2 = micros()-prev_time;
// Serial.println(pwm_value5);
}