Go Down

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

Cyric


Changing as in from post to post.  I suspect you're making many changes to your code without letting anyone here know.


ahh that's from different positions from where the values are being read. in function, in IF statement, before IF just trying to get them where im asked. if you look at the code you will see where they are from. ( did that to keep it simple and noticeable where they are coming out of arduino IDE serial term is not what i would call robust.)
----------------------------------
code as of now.
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_____________

  Serial.println(val1);
  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, 254, 0, 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, 0, 100, 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, 254, 0, 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, 0, 100, 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

Is there any better way of looking at the incoming values and determining the motor direction and speed than this IF/ELSE? i just need to look at val0-4 and take action on if it's stick up/down/left/right and engage the motor driver on those findings. Maybe some buttons in the future.

Cyric

Ok been messing with it for a while and now i got it so that the sticks get transmitted already mapped redid the maps on the rec end and now as it stands it will register forward back left and right. is there a better way of getting the pot vals from the NRF24's and taking action based on the values?



code so far
Code: [Select]

#include <Mirf.h>
#include <MirfHardwareSpiDriver.h>
#include <MirfSpiDriver.h>
#include <nRF24L01.h>
#include <SPI.h>
//***** set vars for each pots/sensor reading from transmitter
int val0; // UP/Down Left Stick   
int val1; // Left/Right Right stick
//int val2; // servo 3   
//int val3; // servo 4   

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);   

  //**************Start Transiever (NRF24L01) config**************
  Mirf.cePin = 48;
  Mirf.csnPin = 49;
  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++++++++++++

  //_________________________Drive___________________________
  //________________Forward / Reverse_____________
  Serial.println(val0);
  Serial.println(val1);
  // Serial.println(val2);
  // Serial.println(val3);

  if(val0 < 110 ){
    motorReverse();
    Serial.println("Reverse");
  }
  else if(val0 > 135){
    motorForward();
    Serial.println("Forward");
  }
  else{
   motorStop();
    Serial.println("Stop");
  }
  //______________Left / Right________________ 
  if(val1 < 110 ){
    motorLeft();
    Serial.println("Left");
  }
  else if(val1 > 135){
    motorRight();
    Serial.println("Right"); 
  }
// else{
//   LRmotorStop();
//   Serial.println("L/R Stop"); 
//  }
  //_____________________END DRIVE___________________


} // end of program
//***********************
//*************** External funct*********************

int motorReverse() {
  val0 = map(val0, 109, 0, 0, 254);
  digitalWrite(ENA_A, LOW);
  digitalWrite(DIR_A, HIGH);
  analogWrite(PWM_A, val0);
  digitalWrite(ENA_B, LOW);
  digitalWrite(DIR_B, HIGH);
  analogWrite(PWM_B, val0);
}
int motorForward() {

  val0 = map(val0, 136, 254, 0, 254);
  digitalWrite(ENA_A, LOW);
  digitalWrite(DIR_A, LOW);
  analogWrite(PWM_A, val0);
  digitalWrite(ENA_B, LOW);
  digitalWrite(DIR_B, LOW);
  analogWrite(PWM_B, val0);
}
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);
}
int LRmotorStop(){

  digitalWrite(ENA_A, LOW);
  digitalWrite(DIR_A, LOW);
  analogWrite(PWM_A, 0);
  digitalWrite(ENA_B, LOW);
  digitalWrite(DIR_B, LOW);
  analogWrite(PWM_B, 0);
}
int motorLeft(){
  val1 = map(val1, 109, 0, 0, 254);
  digitalWrite(ENA_A, LOW);
  digitalWrite(DIR_A, HIGH);
  analogWrite(PWM_A, val1);
  digitalWrite(ENA_B, LOW);
  digitalWrite(DIR_B, LOW);
  analogWrite(PWM_B, val1);
}

int motorRight(){
  val1 = map(val1, 136, 254, 0, 254);
  digitalWrite(ENA_A, LOW);
  digitalWrite(DIR_A, LOW);
  analogWrite(PWM_A, val1);
  digitalWrite(ENA_B, LOW);
  digitalWrite(DIR_B, HIGH);
  analogWrite(PWM_B, val1);
}


