Welcome to Code Forum!

Join a community that supports you and your coding journey from day one. We strive to be a friendly, supportive community that empowers everyone to be better developers. By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.

    GIF shows where to locate </> in the thread and or post editor toolbar.
    To learn more about how to use our BBCode feature, review our "How to post your code into threads" here.

    Thank you, Code Forum.

Toll system code, would you like to check?

lmin

New Coder
I have a question about my code. It is a program which should send my car number through an nrf24l01 transceiver once an IR receiver receives a signal.
So in short IR beam is received send number.
It consists of a track program delivered by the school and the car part is my excercise, and I'm wondering if my code is correct. It is for an arduino DUE board
Track code:
[CODE title="Track code for refference"]
#include <IRremote2.h>

#include <SPI.h>

#include <nRF24L01.h>

#include <RF24.h>



IRsend BeamIR; //Initialize IR transmitter object

RF24 TrackRadio(4, 6); //Radio pins (CE, CSN)

const int IRQpin = 5; //Radio interrupt pin

const int resetPin = 2; //Arduino reset pin

const byte address[6] = "5XIA0"; //Radio Address: Set to the Energy Challenge course code



unsigned long IRtime = 0; //Variable to time infrared pulses



volatile boolean CarPassed = false; //Boolean to indicate a car has passed



byte CarNumber = 0; //Variable to temporarily store the received car number



unsigned long carTime[256]; //Data array to store lap times

int time2print; //Variable to temporarily store time to be sent to the GUI



void setup()

{

for (int i = 0; i <= 255; i++) //Initialize whole carTime array

{

carTime = -10000; //Set to -10000 to make sure the system immediately works without delay

}



Serial.begin(9600); //Initialize Serial communication

digitalWrite(resetPin, HIGH); //Set the reset pin to high

delay(20);

attachInterrupt(IRQpin, Radio, FALLING); //Trigger the "radio" function when the radio interrupt pin goes low

pinMode(resetPin, OUTPUT); //Set reset pin as output



TrackRadio.begin(); //Start radio object

TrackRadio.openReadingPipe(0, address); //Open communication pipe at defined address

TrackRadio.setPALevel(RF24_PA_LOW); //Set power level of radio

TrackRadio.startListening(); //Tell the radio to start receiving

TrackRadio.maskIRQ(1,1,0); //Mask IRQ pin so the interrupt is only triggered when it receives data

pinMode(13, OUTPUT);

digitalWrite(13, HIGH);

delay(3000);

digitalWrite(13, LOW);

}



void loop()

{

if (millis() - IRtime >= 25) //Send IR pulse every 25ms

{

IRtime = millis(); //Save current time

BeamIR.sendSony(1, 2); //Send the number 1 with a length of two bits(01) through the Sony protocol

}



if (CarPassed) //If a car has passed, execute loop

{

CarPassed = false; //Set Boolean back to false

if (TrackRadio.available()) //If data is available at the radio, execute loop

{

TrackRadio.read(&CarNumber, sizeof(CarNumber)); //Read the received car number from the radio

}

if (millis() - carTime[CarNumber] > 10000) //When the previous received ID is more then 10 seconds ago, execute loop

{

time2print = millis() - carTime[CarNumber]; //Calculate lap time

Serial.println(CarNumber); //Print car number to serial(send to matlab GUI)

Serial.println(time2print); //Print lap time to serial(send to matlab GUI)

carTime[CarNumber] = millis(); //Save current time

}

}



if (Serial.available()) //If data is received at the serial port, execute loop

{

char recChar = Serial.read(); //Read data from Serial port

if (recChar == 'r') //If received data is equal to "r", execute loop

{

digitalWrite(resetPin, LOW); //Set reset pin to LOW to reset Arduino

}

}

}



//When data is received at the radio, the function is executed

//CarPassed is set to true to indicate a car has passed the finish

void Radio()

{

CarPassed = true;

}[/CODE]




Car code:
[CODE title="Car code"]
#include <IRremote2.h>

#include <SPI.h>

#include <nRF24L01.h>

#include <RF24.h>



IRsend BeamIR; //Initialize IR transmitter object

RF24 TrackRadio(4, 6); //Radio pins (CE, CSN)

const int IRQpin = 5; //Radio interrupt pin

const int resetPin = 2; //Arduino reset pin

const byte address[6] = "5XIA0"; //Radio Address: Set to the Energy Challenge course code



unsigned long IRtime = 0; //Variable to time infrared pulses



volatile boolean CarPassed = false; //Boolean to indicate a car has passed



byte CarNumber = 0; //Variable to temporarily store the received car number



unsigned long carTime[256]; //Data array to store lap times

int time2print; //Variable to temporarily store time to be sent to the GUI



void setup()

{

for (int i = 0; i <= 255; i++) //Initialize whole carTime array

{

carTime = -10000; //Set to -10000 to make sure the system immediately works without delay

}



Serial.begin(9600); //Initialize Serial communication

digitalWrite(resetPin, HIGH); //Set the reset pin to high

delay(20);

attachInterrupt(IRQpin, Radio, FALLING); //Trigger the "radio" function when the radio interrupt pin goes low

pinMode(resetPin, OUTPUT); //Set reset pin as output



TrackRadio.begin(); //Start radio object

TrackRadio.openReadingPipe(0, address); //Open communication pipe at defined address

TrackRadio.setPALevel(RF24_PA_LOW); //Set power level of radio

TrackRadio.startListening(); //Tell the radio to start receiving

TrackRadio.maskIRQ(1,1,0); //Mask IRQ pin so the interrupt is only triggered when it receives data

pinMode(13, OUTPUT);

digitalWrite(13, HIGH);

delay(3000);

digitalWrite(13, LOW);

}



void loop()

{

if (millis() - IRtime >= 25) //Send IR pulse every 25ms

{

IRtime = millis(); //Save current time

BeamIR.sendSony(1, 2); //Send the number 1 with a length of two bits(01) through the Sony protocol

}



if (CarPassed) //If a car has passed, execute loop

{

CarPassed = false; //Set Boolean back to false

if (TrackRadio.available()) //If data is available at the radio, execute loop

{

TrackRadio.read(&CarNumber, sizeof(CarNumber)); //Read the received car number from the radio

}

if (millis() - carTime[CarNumber] > 10000) //When the previous received ID is more then 10 seconds ago, execute loop

{

time2print = millis() - carTime[CarNumber]; //Calculate lap time

Serial.println(CarNumber); //Print car number to serial(send to matlab GUI)

Serial.println(time2print); //Print lap time to serial(send to matlab GUI)

carTime[CarNumber] = millis(); //Save current time

}

}



if (Serial.available()) //If data is received at the serial port, execute loop

{

char recChar = Serial.read(); //Read data from Serial port

if (recChar == 'r') //If received data is equal to "r", execute loop

{

digitalWrite(resetPin, LOW); //Set reset pin to LOW to reset Arduino

}

}

}



//When data is received at the radio, the function is executed

//CarPassed is set to true to indicate a car has passed the finish

void Radio()

{

CarPassed = true;

}[/CODE]
 
Last edited:

Buy us a coffee!

Buy me a coffee.
Back
Top Bottom