Maybe it gets "called within itself" because of too fast comms?
I don't have the toys to know for sure, but I do not think so. I've Serial.print'd on the send side and the rec've side and the data is going and coming at 8 secs as desired... is the line hanging for 8 sec? again, don't think so because I have a second slave on the bus working correctly.. blinking at 2 seconds. I assume, if the one slave was keeping the line, the second would be sitting and waiting for data...
How? We'd need to see that code
I am very new to this, and this set up was also a test for learning how the bus passes data... with only 8 bits, I can't get to 360 deg... so, there will be some scaling going on, but yes, I've Serial.print'd the data on the send and receive side and it appears to be recieving correctly... both master and slave code below....
This version of the slave is attempting to call the blinker function from loop()... I've tried to call it from within the receiveEvent as well, neither of which has worked... again, I do get a very fast half blink when the data is rec'd. But, it acts like the receiveEvent function isn't releasing ... however you word the interrupt release... the basics were taken from Nick Gammon's tutorial. Thanks for the look!
Slave code
#include <Wire.h>
#define ME 52 // this slave address - CMP
#define LED 5 // used for debugging
int del = 150; // delay to make blinks visible
boolean awake = false; // blink on power on/reset
int hdg_old = 0; // used when this starts working
int x = 90;
int *hdg = &x;
void setup()
{
Serial.begin(9600); // debugn
Wire.begin (ME); // begin
Wire.onReceive (receiveEvent); // event handler
pinMode(LED, OUTPUT); // led pin
} // end setup
void receiveEvent (int data_in) // event handler
{
while (Wire.available() > 0)
{
x= Wire.receive(); // int value from master, heading value
Serial.print("--> Event: "); // debugn
Serial.println(x); // debugn
} // end while available
// blinker(x); // send heading value to blinker
} // end receiveEvent
void blinker (int val) // receive heading value into val
{
if (val >= 180) {val = 180;} // check that val is less than 180, arbitrary for now
int blnk = (int)(val/15); // divide by 15, num of blinks to a reasonable num, bebugn
if (blnk <= 1) {blnk = 1;} // has to be 1 or more
Serial.print("[Blinker]>> Wire recd: "); // debugn
Serial.print(val); // debugn
Serial.print("..Blink Num: "); // debugn
Serial.println(blnk); // debugn
for (int i=0; i<blnk; i++) // blink heading/15 times
{
digitalWrite(LED, HIGH);
delay(del);
digitalWrite(LED, LOW);
delay(del);
}
} // end blinker
void wakeup () // on power up/reset, blink 3 times
{
awake = true; // perform once only
for (int i=1; i<=3; i++)
{
digitalWrite (LED, HIGH);
delay(del);
digitalWrite (LED, LOW);
delay(del);
}
}
void loop()
{
if (awake == false) // on power up/reset awake is false, call once only
{
wakeup();
if (*hdg != hdg_old)
{
blinker(*hdg);
hdg_old = *hdg;
}
}
}
Master Code
#include <Wire.h>
#define ME 12 // master node address
#define PFD 22 // slave PFD
#define CMP 52 // slave Compass CMP
#define LED 13 // debugging, local blinker
#define chkf 500 // check items quickly 500 ms
#define chkm 1000 // check itemes mediam, 1 sec
#define chks 2000 // check slowly, every 2 sec
#define chkr 8000 // check really slow, every 8 seconds
int del = 150;
boolean state = false; // led state
boolean awake = false; // changes once on power up/reset
unsigned long ticker, last; // timer
unsigned long last_pdf, last_cmp; // previous timer values for comparison
int headn = 15; // debugn, sending an initial value to HDG, gets proper blinks
// subsequent sends do not
void setup()
{
Serial.begin(9600); // debugn
Wire.begin (ME);
pinMode (LED, OUTPUT);
digitalWrite (LED, LOW);
} // end of setup
void wakeup () // runs one time at power up/reset - 3 blinks
{
awake = true;
for (int i=1; i<=3; i++)
{
digitalWrite (LED, HIGH);
delay(del);
digitalWrite (LED, LOW);
delay(del);
}
}
void blink_pfd (boolean cstate) // send data to pfd
{
// toggle on and off after x millis()
if (cstate == true) {
int x=1;
Wire.beginTransmission (PFD);
Wire.send (x);
if (Wire.endTransmission () == 0)
digitalWrite (LED, HIGH); // blink on send - debugn
else
digitalWrite (LED, LOW);
}
else
{
int x=0;
Wire.beginTransmission (PFD);
Wire.send (x);
if (Wire.endTransmission () == 0)
digitalWrite (LED, LOW); // blink on send - debugn
else
digitalWrite (LED, HIGH);
}
}
void heading (int hdgg) // send data to CMP
{
Wire.beginTransmission (CMP);
Wire.send (hdgg);
if (Wire.endTransmission () == 0)
{
for (int i=1; i < 4; i++) // blink on send - debugn - Questionable
{
digitalWrite (LED, HIGH);
delay(100);
digitalWrite (LED, LOW);
delay(100);
}
}
else
{
digitalWrite (LED, LOW);
}
} // end heading
unsigned long getTime (unsigned long timech) // ticker / polling for use in loop
{
unsigned long time1 = millis();
unsigned long time2 = time1 - timech;
return(time2);
}
void loop()
{
if (awake == false) // runs one time at power up / reset
{
wakeup();
}
ticker = getTime(last_pdf); // check elapsed time since last calling pdf
if (ticker >= chkm) // blink PFD after chkm millis
{
Serial.println("Calling PDF");
last_pdf = millis();
blink_pfd(state);
Serial.print("Ticker: ");
Serial.print(ticker );
Serial.print(" Last: ");
Serial.print(last_cmp);
Serial.print(" Millis: ");
Serial.println(millis());
if (state == true)
state = false;
else
state = true;
}
ticker = getTime(last_cmp); // check elapsed time since last calling hdg
if (ticker >= chkr) // blink hdg after chkr millis
{
last_cmp = millis();
Serial.println("Calling HDG");
int headn = (int)(rand()*180); // random heading value for debug
heading(headn);
}
} // end of loop
Thanks again for any and all assistance!