hey yall
so im working on a lil tank project and ive hit a snag thats left me lost
ive got a mega with an on off on switch and a pot for speed and motor direction control. using a nano with l298n motor driver. both use nrf24l01 transceivers.
so all the code and connections seem good yet the motors dont run consistently. ive checked that all the components are good and have solid connections. ive check the serial monitor to verify that the desired data is being sent and received and all checks out i genuinely have no idea what the issue is.
thanks for any help ive attached the code for both the tank and the controller.
#include <nRF24L01.h>
#include <printf.h>
#include <RF24.h>
#include <RF24_config.h>
#include <SPI.h>
RF24 radio(6, 7);
byte address[6] = "00001";
byte pos1 = 2;
byte pos2 = 3;
int pot = A0;
void setup() {
pinMode(pos1, INPUT_PULLUP);
pinMode(pos2, INPUT_PULLUP);
Serial.begin(9600);
radio.begin();
radio.openWritingPipe(address);
radio.setPALevel(RF24_PA_MIN);
radio.stopListening();
delay(1000);
}
void loop() {
int spd = analogRead(pot);
spd = map(spd, 0, 1023, 60, 254);
byte state1 = digitalRead(pos1);
byte state2 = digitalRead(pos2);
radio.write(&spd, sizeof(spd));
radio.write(&state1, sizeof(state1));
radio.write(&state2, sizeof(state2));
Serial.print(spd);
Serial.print(state1);
Serial.print(state2);
}
#include <SPI.h>
#include <nRF24L01.h>
#include <printf.h>
#include <RF24.h>
#include <RF24_config.h>
RF24 radio(4, 5);
byte address[6] = "00001";
int m1 = 6; // motor 1 enable
int in1 = 8; // motor 1 input 1
int in2 = 7; // motor 1 input 2
int m2 = 3; // motor 2 enable
int in3 = 5; // motor 2 input 1
int in4 = 4; // motor 2 input 2
void setup() {
pinMode(m1, OUTPUT); // set pin as output
pinMode(in1, OUTPUT); // set pin as output
pinMode(in2, OUTPUT); // set as output
pinMode(m2, OUTPUT); // set as output
pinMode(in3, OUTPUT); // set as output
pinMode(in4, OUTPUT); // SET AS output
Serial.begin(9600);
radio.begin();
radio.openReadingPipe(0, address);
radio.setPALevel(RF24_PA_MIN);
radio.startListening();
delay(1000);
}
void loop() {
byte state1;
byte state2;
int spd;
if (radio.available()) {
radio.read(&state1, sizeof(state1));
radio.read(&state2, sizeof(state2));
radio.read(&spd, sizeof(spd));
Serial.print(state1);
Serial.print(state2);
Serial.print(spd);
if (state1 == LOW) { // if swith in forward postion run motor forward a set speed
digitalWrite(in1, HIGH);
digitalWrite(in2, LOW);
analogWrite(m1, spd); // set motor 1
Serial.print("forward");
}
if (state2 == LOW) { // if switch in rear position run motor backwards at set speed
digitalWrite(in1, LOW);
digitalWrite(in2, HIGH);
analogWrite(m1, spd);
Serial.print("back");
Serial.print(spd);
}
else { // motor off, mid position
analogWrite(m1, 0);
analogWrite(m2, 0);
}
}
}
mcat_controller.ino (1.64 KB)
sketch_may11c.ino (802 Bytes)
Without you clearly explaining what "snag" you hit, how can we come with a solution to it?
The motors don't run consistently. Sometimes they run when you trigger the switch sometimes they don't. They're sending and receiving signal and everything should in theory work but dosent.
Trigger what switch?
How do you know they're actually sending/receiving the signal?
Because I checked via serial monitor to see what info was being sent and received. I covered both you're questions in the post.
Are all connection soldered wires or screw terminals?
Paul
The only screw terminals are the power and motor connection on the l298n. Those all the hookups seem good. I ran other simple code to check all components worked and everything was bueno. The switch registers bother position accurately, the pot works, motors and boards are all good. And the code seems solid to me. Going crazy lol
zjett23:
Because I checked via serial monitor to see what info was being sent and received.
So you mean when sending a command over the radio you always see the messages "forward" and "back" appear, yet sometimes the motors react and sometimes not?
I covered both you're questions in the post.
You have to be more explicit. Remember we can't see what you're doing or what you have on your desk.
Yes exactly. The mega picks up the correct high/low signal and the nano does get it but sometimes the motors run sometimes not. It's not consistent either, it might work 15 times in a row the stop.
That points to a wiring issue. The radio transmission obviously works, and the same block that prints those messages sets the output pins. You will be able to measure the appropriate voltages at the pins of your Arduino.
That's the weird part. If I run code to just turn on the motors they work just fine. It's only the actual code I need that's causing the intermittent behavior I think.
If I hook the switch to the nano runs fine.
Could the problem be that you treat state1 and state2 differently?
Paul
I don't think that's the issue. If it we're only one direction that didn't work but it works forward and back just not consistent
Just looking at your code again - the error is presumably in the second piece of code, and indeed there is one major issue:
if (state1 == LOW) { // if swith in forward postion run motor forward a set speed
digitalWrite(in1, HIGH);
digitalWrite(in2, LOW);
analogWrite(m1, spd); // set motor 1
Serial.print("forward");
}
if (state2 == LOW) { // if switch in rear position run motor backwards at set speed
digitalWrite(in1, LOW);
digitalWrite(in2, HIGH);
analogWrite(m1, spd);
Serial.print("back");
Serial.print(spd);
}
else { // motor off, mid position
analogWrite(m1, 0);
analogWrite(m2, 0);
}
That second if should be an else if.
In the code as is, if state1 is LOW, the motor is set to forward, but if state2 is HIGH the speed on m1 and m2 is set to 0. Also interesting that only there you set pin m2.
That doesn't explain intermittent behaviour, it should simply not let it run forward at all.
You may also want to add a clause that takes care of both state1 and state2 being LOW at the same time.
They can't both go low.
My understanding is that my code ignores the switch going high. That if the forward position goes low the motor should spin one way and vice versa but I can try adding something like if (state1 == LOW && state2 == HIGH)
but I think that would be redundant since it's an on off on toggle if one position is low the other should inherently be high with the middle both being high
The problem is that if state1 == LOW (so that means state2 == HIGH), both m1 and m2 are set to 0. Your motor should never run forward, at all. Only backward.
M1 and m2 are set by the pot not the switch. The signal for the switch is only for track direction
In the code you posted, m1 and m2 are both set to 0 when state2 is HIGH. Don't believe me? Read your code. Carefully.
You were right, a slight revision fixed the problem. Guess I over looked that since I figured it would cause it not to work at all but hey it works now and that's what matters
Thanks