I'm trying to use a Arduino Nano the talk to a Arduino Mega (usingNRF24l01), and (among other things) get momentary switches to operate a 12V dc motor Via. a TB6612.
So far I have had success with other aspects of this project in communicating between the 2 Arduinos, and only am having odd results with this aspect of what is a much larger undertaking.
Simply put: I'm trying to run 1 Dc motor, where, when one of 2 buttons on the Transmitter Arduino is pressed, the Dc motor on the Receiver Rotates either Clockwise or Counterclockwise and stops when neither button is pressed.
I have successfully gotten the NRF24's working in al other sketches and the example sketch the sparkfun library is working fine on the Mega running the dc motor from the TB6612 so I know all connections are correct and working as expected.
However, when trying to put the two together nothing works. When I took the motor portion out of the sketch, and just had it try and light and LED (pin 22) the led would stay lit and with the motor portion (sketch below) left in, the motor just spins and does not stop. So I think it might have something to do with it constantly reading the pin as HIGH.
In the sketches below I've only have data sent out for 1 button, figured if I could get one working right I could just copy it for the second with a negative motor value.
Can anyone help point me in the right direction?
Thank you!
Transmitter:
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
#define sentLed 4
#define rightButton 3
#define leftButton 5
RF24 radio(7, 8 ); // CE, CSN
const byte addresses[][6] = {"00001", "00002"};
int buttonValue;
boolean buttonState = 0;
void setup() {
pinMode(sentLed, OUTPUT);
pinMode(rightButton, INPUT_PULLUP);
pinMode(leftButton, INPUT_PULLUP);
radio.begin();
radio.openWritingPipe(addresses[0]); // 00001
radio.openReadingPipe(1, addresses[1]); // 00002
radio.setPALevel(RF24_PA_MIN);
}
void loop() {
delay(5);
radio.stopListening();
buttonState = digitalRead(rightButton);
radio.write(&buttonState, sizeof(buttonState));
}
Recever:
#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
#include <SparkFun_TB6612.h>
#define led 22
#define AIN1 24
#define BIN1 28
#define AIN2 26
#define BIN2 30
#define PWMA 5
#define PWMB 6
#define STBY 9
RF24 radio(7, 8 ); // CE, CSN
const byte addresses[][6] = {"00001", "00002"};
int buttonValue;
const int offsetA = 1;
const int offsetB = 1;
boolean buttonState = 0;
Motor motor1 = Motor(AIN1, AIN2, PWMA, offsetA, STBY);
Motor motor2 = Motor(BIN1, BIN2, PWMB, offsetB, STBY);
void setup()
{
pinMode(22, OUTPUT);
radio.begin();
radio.openWritingPipe(addresses[1]); // 00002
radio.openReadingPipe(1, addresses[0]); // 00001
radio.setPALevel(RF24_PA_MIN);
}
void loop()
{
delay(5);
radio.startListening();
while (!radio.available());
radio.read(&buttonState, sizeof(buttonState));
if (buttonState == HIGH) {
motor1.drive(100);
delay(1000);
}
}
