Pages: [1] 2   Go Down
Author Topic: Need help with IF ELSE trigger for Nrf24L01+ Arduino Motor Shield  (Read 824 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Full Member
***
Karma: 1
Posts: 131
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
//#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:
---------------------
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.
Logged

Fort Lauderdale, FL
Offline Offline
Faraday Member
**
Karma: 71
Posts: 6144
Baldengineer
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Capacitor Expert By Day, Enginerd by night.  ||  Personal Blog: www.baldengineer.com  || Electronics Tutorials for Beginners:  www.addohms.c

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 652
Posts: 50868
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

0
Offline Offline
Full Member
***
Karma: 1
Posts: 131
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

this better?
 
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();
  }
  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);
}



« Last Edit: December 27, 2012, 11:28:00 am by Cyric » Logged

0
Offline Offline
Full Member
***
Karma: 1
Posts: 131
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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:
if(val1 < 110 ){
    motorReverse();
  }
  else if(val1 > 135){
    motorForward();
}
  else{
    motorStop();
  }
Forward now
Code:
int motorForward() {

  val1 = map(val1, 135, 0, 100, 254);
Reverse now
Code:
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:
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.
Logged

Fort Lauderdale, FL
Offline Offline
Faraday Member
**
Karma: 71
Posts: 6144
Baldengineer
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Before the if-then segment starts, print the value of val1.
Logged

Capacitor Expert By Day, Enginerd by night.  ||  Personal Blog: www.baldengineer.com  || Electronics Tutorials for Beginners:  www.addohms.c

0
Offline Offline
Full Member
***
Karma: 1
Posts: 131
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Before the if-then segment starts, print the value of val1.
val1 is this
Code:
UP
------------
251
247
239
254
254
254
254
-------------
Down
---------
0
254
254
254
0
254
254
254
i checked my transmitter it's sending solid 0 - 254 for each pot on the remote arduino with 2x parallax thumb sticks
Logged

Fort Lauderdale, FL
Offline Offline
Faraday Member
**
Karma: 71
Posts: 6144
Baldengineer
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Why does your serial output keep changing so much?

If you print the value of val1 (or val2) right before your if-else blocks, you should see the value it received and the action that was taken.  You need to see that pattern to find out what values are causing stop to be triggered.
Logged

Capacitor Expert By Day, Enginerd by night.  ||  Personal Blog: www.baldengineer.com  || Electronics Tutorials for Beginners:  www.addohms.c

0
Offline Offline
Full Member
***
Karma: 1
Posts: 131
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Why does your serial output keep changing so much?

If you print the value of val1 (or val2) right before your if-else blocks, you should see the value it received and the action that was taken.  You need to see that pattern to find out what values are causing stop to be triggered.

dunno why there changing so much the transmitter arduino with the thumb sticks is sending out solid 0-254 depending on where the stick is the flux happens in transit or on the rec side
Logged

Fort Lauderdale, FL
Offline Offline
Faraday Member
**
Karma: 71
Posts: 6144
Baldengineer
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Capacitor Expert By Day, Enginerd by night.  ||  Personal Blog: www.baldengineer.com  || Electronics Tutorials for Beginners:  www.addohms.c

0
Offline Offline
Full Member
***
Karma: 1
Posts: 131
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
//#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);
}


Logged

0
Offline Offline
Full Member
***
Karma: 1
Posts: 131
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

0
Offline Offline
Full Member
***
Karma: 1
Posts: 131
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
#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);
}

Logged

0
Offline Offline
Full Member
***
Karma: 1
Posts: 131
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Transmitter code to see how i am getting and mapping the values.
Code:
#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);



Logged

0
Offline Offline
Full Member
***
Karma: 1
Posts: 131
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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 smiley-sad")
Code:
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:
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:
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:
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:
#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.
Logged

Pages: [1] 2   Go Up
Jump to: