Earnst v2, Code "MOD" (Læst 965x)

Offline Danni-Hansen

  • µProcessoren
  • *
  • Indlæg: 544
  • Antal brugbare Indlæg: 17
    • Vis profil
Earnst v2, Code "MOD"
« Dato: December 26, 2014, 19:00:31 »
Hej DEF.

Sidder med en kammart og piller ved Earnst koden.

Recive koden på Earnst ser pt sådan her ud:
Kode:
/**
 * 
 * Project nRF24L01 receiver
 * 
 *
 * 
 * @author Gerd Bartelt - www.sebulli.com
 * 
 *
 * 
 * @brief Receives commands from a nRF24L01 and controls a motor.
 * 
 * @brief Based on the RF24 example "GettingStarted"
 * 
 *       
 * 
 * @copyright GPL2
 * 
 */

#include <SPI.h>
#include "nRF24L01.h"
#include "RF24.h"
#include "printf.h"

//
// Hardware configuration
//
// Set up nRF24L01 radio on SPI bus plus pins 9 & 10

RF24 radio(9,10);



// Set up motor on pin 4,5,6 and 7
int right01 = 4;
int right02 = 5;
int left01 = 6;
int left02 = 7;
int frontSensor = 1; // Analog pin 1





// Radio pipe addresses for the 2 nodes to communicate.
const uint64_t pipes[2] = {
  0xF0F0F0F0E1LL, 0xF0F0F0F0D2LL
};

void setup(void)
{



  // Motor pins as output
  pinMode(right01, OUTPUT);
  pinMode(right02, OUTPUT);
  pinMode(left01, OUTPUT);
  pinMode(left02, OUTPUT);



  //Serial.begin(57600);
  printf_begin();



  // Setup and configure rf radio
  radio.begin();



  // Increase the delay between retries & # of retries
  radio.setRetries(15,15);



  // Open pipes to other nodes for communication
  radio.openWritingPipe(pipes[1]);
  radio.openReadingPipe(1,pipes[0]);



  // Start listening
  radio.startListening();



  // Dump the configuration of the rf unit for debugging
    radio.printDetails();

}



void loop(void)

{



  // Receive each packet, dump it out, and send it back
  // if there is data ready
  if ( radio.available() )
  {
    // Dump the payloads until we've gotten everything

    char got_kore_info;
    bool done = false;

    while (!done)
    {

      /*

       //Distance sensor - If less than 10 cm to obj, turn around!
       
       int read_frontSensor = analogRead(frontSensor);
       
       int result_frontSensor = (4800/(read_frontSensor - 20));
       
       
       
       if (result_frontSensor <= 10) {
       
       char got_kore_info = 'A';
       
       }
       
       //End of obj turn around
       
       */



      // Fetch the payload, and see if this was the last one.

      done = radio.read( &got_kore_info, sizeof(char) );



      // Spew it

      printf("Got command '%c'\n\r",got_kore_info);



      // Delay just a little bit to let the other unit

      // make the transition to receiver

      delay(20);





      // First, stop listening so we can talk

      radio.stopListening();


      String cmd_msg;

      // decode the received "køre info"
      switch (got_kore_info)

      {

      case '8':

        forwards();

        cmd_msg = "Forwards, Success";
        radio.write(cmd_msg, sizeof(String));

        break;



      case '2':

        backwards();

        cmd_msg = "Backwards, Success";
        radio.write(cmd_msg, sizeof(String));

        break;



      case '5':

        around();

        cmd_msg = "Turning Around, Success";
        radio.write(cmd_msg, sizeof(String));

        break;



      case '0':

        stopper();

        cmd_msg = "Stopping, Success";
        radio.write(cmd_msg, sizeof(String));
        
        break;
      }


      delay(2500);





    }



    // Send the final one back.

    radio.write( &got_kore_info, sizeof(char) );



    // Now, resume listening so we catch the next packets.

    radio.startListening();



  }

}



void forwards(){

  printf("Forwards\n\r");

  digitalWrite(right02, LOW);

  digitalWrite(right01, HIGH);

  digitalWrite(left02, LOW);

  digitalWrite(left01, HIGH);

}



void backwards(){

  printf("Backwards\n\r");

  digitalWrite(right01, LOW);

  digitalWrite(right02, HIGH);

  digitalWrite(left01, LOW);

  digitalWrite(left02, HIGH);

}



void around(){

  printf("Around\n\r");

  digitalWrite(right01, LOW);

  digitalWrite(right02, HIGH);

  digitalWrite(left02, LOW);

  digitalWrite(left01, HIGH);



}



void stopper(){

  printf("Stop\n\r");

  digitalWrite(right01, LOW);

  digitalWrite(right02, LOW);

  digitalWrite(left02, LOW);

  digitalWrite(left01, LOW);

}

Og det ser ud til at den virker uden problemer og uden capacitor (Konsendsator), både på sender og modtager.
Man skal dog ikke være sååååå ivrig efter at sende samme signal 100000 gange i træk.

Selve koden har jeg udkommenteret:
Kode:
  // Receive each packet, dump it out, and send it back
  // if there is data ready
  if ( radio.available() )
  {
  }

Af den grund at den faktisk modtager og sender oftere end den gjorde før.

..
Min 'MOD' Skulle have været at jeg ville sende String kommentar til Senderen (PC'eren), så man kan se response, men også for at teste sensor af på pc'eren.

Tanken er som den hele tiden har været, Earnst fortæller hvor langt der er til forhindringen, også finder pc'en ud af at komme uden om forhindringen, ved at trans-recive imellem Earnst og PC.

Hjælp søges.

Mvh. Danni Hansen.
Mvh. Danni Hansen.