Go Down

Topic: Need help with IF ELSE trigger for Nrf24L01+ Arduino Motor Shield (Read 852 times) previous topic - next topic

Cyric

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)
Code: [Select]

//#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
Code: [Select]

---------------------
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.

James C4S

Please make use of the IDEs auto format function and repost the code.  (CTRL-T)
Capacitor Expert By Day, Enginerd by night.  ||  Personal Blog: www.baldengineer.com  || Electronics Tutorials for Beginners:  www.addohms.com

PaulS

You are printing values AFTER the subroutines have mucked with them. That tells you nothing. Print them BEFORE calling the subroutines.

The value printed after motorReverse() looks screwy. After this code:
Code: [Select]
int motorReverse() {
  val1 = map(val1, 135, 254, 0, 254);

val1 is -503. This means that the input was not in the range 135 to 254, which is clear from the fact that you call this function only when val1 is less than 110.

You need to explain the screwy mapping.

Cyric


Please make use of the IDEs auto format function and repost the code.  (CTRL-T)

**Fixed some errors..

this better?
Code: [Select]

//#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();
  }
  else if(val1 > 135){
    motorForward();
  }
  else{
    motorStop();
  }
  //______________Left / Right________________ 


  if(val2 < 110 ){
    motorLeft();
  }
  else if(val2 > 135){
    motorRight();
  }
  else{
    motorStop();
  }
  //_____________________END DRIVE___________________


} // end of program
//***********************


//*************** External funct*********************
int motorForward() {

  val1 = map(val1, 135, 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.println("Forward: ");
//  Serial.println(val1);

}

int motorReverse() {
  val1 = map(val1, 110, 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.println("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.println("Stop: ");
// Serial.println(val1);

}


int motorLeft(){
  val2 = map(val2, 135, 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.println("Left: ");
// Serial.println(val0);
}

int motorRight(){
  val2 = map(val2, 110, 254, 0, 254);
  digitalWrite(ENA_A, LOW);
  digitalWrite(DIR_A, LOW);
  analogWrite(PWM_A, val2);
  digitalWrite(ENA_B, LOW);
  digitalWrite(DIR_B, HIGH);
  analogWrite(PWM_B, val2);
  Serial.println("Right: ");
// Serial.println(val0);
}




Cyric


You are printing values AFTER the subroutines have mucked with them. That tells you nothing. Print them BEFORE calling the subroutines.

The value printed after motorReverse() looks screwy. After this code:
Code: [Select]
int motorReverse() {
  val1 = map(val1, 135, 254, 0, 254);

val1 is -503. This means that the input was not in the range 135 to 254, which is clear from the fact that you call this function only when val1 is less than 110.

You need to explain the screwy mapping.


in loop
Code: [Select]
if(val1 < 110 ){
    motorReverse();
  }
  else if(val1 > 135){
    motorForward();
}
  else{
    motorStop();
  }

Forward now
Code: [Select]

int motorForward() {

  val1 = map(val1, 135, 0, 100, 254);

Reverse now
Code: [Select]
int motorReverse() {
  val1 = map(val1, 110, 254, 0, 254);

Serial output  is called in the external as just Forward/Back/Stop/L/R to see if and when they are triggering.
this is what it is now outputting.
Code: [Select]

Up
--------
Reverse:
Stop:
Forward:
Stop:
Reverse:
Stop:
Reverse:
Stop:
Forward:
Stop:
_________
Down
-------
Reverse:
Stop:
Reverse:
Stop:
Reverse:
Stop:
Reverse:
Stop:
Reverse:
_________
Left
-------
Right:
Stop:
Right:
Stop:
Right:
Stop:
Right:
Stop:
Right:
__________
Right
--------
Right:
Stop:
Right:
Stop:
Right:
Stop:
Right:
Stop:
Right:
__________



Why is stop injecting between each call FsFsF RsRsRs etc with out skipping a beat and why is L/R only calling right function.

Go Up