Go Down

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

Cyric

Looks like its working now. So i changed how the mapping was some because it was mucking with the base "vals" and recycling them so i made new vars for them for raw incoming data and then new "mapped" ones and now it works nice and smooth forward and back and left and right
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   
//-------Remaped pot data-----------------
int val0ForwardMaped;
int val0ReverseMaped;
int val1LeftMaped;
int val1RightMaped;

//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(val0);
  if(val0 > 135){
    motorForward();
  }
  else if(val0 < 110 ){
    motorReverse();
  }
  else if(val1 < 110 ){
    motorLeft();
  }
  else if(val1 > 135){
    motorRight();
  }
  else{
    motorStop();
  }
  //______________Left / Right________________ 


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


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


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

  val0ForwardMaped = map(val0, 135, 254, 0, 254);
  digitalWrite(ENA_A, LOW);
  digitalWrite(DIR_A, LOW);
  analogWrite(PWM_A, val0ForwardMaped);
  digitalWrite(ENA_B, LOW);
  digitalWrite(DIR_B, LOW);
  analogWrite(PWM_B, val0ForwardMaped);
  Serial.print("val0f: ");
  Serial.println(val0);
  Serial.print("Forward: ");
  Serial.println(val0ForwardMaped);

}

int motorReverse() {
  val0ReverseMaped = map(val0, 110, 0, 100, 254);
  digitalWrite(ENA_A, LOW);
  digitalWrite(DIR_A, HIGH);
  analogWrite(PWM_A, val0ReverseMaped);
  digitalWrite(ENA_B, LOW);
  digitalWrite(DIR_B, HIGH);
  analogWrite(PWM_B, val0ReverseMaped);
  Serial.print("val0r: ");
  Serial.println(val0);
  Serial.print("Reverse: ");
  Serial.println(val0ReverseMaped);


}

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(){
  val1LeftMaped = map(val1, 135, 254, 0, 254);
  digitalWrite(ENA_A, LOW);
  digitalWrite(DIR_A, HIGH);
  analogWrite(PWM_A, val1LeftMaped);
  digitalWrite(ENA_B, LOW);
  digitalWrite(DIR_B, LOW);
  analogWrite(PWM_B, val1LeftMaped);
  Serial.print("val1l: ");
  Serial.println(val1);
  Serial.print("Left: ");
  Serial.println(val1LeftMaped);
}

int motorRight(){
  val1RightMaped = map(val1, 110, 0, 100, 254);
  digitalWrite(ENA_A, LOW);
  digitalWrite(DIR_A, LOW);
  analogWrite(PWM_A, val1RightMaped);
  digitalWrite(ENA_B, LOW);
  digitalWrite(DIR_B, HIGH);
  analogWrite(PWM_B, val1RightMaped);
  Serial.print("val1r: ");
  Serial.println(val1);
  Serial.print("Right: ");
  Serial.println(val1RightMaped);
}

Go Up