This is the whole code for reference.
Had to remove the ISR's posted in the previous post to have less than 9000 chars.
// this worked on boaeduino as a second i2c slave
//worked with teensy as "master" with MasterUNO also sending i2c
#include <Wire.h>
#include <SPI.h>
/***********************************************************************
************* Declarations *********************************************
************************************************************************/
//-----------add here pin numbers for analog values--------
int dir_pin = 4; // pin number where dir pin is connected
int t_stmotor_pin = 15; // pin number where dir pin is connected
int t_stdriver_pin = 17; // pin number where dir pin is connected
int temp_st_motor = 0;
int temp_st_driver = 0;
int coded_value = 0;
int test_led =13;
boolean test_led_stat=LOW;
int test_led_counter=0;
volatile int dir_pin_val = 1; // based on dir_pin_stat LOW HGH, value is +1 or -1
volatile int dir_pin_prev = 1; // previous val for checking
volatile boolean dir_pin_stat = LOW; // value read from dir_pin
volatile long pul_odo = 0L; // Total Pulses Just adds mileage
volatile long pul_odo_cm = 0L; // Total mileage in cm's
volatile long pul_total = 0L; // Total Pulses adjusted for dir +/- dir
volatile long Pul_count_net = 0L; // no of pulses after h21a interrupt +/- dir, if increase a lot > stalled!!!!!!!GOES VIA I2C
volatile long Pul_counter_error = 0L; // no of pulses over or under run !!!!!!!GOES VIA I2C
volatile long Pul_counter_diff = 0L; // temp clc'd value,
long H21A_pul_width = 80L; // 1600 stps/mm / encoder slots = 1600/10/2
volatile long H21A_total = 0L; // H21A counts adjusted for dir +/- dir
volatile int no_val = 6 ;
volatile int no_dgt = 5 ;
volatile int arr_rdy = 0; // this value is 0 or 1 for which array is ready for i2c
volatile int arr_fil = 1; // this value is 0 or 1 for which array is being filled in loop
volatile int dgt_arr [2] [6] [5]; // no_val , no_dgt; converted and i2c sent from here
long val_arr [6];
long temp_val_prev = 0L;
long temp_val = 0L;
int factor_10 [5] = {10000,1000,100,10,1}; //same as no_dgt
long zzcounter = 0L;
volatile long zzcounteri2c = 0L;
int zztestcount = 0 ;
volatile int ewqewq=3;
/***********************************************************************
************* SETUP ****************************************************
************************************************************************/
void setup() {
Serial.begin(9600);
Wire.begin(15); // join i2c bus with address #2
Wire.onRequest(send_i2c_data); // register event
pinMode (test_led, OUTPUT); // status led; blinking = working
pinMode (dir_pin, INPUT); // pin mode set for dir pin of C11
pinMode (t_stmotor_pin, INPUT); // pin mode set for dir pin of C11
pinMode (t_stdriver_pin, INPUT); // pin mode set for dir pin of C11
//setup*************** attach INTERRUPTS **************
attachInterrupt(0,Pul_void,RISING); //this is pin 2 --> C11 Pulse
attachInterrupt(1,H21A_void,CHANGE); //this is pin 3 --> Stepper H21A1
int i_vl = 0;
int i_dg = 0;
for (i_vl = 0; i_vl < no_val; i_vl ++) {
val_arr [i_vl] = 1L;
for (i_dg = 0; i_dg < no_dgt; i_dg ++) {
dgt_arr [0] [i_vl] [i_dg]=1; } } // end for loop
for (i_vl = 0; i_vl < no_val; i_vl ++) {
for (i_dg = 0; i_dg < no_dgt; i_dg ++) {
dgt_arr [1] [i_vl] [i_dg]=1; } } // end for loop
}
//--------------------ISR's were here----------------
/***********************************************************************
***************************** LOOP *************************************
************************************************************************/
void loop() {
//zzzzzzzzzz read the analog values, add here
temp_st_motor = analogRead (t_stmotor_pin);
temp_st_driver = analogRead (t_stdriver_pin);
coded_value=0;
if(Pul_counter_error < 0) coded_value = coded_value + 100;
else coded_value = coded_value + 300;
if(Pul_count_net < 0) coded_value = coded_value + 10;
else coded_value = coded_value + 30;
if(dir_pin_val < 0) coded_value = coded_value + 1;
else coded_value = coded_value + 3;
if(pul_total < 0) coded_value = coded_value + 1000;
else coded_value = coded_value + 3000;
// coded_value = coded_value + 10000 * (int) (Pul_counter_error/abs(Pul_counter_error) + 2);
// coded_value = coded_value + 1000 * (int) (Pul_count_net/abs(Pul_count_net) +2 );
// coded_value = coded_value + 100 * (int) (dir_pin_val/abs(dir_pin_val) + 2);
val_arr [0] = abs (Pul_counter_error) ; // uses all 5 digits
val_arr [1] = abs (Pul_count_net) ; // uses all 5 digits
val_arr [2] = pul_odo_cm ; // uses all 5 digits
// val_arr [3] = (long)temp_st_motor ;
val_arr [3] = abs (pul_total) ; //xxxxxxxxxxxxxxxxxx
val_arr [4] = (long)temp_st_driver ;
val_arr [5] = (long)coded_value ; //dgt 0:+/- Pul_counter_error 3= +1 / 1= -1
//dgt 1:+/- Pul_count_net 3= +1 / 1= -1
//dgt 2:+/- dir 3= +1 / 1= -1
//dgt 3:
//dgt 4:
zzcounter++;
if(zzcounter>32765) zzcounter=0;
// val_arr [3] = (long) ( zzcounter ) ;
// val_arr [4] = (long) ( zzcounteri2c) ;
// val_arr [5] = (long) ( zzcounter ) ;
//loop... split val_arr into dgt_arr..................
int i_vl = 0;
int i_dg = 0;
for (i_vl = 0; i_vl < no_val; i_vl ++) {
// Serial.print("val ");
// Serial.print(val_arr [i_vl]);
// Serial.print(" > dgt ");
// Serial.print(i_vl);
// Serial.print(" ");
temp_val_prev = (long)val_arr [i_vl];
temp_val = (long)val_arr [i_vl];
for (i_dg = 0; i_dg < no_dgt; i_dg ++) {
dgt_arr [arr_fil] [i_vl] [i_dg]=(int) temp_val_prev/factor_10 [i_dg];
temp_val_prev = (long)(temp_val_prev -
dgt_arr [arr_fil] [i_vl] [i_dg] * (long)(factor_10 [i_dg] ));
// Serial.print(dgt_arr [arr_fil] [i_vl] [i_dg]);
}
// Serial.println("...");
} // end for loops
if (arr_fil == 0) { arr_fil=1; arr_rdy =0; }
else { arr_fil=0; arr_rdy =1; }
//loop-----------------------------------------------
// ----------------- status led blink --------------
test_led_counter++;
if (test_led_counter > 150) {
test_led_counter = 0;
digitalWrite(test_led,test_led_stat);
if (test_led_stat==HIGH) { test_led_stat=LOW; }
else { test_led_stat=HIGH; }
}
// Serial.println(test_led_counter);
// Serial.println(".....");
} // .....END LOOP ..........