I have started to make an engine controller, but at the moment I am facing some PWM problems.
Board : ARDUINO UNO
Inputs:
Signal from potentiometer ( throttle )
3 momentary pushbuttons. ( gear position )
Outputs:
1 servo for throttle
1 servo for gear position
3 leds for indication of gear position.
I have a suspicion that the servo.h are interference with the normal use of PWM pins (analogWrite). In the program only pin 11 seems to works as I expected. Pin 9 and 10 are just flickering. When I remove all program there are belonging to the servo.h the IF ELSE statements are working as expected. Therefor I also know that my hardware is working well. It must be a software problem (a bug)
Have any of you had similar problems?
Is there a known limitation of servo.h I don’t know about?
Have I made any obviously mistakes ?
Best regards
Søren
#include <Servo.h>
int gearPos = 50;
const int pinfrem = 2;
const int pinneu = 3;
const int pinbak = 4;
int val1;
int val2;
int val3;
int brightness_low;
int brightness_high;
Servo myservo_gear;
Servo myservo_gas;
//int ledpinfrem = 9;
//int ledpinneu = 10;
//int ledpinbak = 11;
int potpin = 0; // analog pin used to connect the potentiometer
int val; // variable to read the value from the analog pin
void setup() {
// Serial.begin(9600);
myservo_gear.attach(6);
myservo_gas.attach(5);
pinMode(pinfrem,INPUT);
pinMode(pinneu,INPUT);
pinMode(pinbak,INPUT);
//pinMode(9,OUTPUT);
//pinMode(10,OUTPUT);
//pinMode(11,OUTPUT);
}
void loop() {
val = analogRead(potpin); // reads the value of the potentiometer (value between 0 and 1023)
val = map(val, 0, 1023, 30, 140 );
val1 = digitalRead(pinfrem);
val2 = digitalRead(pinneu);
val3 = digitalRead(pinbak);
brightness_high = 250;
brightness_low = 2;
if (val1 == HIGH) {gearPos = 50 ;analogWrite(9, brightness_high);analogWrite(10, brightness_low);analogWrite(11, brightness_low);}
else if (val2 == HIGH) {gearPos = 90 ;analogWrite(9, brightness_low);analogWrite(10, brightness_high);analogWrite(11, brightness_low);}
else if (val3 == HIGH) {gearPos = 130;analogWrite(9, brightness_low);analogWrite(10, brightness_low);analogWrite(11, brightness_high);}
brightness_high = 250;
brightness_low = 2;
delay(2);
myservo_gear.write(gearPos);
myservo_gas.write(val);
}