Hello, I've just finished setting up a basic motor control circuit (2 Unos - one has a RF24 radio and a controller, the other has another RF24 and a L298 motor driver with 2 motors connected).
everything is set up correctly, and the code works, but something weird has happened.
this is the code for the motor operating Uno:
#include <nRF24L01.h>
#include <printf.h>
#include <RF24.h>
#include <RF24_config.h>
#include <SPI.h>
RF24 radio(7, 8);
byte addresses[][6] = {"1Node", "2Node"};
#define ENA 2
#define ENB 4
#define IN1 3
#define IN2 5
#define IN3 6
#define IN4 9
void setup() {
radio.begin();
radio.setPALevel(RF24_PA_LOW);
radio.openWritingPipe(addresses[0]);
radio.openReadingPipe(1, addresses[1]);
motorBegin();
Serial.begin(9600);
}
void loop() {
char package = getPackage(); //recieve data transmission
char in1, in2, in3, in4;
Unpack(package,&in1,&in2,&in3,&in4); //decode the data pack into the corresponding variables
// print to screen //
Serial.print("in1 = ");
Serial.print(int(in1));
Serial.print(" in2 = ");
Serial.print(int(in2));
Serial.print(" in3 = ");
Serial.print(int(in3));
Serial.print(" in4 = ");
Serial.println(int(in4));
OperateMotors(in1,in2,in3,in4);
}
void motorBegin(){
pinMode(ENA,OUTPUT);
pinMode(ENB,OUTPUT);
pinMode(IN1,OUTPUT);
pinMode(IN2,OUTPUT);
pinMode(IN3,OUTPUT);
pinMode(IN4,OUTPUT);
digitalWrite(ENA,HIGH);
digitalWrite(ENB,HIGH);
}
char getPackage() {
char package = 0;
radio.startListening();
if (radio.available()) {
while (radio.available()) { // While there is data ready
radio.read( &package, sizeof(package) );
}
radio.stopListening(); // First, stop listening so we can talk
radio.write( package, sizeof(package) ); // Send the final one back.
radio.startListening();
}
return package;
}
void Unpack(char package, char* in1, char* in2, char* in3, char* in4){
*in4 = package/8;
package -= *in4*8;
*in3 = package/4;
package -= *in3*4;
*in2 = package/2;
package -= *in2*2;
*in1 = package;
return;
}
void OperateMotors(int in1, int in2, int in3, int in4){
if(in1 == 1) digitalWrite(IN1,HIGH);
if(in1 == 0) digitalWrite(IN1,LOW);
if(in2 == 1) digitalWrite(IN2,HIGH);
if(in2 == 0) digitalWrite(IN2,LOW);
if(in3 == 1) digitalWrite(IN3,HIGH);
if(in3 == 0) digitalWrite(IN3,LOW);
if(in4 == 1) digitalWrite(IN4,HIGH);
if(in4 == 0) digitalWrite(IN4,LOW);
}
when this code runs - everything is well - the motors turn as expected, but, if i erase the "print to screen" part (or the "Serial.begin(9600)" line) - the motors seem to run on a very small current, not enough to move. (if i erase the print to screen partially the motors seem to get a part of the current).
why does this happen? what's the connection between a serial print and the current sent to the motors?
thanks!