Cyric

Transmitter code to see how i am getting and mapping the values.
Code: [Select]

#include <Mirf.h>
#include <MirfHardwareSpiDriver.h>
#include <MirfSpiDriver.h>
#include <nRF24L01.h>
#include <SPI.h>
//**************Start Pin Setup***************
int LS_U_D = 3;  // analog pin used for the pot Left U/D
//int LS_L_R = 2;  // analog pin used for the pot Left L/R
//int RS_U_D = 5;  // analog pin used for the pot Right U/D
int RS_L_R = 4;  // analog pin used for the pot Right L/R
//**************End Pin Setup***************
//**************Global vars Setup***************
int LS_U_Dval;
//int LS_L_Rval;   
//int RS_U_Dval;
int RS_L_Rval;   
//**************END vars Setup***************
void setup(){
  Serial.begin(57600); // start serial communications
  //**************Start Transiever (NRF24L01) config**************
  Mirf.cePin = 9;
  Mirf.csnPin = 10;
  Mirf.spi = &MirfHardwareSpi;
  Mirf.init();
  Mirf.setRADDR((byte *)"clie1");
  Mirf.payload = 4 * sizeof(byte);
  Mirf.config();
  //**************End Transiever (NRF24L01) config**************
  Serial.println("Beginning ... "); // Print some stuff to serial
}
void loop(){
  //****************************Read Sticks**********************
  LS_U_Dval = analogRead(LS_U_D); // set val to read analog pin 2
  //  LS_L_Rval = analogRead(LS_L_R);// set val to read analog pin 3
  //  RS_U_Dval = analogRead(RS_U_D); // set val to read analog pin 4
  RS_L_Rval = analogRead(RS_L_R);// set val to read analog pin 5
  //*****************************End Read Sticks*******************
  //**************convert pot to PWM values**************
  LS_U_Dval = map(LS_U_Dval, 0, 1023, 0, 254);
  //     LS_L_Rval = map(LS_L_Rval, 0, 1023, 0, 254);   
  //     RS_U_Dval = map(RS_U_Dval, 0, 1023, 0, 254);
  RS_L_Rval = map(RS_L_Rval, 0, 1023, 0, 254);
  Serial.println(LS_U_Dval);
  //**************convert pot to PWM values**************
  //**************put values in array?**************
  byte vals[2];
  //**********Send only 2 pots from sticks for now**********
  vals[0] =LS_U_Dval;
  vals[1] =RS_L_Rval; 
  //**********Send only 2 pots from sticks for now**********
  //  vals[0] =LS_L_Rval;
  //  vals[1] =LS_U_Dval; 
  //  vals[2] =RS_L_Rval; 
  //  vals[3] =RS_U_Dval;
  //**************finish puting values in array?**************
  //**************Start Transmit package**************
  Mirf.setTADDR((byte *)"reci1"); // set name of Reciever
  Mirf.send(vals); // data to send
  //**************Start For loop to print array to local serial**************
  /*
  int i;
   for (i = 0; i < 4; i = i + 1)
   {
   Serial.println(vals[i], DEC);
   }
   // */
  //**************End For loop to print array to local serial**************
  while(Mirf.isSending()){
  }
  delay(10);





Cyric

Ok this seems to be more working than not so here goes. if you notice the output it's showing the incoming data is center pot post map so high 120's and because of that the else part of the if else kicks in to stop sending movement commands to motors ( without it the motors will stay turning in last direction rec till new value is given)
sticks centered
Code: [Select]

127
129
Stop
L/R Stop
Stop
L/R Stop
Stop
L/R Stop
127
129


If i push UP on left stick i get wheel movement in one direction  ( tho a lil choppy "L/R Stop is disabled or i would just hang :(")
Code: [Select]

254
129
Forward
L/R Stop
Forward
L/R Stop
253
129

If i push DOWN the driver tries to move the left side wheels but the right side kinda hang and i get this for output ( as mentioned L/R STOP leads no ware)
Code: [Select]

1
129
Reverse
L/R Stop
Reverse
L/R Stop
1
129

If i press right on the right side stick i get this and movement much like foreword on the other stick (Stop IS tied to the motorStop() function)
Code: [Select]

127
254
Stop
Right
Stop
Right
127
253

If i pres LEFT on the right side stick i get sound with no movement erratic movement but in the right directions ( im sure it's cos of motorStop from the first if else jumping in and mucking things up)
Code: [Select]

127
33
Stop
Left
Stop
Left
127
33


So the triggers seem? to work but now i am having to worry about stop functions getting in the way?

here is the full code thus far.
Code: [Select]
#include <Mirf.h>
#include <MirfHardwareSpiDriver.h>
#include <MirfSpiDriver.h>
#include <nRF24L01.h>
#include <SPI.h>
//***** set vars for each pots/sensor reading from transmitter
int val0; // UP/Down Left Stick   
int val1; // Left/Right Right stick
//int val2; // servo 3   
//int val3; // servo 4   

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);   

  //**************Start Transiever (NRF24L01) config**************
  Mirf.cePin = 48;
  Mirf.csnPin = 49;
  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 < 2; 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++++++++++++

  //_________________________Drive___________________________
  //________________Forward / Reverse_____________
//  Serial.println(val0);
//  Serial.println(val1);
  // Serial.println(val2);
  // Serial.println(val3);

  if(val0 < 110 ){
    motorReverse();
    Serial.println("Reverse");
  }
  else if(val0 > 135){
    motorForward();
  Serial.println("Forward");
  }
  else{
   motorStop();
  Serial.println("Stop");
  }
  //______________Left / Right________________ 
  if(val1 < 110 ){
    motorLeft();
    Serial.println("Left");
  }
  else if(val1 > 135){
    motorRight();
  Serial.println("Right"); 
  }
else{
//   LRmotorStop();
   Serial.println("L/R Stop"); 
  }
  //_____________________END DRIVE___________________


} // end of program
//***********************
//*************** External funct*********************

