I got this working by taking your initial suggestion. I believe the string variable was the problem. I had written the code around that string variable. I created a union with the long and char and was able to send it over that way. Pretty much redid the whole thing. Its so much fun to watch the encoder correcting the stepper in action. Thanks Golam!! The reworked code below.
/* I2c Master receiving optical rotary encoder ZSP4006-003G-600B-5-24 from Nano to Uno
By Joel Butler 5/3/2020
warnng im no engineer, feel free to use how you want
will send over i2c pins A4 & A5
*/
////To use type "1" and enter to get response from slave
#include <Wire.h>
// Define Slave I2C Address
#define SLAVE_ADDR 9
// Define Slave answer size
#define ANSWERSIZE 5
////union converts the byte array to long, to use: converter.buffer1 OR converter.encoder
union longToBytes{
char buffer1[4];
long encoder;
}converter;
/////////////////////////////////////////////////////////////////////////
char userInput=0;///serial input
////////////////////////////////////////////////Setup//////////////////////////////////
void setup() {
Wire.begin();
Serial.begin(9600);
}
/////////////////////////////////////////loop///////////////////////////////////////////
///////////gets data from slave when enter "1" in serial monitor///////////////////////
void loop() {
while (Serial.available()>0){
userInput = Serial.read();
Serial.println(userInput);
}
if(userInput == '1'){
getData();
}
}
//////////////////////////////////////receive event///////////////////////////////////
void getData(){
//clears buffer
uint8_t index = 0;
converter.encoder=0;
//delay(50);////delay required when run from loop
// Write a character to the Slave
Wire.beginTransmission(SLAVE_ADDR);
Wire.write(0);
Wire.endTransmission();
// Read response from Slave
// Read back 5 characters
Wire.requestFrom(SLAVE_ADDR,ANSWERSIZE);
//getting bytes from slave
while (Wire.available()) {
converter.buffer1[index] = Wire.read();
index++;
}
long encoderDistance = (converter.encoder);
//converts to float includes two decimal places
float correctionDistance = (encoderDistance/100.00);
//output to serial
Serial.println(converter.encoder);
Serial.println(correctionDistance);
}
/*
I2c Slave sending optical rotary encoder ZSP4006-003G-600B-5-24 from Nano to Uno
By Joel Butler 5/3/2020
warnng im no engineer, feel free to use how you want
will send over i2c pins A4 & A5
encoder pins 2 & 3
*/
#include <Wire.h>
#define SLAVE_ADDR 9
volatile unsigned long counter = 0;
long tcount = 0;
const int clrpn = 7;
////union converts the long to byte array, to use: converter.buffer1 OR converter.encoder
union longToBytes{
char buffer1[4];
long encoder;
}converter;
///////////////////////////////////////////////////////////////////////////
float encoderPPR = 1200.00;///encoder pulses per rev
float encoderCirc = 6.1;////encoder calibrated at 6.1 inches per revolution
////////////////////////////////////////////SETUP////////////////////////////////
void setup() {
pinMode(clrpn, INPUT);
pinMode(2, INPUT);
pinMode(3, INPUT);
digitalWrite(2, HIGH);
digitalWrite(3, HIGH);
digitalWrite(clrpn, LOW);
//Setting up interrupt
//A rising pulse from encodenren activated ai0(). AttachInterrupt 0 is DigitalPin nr 2 on moust Arduino.
attachInterrupt(0, ai0, RISING);
//B rising pulse from encodenren activated ai1(). AttachInterrupt 1 is DigitalPin nr 3 on moust Arduino.
attachInterrupt(1, ai1, RISING);
Wire.begin(SLAVE_ADDR);
// Function to run when data requested from master
Wire.onRequest(requestEvent);
// Function to run when data received from master
Wire.onReceive(receiveEvent);
Serial.begin(9600);
converter.encoder = 0;
}
///////////////////////////////////////////receiveEvent/////////////////////////////
void receiveEvent() {
while (0 < Wire.available()) {
byte x = Wire.read();
}
}
//////////////////////////////////////////requestEvent//////////////////////////////
void requestEvent() {
//encoder value devided by pulses per revolution
float revs = counter/encoderPPR;
//clculates distance based on wheel circumference
float dist = revs * encoderCirc;
//changes to integer from float
long sendDist = dist * 100;
//long integer distance to be sent
converter.encoder = (sendDist);
//sends byte array from union
Wire.write(converter.buffer1,4);
}
///////////////////////////////////////////loop/////////////////////////////////////
/////////////////////loop empty except getting serial output for debugging//////////
void loop() {
resetCounter();
/*
float revs = counter/encoderPPR;
float dist = revs * encoderCirc;
int sendDist = dist * 100;
//encoderHolder = (((counter/encoderPPR)*Enc_Circ)*100);
converter.encoder = counter/10;/////devides the encoder value by 10 to fit into long int
if (tcount != counter){
//Serial.println(converter.encoder);
//Serial.println(revs,4);
//Serial.println(dist,4);
Serial.println(sendDist);
}
tcount = counter;
*/
}
//////////////////////////////////////////read encoder pin 3//////////////////////////////////
void ai0() {
// ai0 is activated if DigitalPin nr 2 is going from LOW to HIGH
// Check pin 3 to determine the direction
if (digitalRead(3) == LOW) {
counter++;
} else {
counter--;
}
}
/////////////////////////////////////////read encoder pin 4///////////////////////////////
void ai1() {
// ai0 is activated if DigitalPin nr 3 is going from LOW to HIGH
// Check with pin 2 to determine the direction
if (digitalRead(2)==LOW) {
counter--;
} else {
counter++;
}
}
////////////////////////////////resets counter when pin 7 high///////////////////////////
void resetCounter() {
if (digitalRead(clrpn) == HIGH) {
//Serial.println(answer);
counter = 0;
converter.encoder=0;
}
}