Pages: [1]   Go Down
Author Topic: data transmission nrf24l01 + need help!  (Read 171 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 1
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I apologize if the question is not asked that branch! Need help! I want to collect the following construction: PROCESSING takes some information from the virtual midi port and sends it to the serialport

Code:
//Import libraries
import processing.serial.*;
import promidi.*;

Serial myPort;
MidiIO midiIO;
//Message to be sent
int message=0;

void setup() {
  myPort = new Serial(this, "COM4", 9600);
  midiIO = MidiIO.getInstance(this);
  //Line that prints I/O devices in console
  midiIO.printDevices();
  //Receive input from Virtual MIDI Ports
  midiIO.openInput(0,0);
}

void draw() {}
void noteOn(Note note, int deviceNum, int midiChan){
  int vel = note.getVelocity();
  int pitch = note.getPitch();
  if(vel>0){ // If velocity > 0 set message value to note's pitch
  if(pitch==60){ message=1; } else
  if(pitch==62){ message=2; } else
  if(pitch==64){ message=3; } else
  if(pitch==65){ message=4; } else
  if(pitch==67){ message=5; } else message=0;
    } else { message=0;}
  //Send message to Arduino
  myPort.write(message);
}

arduino with a radio module NRF24I01 + on board connected to the PC listens serialport receives data {message = 1; } {Message = 2; } {Message = 3; } .... And sends them to the next with the same arduino konstrutsiey. It receives the data and performs an action (on / off LED).

Now the question is if you do not use the radio and raotat directly directly through serialport everything is fine has been online "PROCESSING ---- >>> ARDUINO" (code arduino):

Code:
// Data received from the serial port
int val;
//LEDs are connected to the following pins
int pins[] = {2,3,4,5,6};
void setup() {
  for(int i=0; i<5; i++){
  pinMode(pins[i], OUTPUT); // Set pins as OUTPUTs
  }
  Serial.begin(9600); // Start serial communication at 9600 bps
}
void loop() {
if (Serial.available()) { // If data is available to read,
val = Serial.read(); // read it and store it in val
// Turn on LEDs, depending on pitches received
  if (val == 1) { digitalWrite(pins[0],HIGH); } else
  if (val == 2) { digitalWrite(pins[1],HIGH); } else
  if (val == 3) { digitalWrite(pins[2],HIGH); } else
  if (val == 4) { digitalWrite(pins[3],HIGH); } else
  if (val == 5) { digitalWrite(pins[4],HIGH); } else
  if (val==0) { resetLEDs(); }
}
}
//Function that turns all the LEDs off
void resetLEDs(){
  for(int i=0; i<5; i++){
  digitalWrite(pins[i],LOW);
  }
}


Now cling NRF24I01 + get the following structure "PROCESSING - >> ---- >> ARDUINOperedaet ARDUINOprinimaet" code processing leave the same code Arduino transmitter:

Code:
#include <SPI.h>
#include "RF24.h"
RF24 radio(9, 10);
const uint64_t pipe= {
0xF0F0F0F000LL};// адреса каналов приема и передачи
void setup(){
Serial.begin(9600);
radio.begin();
radio.setDataRate(RF24_250KBPS); // Скорость передачи
radio.setChannel(100); // Номер канала от 0 до 127
radio.setRetries(15,15); // Кол-во попыток и время между попытками
radio.openWritingPipe(pipe); // Открываем канал передачи
radio.startListening(); // Начинаем слушать эфир
}

void loop(){
if(Serial.available()){
char data[32] = "";
byte i = 0;
while(Serial.available()){
data[i] = Serial.read(); //получаем данные из сериал.
i++;
delay(2);
}
data[i] = 0;
radio.stopListening();
radio.write(&data, 32); //и отправляем их в Arduino №2
radio.startListening();
}
}

ARDUINO code receiver:

Code:
#include <SPI.h>
#include "RF24.h"
RF24 radio(9, 10);
//пины куда подключены светодиоды
int pins[] = {1,2,3,4,5,6,7,8};
const uint64_t pipe = {
0xF0F0F0F000LL};// адреса каналов приема и передачи

void setup(){
  for(int i=0; i<8; i++){
  pinMode(pins[i], OUTPUT); // Set pins as OUTPUTs
  }
radio.begin();
radio.setDataRate(RF24_250KBPS); // Скорость передачи
radio.setChannel(100); // Номер канала от 0 до 127
radio.setRetries(15,15); // Кол-во попыток и время между попытками
radio.openReadingPipe(1,pipe); // Открываем один из 6-ти каналов приема
radio.startListening(); // Начинаем слушать эфир
}

void loop(){
if(radio.available()){
char data[32] = "";
radio.read(&data, 32); //
//Если пришла цифра 1 включаем светодиод
if (data[0] == '1') { digitalWrite(pins[0],HIGH); } else
if (data[0] == '2') { digitalWrite(pins[1],HIGH); } else
if (data[0] == '3') { digitalWrite(pins[2],HIGH); } else
if (data[0] == '4') { digitalWrite(pins[3],HIGH); } else
if (data[0] == '5') { digitalWrite(pins[4],HIGH); } else
if (data[0] == '6') { digitalWrite(pins[5],HIGH); } else
if (data[0] == '7') { digitalWrite(pins[6],HIGH); } else
if (data[0] == '8') { digitalWrite(pins[7],HIGH); } else
if (data[0] == '0') { resetLEDs(); }
}
}

void resetLEDs(){
for(int i=0; i<8; i++){
digitalWrite(pins[i],LOW);
  }
}

Launch and receiver silence. Only works when the data is transferred to himself through



Prompt where the error what is wrong???? possible to fix the code!
It is very necessary!
I apologize for my English!
« Last Edit: May 11, 2014, 07:55:54 pm by sdvdio » Logged

UK
Offline Offline
Shannon Member
****
Karma: 222
Posts: 12549
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Your sending sketch doesn't receive any radio messages so it is not necessary to start and stop listening.

Your serial port handling code looks wrong. I haven't look at the message format you're trying to use but it looks as if you expect to receive multiple bytes from the serial port and that there will always be a complete message available. That isn't right - the message will come in slowly one byte at a time and if you want to process a complete message you will have to buffer the bytes as they arrive and work out when you have received a complete message.

On the receiving side you only process the first byte in the payload although there may be up to 32 bytes of useful data there.

For the purpose of testing the radio part, I suggest you disable all the serial port handling and just send a fixed hardcoded message and confirm it is received OK. Then you can check how fast and how reliable that is before you design the rest of your solution around it. Depending how fast you receive input on the serial port, you could easily find that the RF24 can't keep up when it is transmitting a message per byte.

The code to work out what number you have received is very repetitive and you could avoid the repetition by converting the character to a number and doing a range check on it.

Code:
if(isdigit(data[0])
{
  byte value = data[0] - '0';
  if(value == 0)
  {
    resetLEDs();
  }
  else
  {
    digitalWrite(pins[value-1], HIGH);
  }
}
else
{
  // unexpected character ignored
}

If I were you I would connect the receiver to a PC and use the serial port to tell you what it is doing. (Even if there won't be a PC available in the final system, use one for testing.) In that case you must avoid using digital pin 1 in your sketch since the hardware serial port uses it.
« Last Edit: April 18, 2014, 09:15:42 pm by PeterH » Logged

I only provide help via the forum - please do not contact me for private consultancy.

Pages: [1]   Go Up
Jump to: