As bellow im trying to set my TCCR0B to allow the 4kHz frequency mode
but it seems to be stuck on the 490Hz default.
int TriggerPinFWD = 9;// MOSFETS on pin 9
int TriggerPinRVS = 10; // MOSFETS on pin 10
int analogPin = 3; // potentiometer connected to analog pin 3
const int buttonPinFWD= 6; //button in for fwd motion
const int buttonPinRVS= 5; // button in for backwards motion
int LEDPin = 7; //Output for led pin
void setup()
{
//First clear all three prescaler bits:
int prescalerVal = 0x07; //create a variable called prescalerVal and set it equal to the binary number "00000111"
TCCR0B &= ~prescalerVal; //AND the value in TCCR0B with binary number "11111000"
//Now set the appropriate prescaler bits:
int prescalerVall = 2; //set prescalerVal equal to binary number "00000010"
TCCR0B |= prescalerVall; //OR the value in TCCR0B with binary number "00000010"
pinMode(TriggerPinFWD, OUTPUT); // Setting the IO
pinMode(TriggerPinRVS, OUTPUT);
pinMode(LEDPin, OUTPUT);
pinMode(buttonPinFWD, INPUT);
pinMode(buttonPinRVS, INPUT);
pinMode(analogPin, INPUT);
}
void loop()
{
int val = analogRead(analogPin); // read the input pin for PWM
int val2 = digitalRead(buttonPinFWD); //read button switch 1 state
int 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
{
analogWrite(TriggerPinFWD, 0);
}
if (val3 == HIGH)
{
analogWrite(TriggerPinRVS, val / 4); //write a value to Pin 10 if the switch 2 is on
}
else
{
analogWrite(TriggerPinRVS, 0);
}
if (val3 == HIGH || val2 == HIGH)
{
digitalWrite(LEDPin, LOW); //Light standby led
}
else
{
digitalWrite(LEDPin, HIGH); //Light standby led
}
}