TIA anyone.
I worked on this project a while back and just got some new stuffs to put on it and a new motor controller and motors.
The problem i am having is the IF ELSE seems to be triggering the correct function ( tho with some odd PWM values i have no idea why they are going neg now) as well as the last function ( motorStop).
This is causing the motor driver to try and start and stop at the same time. could some one have a look and see why. TIA
Inside the (Drive section in the F/R and L/R code)
//#include <Servo.h>
#include <Mirf.h>
#include <MirfHardwareSpiDriver.h>
#include <MirfSpiDriver.h>
#include <nRF24L01.h>
#include <SPI.h>
//**************Setup # servos to use*********
//Servo myservo0;
//Servo myservo1;
// Servo myservo#;
//**************End Servo Setup***************
//***** set vars for each pots/sensor reading from transmitter
int val0; // servo 1
int val1; // servo 2
int val2; // servo 3
int val3; // servo 4
//int oldVal0;
int DIR_A = 12; //L298 DIR A Digital High/Low = left/right
int PWM_A = 3; //L298 PWM A Analog
int ENA_A = 9; //L298 Enable A Digital Hight/low on/off
int DIR_B = 13; //L298 DIR A Digital High/Low = left/right
int PWM_B = 11; //L298 PWM A Analog
int ENA_B = 8; //L298 Enable A Digital Hight/low on/off
//*********************************************
void setup(){
Serial.begin(57600);
pinMode(DIR_A, OUTPUT);
pinMode(PWM_A, OUTPUT);
pinMode(ENA_A, OUTPUT);
pinMode(DIR_B, OUTPUT);
pinMode(PWM_B, OUTPUT);
pinMode(ENA_B, OUTPUT);
//************ Pin set for servos*************
// myservo0.attach(4);
// End Pinset for servos
//**************Start Transiever (NRF24L01) config**************
Mirf.cePin = 48;
Mirf.csnPin = 49;
// Mirf.cePin = 7;
// Mirf.csnPin = 9;
Mirf.spi = &MirfHardwareSpi;
Mirf.init();
Mirf.setRADDR((byte *)"reci1");
Mirf.payload = 4 * sizeof(byte);
Mirf.config();
//**************End Transiever (NRF24L01) config**************
Serial.println("Beginning..."); // print somthing once to Serial to know your up
}
void loop(){
//+++++++++++Start data collection from transciever+++++++++
byte data[Mirf.payload];
if(Mirf.dataReady()){
do{
Mirf.getData(data);
//*********** Start array to collect pot/sensor data *********
val0 = data[0]; //Pot 1 on Transmitter
val1 = data[1]; //Pot 2 on Transmitter
val2 = data[2]; //Pot 1 on Transmitter
val3 = data[3]; //Pot 2 on Transmitter
//*********** End array to collect pot/sensor data *********
//**********Start Loop to print array to Serial Monitor
/*
int i;
for (i = 0; i < 4; i = i + 1) {
Serial.println(data[i], DEC);
}
// */
//**********End Loop to print array to Serial Monitor
delay(10);
}while(!Mirf.rxFifoEmpty());
}
//++++++++ END data collection from transciever++++++++++++
// int delta0 = abs(val0 - oldVal0);
// if(delta0 > 2){
// myservo0.write(val0);
// Serial.print("Servo:");
// Serial.println(val0);
// }
//_________________________Drive___________________________
//________________Forward / Reverse_____________
if(val1 < 110 ){
motorReverse();
Serial.print("REV,Val1: ");
Serial.println(val1);
}
else if(val1 > 135){
motorForward();
Serial.print("FOR,Val1: ");
Serial.println(val1);
}
else{
motorStop();
Serial.print("STOP,Val1: ");
Serial.println(val1);
}
//______________Left / Right________________
if(val2 < 110 ){
motorLeft();
Serial.print("LEFT,Val2: ");
Serial.println(val2);
}
else if(val2 > 135){
Serial.print("RIGHT,Val2: ");
Serial.println(val2);
motorRight();
}
else{
Serial.print("STOP,Val2: ");
Serial.println(val2);
motorStop();
}
//_____________________END DRIVE___________________
} // end of program
//***********************
//*************** External funct*********************
int motorForward() {
val1 = map(val1, 110, 0, 100, 254);
digitalWrite(ENA_A, LOW);
digitalWrite(DIR_A, LOW);
analogWrite(PWM_A, val1);
digitalWrite(ENA_B, LOW);
digitalWrite(DIR_B, LOW);
analogWrite(PWM_B, val1);
// Serial.print("Forward: ");
// Serial.println(val1);
}
int motorReverse() {
val1 = map(val1, 135, 254, 0, 254);
digitalWrite(ENA_A, LOW);
digitalWrite(DIR_A, HIGH);
analogWrite(PWM_A, val1);
digitalWrite(ENA_B, LOW);
digitalWrite(DIR_B, HIGH);
analogWrite(PWM_B, val1);
// Serial.print("Reverse: ");
//Serial.println(val1);
}
int motorStop(){
digitalWrite(ENA_A, LOW);
digitalWrite(DIR_A, LOW);
analogWrite(PWM_A, 0);
digitalWrite(ENA_B, LOW);
digitalWrite(DIR_B, LOW);
analogWrite(PWM_B, 0);
// Serial.print("Stop: ");
// Serial.println(val1);
}
int motorLeft(){
val2 = map(val1, 110, 0, 100, 254);
digitalWrite(ENA_A, LOW);
digitalWrite(DIR_A, HIGH);
analogWrite(PWM_A, val2);
digitalWrite(ENA_B, LOW);
digitalWrite(DIR_B, LOW);
analogWrite(PWM_B, val2);
// Serial.print("Left: ");
// Serial.println(val0);
}
int motorRight(){
val2 = map(val1, 135, 254, 0, 254);
digitalWrite(ENA_A, LOW);
digitalWrite(DIR_A, LOW);
analogWrite(PWM_A, val0);
digitalWrite(ENA_B, LOW);
digitalWrite(DIR_B, HIGH);
analogWrite(PWM_B, val2);
// Serial.print("Right: ");
// Serial.println(val0);
}
This is the output i am getting
---------------------
Up on left stick
---------------------
REV,Val1: -503
STOP,Val2: 129
FOR,Val1: -101
STOP,Val2: 129
**********************
---------------------
Down on left stick
------------------------
REV,Val1: -471
STOP,Val2: 129
FOR,Val1: -86
STOP,Val2: 129
*************************
-------------------------
Right on right stick
--------------------------
STOP,Val1: 127
RIGHT,Val2: 253
STOP,Val1: 127
LEFT,Val2: 77
STOP,Val1: 127
RIGHT,Val2: 253
****************************
-------------------------
Left on right stick
-------------------------
LEFT,Val2: 77
STOP,Val1: 127
LEFT,Val2: 77
STOP,Val1: 127
LEFT,Val2: 77
*************************
Hope some one sees something simple there.