Go Down

Topic: Can't realize what i'm missing. (Read 1 time) previous topic - next topic

dab77

Hallo all there!
I've a problem when i try to mix two working codes. the mixed doesn't work.
this works, simply gets midi signals and turns a LED on when receives a 'note on' and off with 'note off':
Code: [Select]
#include <MIDI.h>

RF22 rf22;

boolean state = LOW;

void HandleNoteOn(byte channel, byte pitch, byte velocity) {
 
  state = HIGH;
}
void HandleNoteOff(byte channel, byte pitch, byte velocity) {
 
  state = LOW;
}

void setup() {
  pinMode(A5,OUTPUT);
 
  // Initiate MIDI communications, listen to all channels
  MIDI.begin(MIDI_CHANNEL_OMNI);   
 
  MIDI.setHandleNoteOn(HandleNoteOn);
  MIDI.setHandleNoteOff(HandleNoteOff);
 
}

void loop() {
 
  MIDI.read();
 
  digitalWrite(A5,state); 
}

then this works for a simple spi comunication with a RF22 transceiver:
Code: [Select]
#include <SPI.h>
#include <RF22.h>

RF22 rf22;
int i = 0;

void setup()
{
  Serial.begin(9600);
  if (!rf22.init())
    Serial.println("RF22 init failed");
  // Defaults after init are 434.0MHz, modulation GFSK_Rb2_4Fd36
  pinMode(A5, OUTPUT);
 
}

void loop()
{
  while (1)
  {
    digitalWrite(A5, HIGH);
    for (i=0;i<=255;i+=15) {     
      uint8_t data[] = {i};
      rf22.send(data, sizeof(data));
      rf22.waitPacketSent();
    }
    digitalWrite(A5, LOW);
    for (i=255;i>=0;i-=15) {
      uint8_t data[] = {i};
      rf22.send(data, sizeof(data));
      rf22.waitPacketSent();
    }
  }
}


So what i want to do is send a signal when receiving MIDI, so i wrote this, but doesn't work:
Code: [Select]
#include <MIDI.h>

#include <SPI.h>
#include <RF22.h>

RF22 rf22;

boolean state = LOW;
int batz = 0;

void HandleNoteOn(byte channel, byte pitch, byte velocity) {
 
  state = HIGH;
  batz=255;
}
void HandleNoteOff(byte channel, byte pitch, byte velocity) {
 
  state = LOW;
  batz=0;
}

void setup() {
  pinMode(A5,OUTPUT);
 
  // Initiate MIDI communications, listen to all channels
  MIDI.begin(MIDI_CHANNEL_OMNI);   
 
  MIDI.setHandleNoteOn(HandleNoteOn);
  MIDI.setHandleNoteOff(HandleNoteOff);
 
}

void loop() {
 
  MIDI.read();
 
  digitalWrite(A5,state);
   
  uint8_t data[] = {batz};
  rf22.send(data, sizeof(data));
  rf22.waitPacketSent();
   
}

i simply added to the first code posted, the three end lines and the int 'batz' to give the data to send.
this way the LED on the tx doesn't go on anymore.
i also tried to comment last line - // rf22.waitPacketSent(); - but no result.

where is my mistake?
thanks, Davide.

dab77

Sorry gents, i realized where my mistake was.
i simply forgot the init() command...
for courios this works:
Code: [Select]
#include <MIDI.h>

#include <SPI.h>
#include <RF22.h>

RF22 rf22;

boolean state = LOW;
int batz = 0;

void HandleNoteOn(byte channel, byte pitch, byte velocity) {
 
  state = HIGH;
  batz=255;
}

void HandleNoteOff(byte channel, byte pitch, byte velocity) {
 
  state = LOW;
  batz=0;
}

void blinkFast() {
  for (int i=0;i<=10;i++) {
    digitalWrite(A5, HIGH);
    delay(30);
    digitalWrite(A5, LOW);
    delay(30);
  }   
}

void setup() {
  pinMode(A5,OUTPUT);
  if (!rf22.init()) { //that was missing....
    blinkFast();
  }

  MIDI.begin(MIDI_CHANNEL_OMNI);   
 
  MIDI.setHandleNoteOn(HandleNoteOn);
  MIDI.setHandleNoteOff(HandleNoteOff);
 
}

void loop() {

  MIDI.read();
 
  digitalWrite(A5,state);
   
  uint8_t data[] = {batz};
  rf22.send(data, sizeof(data));
  rf22.waitPacketSent();
   
}

MarkT

BTW why not digitalWrite A5 in the two handler functions?
[ I won't respond to messages, use the forum please ]

dab77

It's because i'm trying to manage the functions into loop() and modify variables inside the handlers.
that's because in a more mature version i should send the same functions (i.e. lamp on, off, fade in, out ect..) to different receivers, so i'm thinking to have one function and different vars to tell which receivers has to blink or what..
I really don't know if i'm doing right, i'll understand later...i wish..  :)

thanks!

Go Up