Hi im trying to write a code that will output a variable PWM to one pin if a switch is on. and to another pin if another switch is on.
when neither switches are on it should light a standby led.
int TriggerPinFWD = 9;// MOSFETS on pin 9
int TriggerPinRVS = 10; // MOSFETS on pin 10
int analogPin = 3; // potentiometer connected to analog pin 3
int val = 0;// variable to store the read value
int buttonPinFWD= 1; //button in for fwd motion
int buttonPinRVS= 2; // button in for backwards motion
int val2 = 0; //variable for FWD switch
int val3 = 0; //variable for RVS switch
int LEDPin = 0; //Output for led pin
void setup()
{
pinMode(TriggerPinFWD, OUTPUT); // Setting the IO
pinMode(TriggerPinRVS, OUTPUT);
pinMode(LEDPin, OUTPUT);
pinMode(buttonPinFWD, INPUT);
pinMode(buttonPinRVS, INPUT);
TCCR2B = (TCCR2B & 0xF8) | 2;
}
void loop()
{
val = analogRead(analogPin); // read the input pin for PWM
val2 = digitalRead(buttonPinFWD); //read button switch 1 state
val3 = digitalRead(buttonPinRVS); //read button switch 2 state
if (val2 == HIGH)
{
analogWrite(TriggerPinFWD, val / 4); //write a value to Pin 9 if the switch 1 is on
}
else
{
digitalWrite(LEDPin, HIGH); //Light standby led
}
if (val3 == HIGH)
{
analogWrite(TriggerPinRVS, val / 4); //write a value to Pin 10 if the switch 2 is on
}
else
{
digitalWrite(LEDPin, HIGH); //Light standby led
}
}
The code seems fine to me but i wanted a second set of eyes and since im the only person doing this using arduino and not pic at my uni i thought i'd ask here.