Hi I am having trouble getting the correct values from a Nano(slave) to Uno(master). The only real difference I can tell is that I am not running te receive event in the loop but am calling it in a function. I think it is necessary because it will stall the motor operations if it is running in the main loop. I am a novice and have cobbled this together the best I can but do not know what I need to do to resolve this issue.
The working master version on UNO:
//MASTER UNO TEST ENVIRO
//
#include <Wire.h>
#define ANSWERSIZE 6
const byte SLV_ADR = 42;
const int clrpn = 7; ///////////////assign pin for clear pin
volatile boolean haveData = false;
String response = "";
long correctSteps = 0;
long stepLength = 90000;///test value
const int stepsPerIn = 300;///test value
float encoder = 0.0;
void setup()
{
pinMode(clrpn,OUTPUT);
digitalWrite(clrpn,LOW);
Wire.begin();
Serial.begin (115200);
} // end of setup
void loop(){
receiveEvent();
} // end of loop
// called by interrupt service routine when incoming data arrives
void receiveEvent(){
Wire.beginTransmission(SLV_ADR);
Wire.write(0);
Wire.endTransmission();
Wire.requestFrom(SLV_ADR,ANSWERSIZE);
response = "";
while(Wire.available()){
char c = Wire.read();
response += c;
}
encoder = ((response.toFloat())/100); ////encoder is encoder val in inches
Serial.println (encoder);
//correctSteps = (stepLength - (encoder * stepsPerIn));
// Serial.println (correctSteps);
// end if have enough data
} // end of receiveEvent
The main program with master incorporated, receive function at bottom:
///////////Notes: Pausing still not working, pausing will not keep the quant values returns zero.
//////////RunFeed not working, need to hook a sensor on and check
/////////////
#include <AccelStepper.h>
#include <Wire.h>
#define ANSWERSIZE 6
// Define a stepper and the pins it will use
AccelStepper stepper(AccelStepper::DRIVER, 5,6);//5,6
AccelStepper stepper2(AccelStepper::DRIVER, 10,11);///10,11 ////////////////////////(AccelStepper::FULL4WIRE, 10, 11, 12, 13);
boolean gotserial = false;
byte f2 = 0;
const byte SLV_ADR = 42; /////////////////////i2c address for encoder
const int clrpn = 7; ///////////////assign pin for clear pin
volatile boolean haveData = false;
String response = ""; //////////////string response for encoder
float encoder = 0.0; ///////////inch val from encoder
////const int cuton = 6;///pin for cutter power
////////////vars from datastreme
const byte numChars = 120;
char receivedChars[numChars];
char tempChars[numChars]; ///temp array for parsing
//variables to hold parsed data
int integerFromPC = 0;
int integerFromPC2 = 0;
float floatFromPC = 0.0;
//////////////////////////holds pause values
volatile long tempPosition = 0;
int cutspd = 6000;///speed for cutter stepper
long cutdist = 33759;//dist in steps for cutter to move
int mapspeed = 0;
int fmapspeed = 0;
int pmapspeed = 0;
float correctDist = 0; //encoderval/encoderperin
long correctSteps = 0;
float tlength = 0; /////total length in inches
float distCut = 0; ///////distance between marks in inches
long stepLength = 0; ////////////////////////total length in
int driveAccel = 6000; /////////////Acceleration for drive function
int cutAccel = 2000; ////////////////Acceleration for cut speed
float stepsPerIn = 302.0;///////adjust steps per inch
const int sensval = 3; //////////////////photoresistor value
const int enable2 = 4;
const int cutOn = 8;//////////////cutter power pin 8
float encoderPerIn = 0.0 ; ////encoder distance inches
const int cutLimit = 12;
void setup() {
Serial.begin(115200);
pinMode(cutOn, OUTPUT); ////////////////////////power to cutter
digitalWrite(cutOn, LOW);
pinMode(enable2, OUTPUT); ////////////////////////power to cutter
digitalWrite(enable2, HIGH);
pinMode(clrpn, OUTPUT); //////////////////clears encoder
digitalWrite(clrpn, HIGH);
delay(20);
digitalWrite(clrpn, LOW);
delay(20);
Wire.begin();
pinMode(sensval, INPUT);
pinMode(cutLimit, INPUT);
while (digitalRead(cutLimit) == HIGH){
digitalWrite(enable2, LOW);
stepper2.setMaxSpeed(900);
stepper2.setAcceleration(1200);
stepper2.setSpeed(-900);
stepper2.runSpeed();
}
digitalWrite(enable2, HIGH);
stepper2.setCurrentPosition(0);
}
void loop(){
getserial();
if (cutState > 0) {
Cut();
}
if (feedfore > 0) {
Feedfore();
}
if (feedback > 0) {
Feedback();
}
if (runfeed > 0) {
feedSeq();
}
if ((message != lastmsg) || (message == "Resum")) {
gotserial = false;
Drive();
}
}
////////////THIS PART OF CODE MISSING
/////////////////////////////////////////i2c receive from encoder///////////////
void receiveEvent() {
Wire.beginTransmission(SLV_ADR);
Wire.write(0);
Wire.endTransmission();
Wire.requestFrom(SLV_ADR,ANSWERSIZE);
response = "";
while (Wire.available()) {
char c = Wire.read();
response += c;
}
encoder = ((response.toFloat()) / 100); ////encoder is encoder val in inches
Serial.println (encoder);
// end if have enough data
} // end of receiveEvent
The UNO checks the encoder state at a certain point in the operation and gets the value which is correct for a certain distance then returns the same value for a longer distance and when the distance is longer returns zero. I had to cut it down the full code is attached.
JTS_ENCODER_MASTER.ino (1.19 KB)
JTS_ENC_SLV.ino (3.21 KB)
JTS_ENCODER_MASTER.ino (1.19 KB)