I have made a Bluetooth car which works amazingly, now when I want to control it through 433Mhz module it behaves abnormally. Here I have this code:-
#include <RH_ASK.h>
#include <SPI.h> // Not actualy used but needed to compile
RH_ASK driver;
const int lf=9;
const int rf=5;
void setup()
{
Serial.begin(9600);
pinMode(lf, OUTPUT);
pinMode(rf, OUTPUT);
if (!driver.init()) // <<<<<<<<<<<<<<<-------------------- THIS LINE----------------------------------------------------------------
Serial.println("init failed");
}
void loop() {
analogWrite(lf,150);
analogWrite(rf,165);
}
here on uploading this code only right wheel starts moving i.e. 'rf' (at pin 5) and on removing the line "if (!driver.init())" from code, both wheels starts spinning normally. And without this line of code, rf module will not work. There is one more thing that is very freaking is that if I make analogWrite 255 to both pins then all works perfectly. Please help me
What Arduino are you using?
RH_ASK takes over a timer and thus prevents some PWM outputs from working. Which timer it takes over depends on what Arduino and is somewhat under your control.
Arduino uno. What should i do for this?
You use output pins 5 and 9.
According to https://www.arduino.cc/en/Tutorial/SecretsOfArduinoPWM
output pin 5 is on timer 0 (which is used for millis() and delay(...)).
output pin 9 is on timer 1 (which is used by default by RH_ASK)
As is documented in various places, including in RH_ASK.h,
/// The RH_ASK driver uses a timer-driven interrupt to generate 8 interrupts per bit period. RH_ASK
/// takes over a timer on Arduino-like platforms. By default it takes over Timer 1. You can force it
/// to use Timer 2 instead by enabling the define RH_ASK_ARDUINO_USE_TIMER2 near the top of RH_ASK.cpp
That means:
(1) Find RH_ASK.cpp
(2) Save a copy of RH_ASK.cpp as RH_ASK_original.cpp
(3) Edit RH_ASK.cpp, looking for this line:
//#define RH_ASK_ARDUINO_USE_TIMER2
(4) Remove the two slashes at the beginning of the line so that the line starts with #define .
(5) Save the file RH_ASK.cpp
(6) Try again.
An alternative would be to use any of pins 5, 6, 11 and 3, and do not use pins 9 and 10.
I cannot see your project so I do not know which is easiest for you.
Good Luck!
Sorry for late reply. Thanks a lot vaj you solved my problem. Now it works as I want. Just uncommented those lines and all works just perfectly.