Ik ben momenteel al een tijdje aan het stoeien met de nRF24L01+
Ik ben nieuw met arduino en heb al tientallen tutorials gemaakt om C onder de knie te krijgen. Lezen en aanpassen lukt al aardig. Echter het zelf schrijven is moeilijk.
Nu heb ik 2 arduino’s met de nRF24L01+ modules werkend. Nu kom ik bij de code.
Op internet heb ik een code gevonden welke ik zou willen aanpassen naar mijn behoefte.
Mijn vraag gaat over de transmitter code. Momenteel loopt de code door in de serial monitor getallen 1 tot en met 5 in te type. hieraan zitten LED gelinkt op receiver. Dit werkt prima. Nu wil ik ipv zelf die getallen in te typen een vaste “loop” aan getallen hebben met de daarbij behorende delay.
alvast bedankt
Transmitter code
// * Code №1 for transmitter
//****************************
//урок от Дмитрия Осипова. http://www.youtube.com/user/d36073?feature=watch
//v.01 transmitter Arduino NRF24L01 radio connection between two Arduino
//Version 0.1 2013/07/13
//detailed video instructions lay here
//http://www.youtube.com/user/d36073?feature=watch
//код №2 для приёмника http://yadi.sk/d/b2t1JAhT6lgYU
//NRF24L01+ 2.4GHz Antenna Wireless Transceiver Module For Microcontr
//Arduino №1 received data from the TV series, sends a radio broadcast packet
//Arduino №2 receives the packet and sends it back to the Arduino №1.
//Arduino №1 receives the packet and sends back to the show.
//that is what is written in the series, then it should go back.
//If you send in a series with Arduino №1 team (1,2,3,4,5) is on the Arduino №2
//will be switched on and off the LEDs.
//http://arduino.ru/forum/programmirovanie/nrf24l01
//*/
// This Download Library
//https://github.com/maniacbug/RF24
//https://github.com/maniacbug/RF24/archive/master.zip
//http://yadi.sk/d/ZvMq19fB6lgPs
#include <SPI.h>
#include "RF24.h"
RF24 radio(9, 10);
const uint64_t pipes[2] = {
0xF0F0F0F000LL, 0xF0F0F0F0FFLL};// The address of reception and transmission channels
void setup(){
Serial.begin(9600);
radio.begin();
radio.setDataRate(RF24_250KBPS); // Transfer Rate
radio.setChannel(100); // Channel number from 0 to 127
radio.setRetries(15,15); // Number of attempts and the time between attempts
radio.openWritingPipe(pipes[1]); // Open channel
radio.openReadingPipe(1, pipes[0]); // Open one of the 6 channels of reception
radio.startListening(); // Start listening to Live
}
void loop(){
if(Serial.available()){
char data[32] = "";
byte i = 0;
while(Serial.available()){
data[i] = Serial.read(); // get the data from the series.
i++;
delay(2);
}
data[i] = 0;
radio.stopListening();
radio.write(&data, 32); // and send them to the Arduino №2
radio.startListening();
}
if(radio.available()){
char data[32] = "";
radio.read(&data, 32); // back to accept a package of Arduino №2
Serial.println(data); // And pass it back to the show
}
}
receiver code
// * Code to the receiver №2
//lesson from Dmitry Osipov. http://www.youtube.com/user/d36073?feature=watch
//v.01 receiver Arduino NRF24L01 radio connection between two Arduino
//Version 0.1 2013/07/13
//detailed video instructions lay here
//http://www.youtube.com/user/d36073?feature=watch
//NRF24L01+ 2.4GHz Antenna Wireless Transceiver Module For Microcontr
//Arduino №1 received data from the TV series, sends a radio broadcast packet
//Arduino №2 receives the packet and sends it back to the Arduino №1.
//Arduino №1 receives the packet and sends back to the show.
//that is what is written in the series, then it should go back.
//If you send in a series with Arduino №1 team (1,2,3,4,5) is on the Arduino №2
//will be switched on and off the LEDs.
//http://arduino.ru/forum/programmirovanie/nrf24l01
// This Download Library
//https://github.com/maniacbug/RF24
//https://github.com/maniacbug/RF24/archive/master.zip
//http://yadi.sk/d/ZvMq19fB6lgPs
#include <SPI.h>
#include "RF24.h"
// Set up nRF24L01 radio on SPI bus plus pins 9 & 10
// Contact NRF24L01 from radio to connect to pinamnam -> Arduino
//SCK -> 13
//MISO -> 12
//MOSI -> 11
//CSN -> 10
//CE -> 9
RF24 radio(9, 10);
// where the pins are connected LEDs
int led = 2;
int led1 = 4;
const uint64_t pipes[2] = {
0xF0F0F0F000LL, 0xF0F0F0F0FFLL};// The address of reception and transmission channels
void setup(){
radio.begin();
radio.setDataRate(RF24_250KBPS); // Transfer Rate
radio.setChannel(100); // Channel number from 0 to 127
radio.setRetries(15,15); // Number of attempts and the time between attempts
radio.openWritingPipe(pipes[0]); // Open channel
radio.openReadingPipe(1, pipes[1]); // Open one of the 6 channels of reception
radio.startListening();// Start listening to Live
pinMode(led1, OUTPUT);
pinMode(led, OUTPUT);
}
void loop(){
if(radio.available()){
char data[32] = "";
radio.read(&data, 32); // receives a packet with Arduino №1
// If you have to figure 1, the LED
if (data[0] == '1') digitalWrite(led, HIGH);
if (data[0] == '2') digitalWrite(led, LOW);
if (data[0] == '3') digitalWrite(led1, HIGH);
if (data[0] == '4') digitalWrite(led1, LOW);
if (data[0] == '5') digitalWrite(led, HIGH), delay(1000),digitalWrite(led, LOW);
radio.stopListening();
radio.write(&data, 32);// And sends back to the Arduino №1
radio.startListening();
}
}