Hi all,
I'm trying to control the PWM on my Arduino by pressing pushbuttons. My code is as follows:
int turnleft = A0;
int turnright = A1;
int returnhome = A2;
int val1 = 0;
int val2 = 0;
int val3 = 0;
void setup()
{
pinMode(A0, INPUT);
pinMode(A1, INPUT);
pinMode(A2, INPUT);
pinMode(10, OUTPUT);
}
void loop()
{
val1 = digitalRead(A0);
val2 = digitalRead(A1);
val3 = digitalRead(A2);
if(val1 == HIGH)
{
digitalWrite(10, HIGH);
delay(3); //Approimately 5% Duty Cycle at 50Hz
digitalWrite(10, LOW);
delay(17);
}
else
{
PORTC = B000000;
}
if(val2 == HIGH)
{
digitalWrite(10, HIGH);
delay(2); //Approimately 5% Duty Cycle at 50Hz
digitalWrite(10, LOW);
delay(18);
}
else
{
PORTC = B000000;
}
if(val3 == HIGH)
{
digitalWrite(10,HIGH);
delay(1);
digitalWrite(10, LOW);
delay(19);
}
else
{
PORTC = B000000;
}
}
From what I understand I'm doing something called "bit banging?" All I need to do is get 50Hz out, (Which it is doing), but I want to vary the duty cycle from 5%, 10%, and 15% by pressing pushbuttons.
When I try and use this code, I have pin 10 hooked up on the oscilloscope, but the duty cycle is varying between the three rapidly, as if all the if statements are true.
I'm not sure what to do at this point, or what I'm doing wrong. Any help would be appreciated.
Thank you.