int motorReverse() {
  val0 = map(val0, 0, 110, 0, 254);
  digitalWrite(ENA_A, LOW);
  digitalWrite(DIR_A, HIGH);
  analogWrite(PWM_A, val0);
  digitalWrite(ENA_B, LOW);
  digitalWrite(DIR_B, HIGH);
  analogWrite(PWM_B, val0);
}
int motorForward() {

  val0 = map(val0, 135, 254, 0, 254);
  digitalWrite(ENA_A, LOW);
  digitalWrite(DIR_A, LOW);
  analogWrite(PWM_A, val0);
  digitalWrite(ENA_B, LOW);
  digitalWrite(DIR_B, LOW);
  analogWrite(PWM_B, val0);
}
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);
}
int LRmotorStop(){

  digitalWrite(ENA_A, LOW);
  digitalWrite(DIR_A, LOW);
  analogWrite(PWM_A, 0);
  digitalWrite(ENA_B, LOW);
  digitalWrite(DIR_B, LOW);
  analogWrite(PWM_B, 0);
}
int motorLeft(){
  val1 = map(val1, 0, 110, 0, 254);
  digitalWrite(ENA_A, LOW);
  digitalWrite(DIR_A, HIGH);
  analogWrite(PWM_A, val1);
  digitalWrite(ENA_B, LOW);
  digitalWrite(DIR_B, LOW);
  analogWrite(PWM_B, val1);
}

int motorRight(){
  val1 = map(val1, 135, 254, 0, 254);
  digitalWrite(ENA_A, LOW);
  digitalWrite(DIR_A, LOW);
  analogWrite(PWM_A, val1);
  digitalWrite(ENA_B, LOW);
  digitalWrite(DIR_B, HIGH);
  analogWrite(PWM_B, val1);
}


So some how i need to free the motors when center pot move F/R and L/R( combo LR skid steering) i guess with out repeatedly calling motorStop? any input would help guys thanks.

Go Up