Hello All,
I am working on a project that requires sending a value from the Primary Arduino Uno after running a PID calculation, then having the Secondary Arduino Uno receive that value and manipulate it (two integrations), and then finally send back that value to the Primary Arduino to output the value and to update the previous variable with the new value.
Currently with the code I am able to send the value between the Primary Arduino to the Secondary Arduino, in this case it is a value of 400. It is divided by 4 since I am using the Analog ports to send the information and it can only send values of 0-255 and cannot take anything larger. Then receiving the value of 100 on the Secondary Arduino, I convert it back to the original value by multiplying it by 4 and running the calculations I need. I then end up with xt which is my desired value. Now it may be larger than 255 so I divide it by 10 (xtAnalog) before sending it over to the Primary Arduino. When viewing the serial monitor I see that my xtAnalog is around 46-50. When I go onto my Primary Arduino serial monitor it is receiving values of 150 - 200. I am incredibly confused on why this is the case.
Any help on how to fix this issue, or even to better the process would be great! I am very new to Arduino and using I2C communication. Thank you for your time. ![]()
Primary (Master) Arduino Uno Code
#include <Wire.h>
#include <PID_v1.h>
#define SlaveAddr 2
#define AnswerSize 7
//Define Variables
double Setpoint;
double Input;
double Output;
int inputPin=0, outputPin=3;
double Kp = 1, Ki = .5, Kd = 2;
int xt = 400;
byte xtAnalog;
//Specify the links and initial tuning parameters
PID myPID(&Input, &Output, &Setpoint,Kp,Ki,Kd, DIRECT);
unsigned long serialTime; //this will help us know when to talk with processing
void setup()
{
Wire.begin(); // Join I2C Bus
//initialize the serial link with processing
Serial.begin(9600);
//initialize the variables we're linked to
Setpoint = 100;
//turn the PID on
myPID.SetMode(AUTOMATIC);
}
void loop()
{
//Change value larger than 255 to within the 0-255 range
xtAnalog = xt/4;
//Check if xtAnalog works
//-------------------------
//Serial.println("xtanalog");
//Serial.println(xtAnalog);
//-------------------------
//Send xtAnalog over to Slave Arduino
delay(50);
Wire.beginTransmission(2);
Wire.write(xtAnalog);
Wire.endTransmission();
//Check if xtAnalog was sent
//Serial.println("Recieved xt");
//Serial.println(xtAnalog);
Wire.requestFrom(2, 9); // request 9 bytes from slave device #2
String string, string1;
do // slave may send less than requested
{
char yeet = Wire.read(); // receive a byte as character
string = string + yeet; //Keep saving whatever is comming
string1 = string.substring(0, 9); //split String from 0 to 9
Serial.println("String");
Serial.println(string1);
}
while(Wire.available());
//pid-related code
Input = analogRead(inputPin);
myPID.Compute();
analogWrite(outputPin,Output);
//send-receive with processing if it's time
if(millis()>serialTime)
{
SerialReceive();
SerialSend();
serialTime+=500;
}
}
Secondary (Slave) Arduino Uno Code
#include<Wire.h>
#define SlaveAddr 2
#define AnswerSize 9
float xt = 0.000;
float xtdot = 0.000;
float xtdotInterval = 0.000;
float xtAnalog;
int Mass = 1;
int Kamp = 1;
double currentTime;
float elapsedTime;
float previousTime = 0.000;
float elapsedTimeSeconds;
char fBuff[20]; //What does the number even mean?
void setup() {
// put your setup code here, to run once:
Wire.begin(2);
Wire.onRequest(Request);
Wire.onReceive(Receive);
Serial.begin(9600);
//Serial.println("Successfuly Set Up");
}
void Receive() {
while (0<Wire.available()) {
xtAnalog = Wire.read();
//Check if xtAnalog is trasfering sucessfuly
//Serial.println("Slave xtAnalog");
//Serial.println(xtAnalog);
xt = xtAnalog*4;
//Check if xt is calculated correctly
Serial.println("Slave xt");
Serial.println(xt, 5);
float fraction = (Kamp*xt)/(Mass);
Serial.println("Fraction Value");
Serial.println(fraction, 5);
//Clock for the Elapsted Time calculation
currentTime = millis();
elapsedTime = currentTime - previousTime;
elapsedTimeSeconds = elapsedTime/1000;
//Check time is converted correctly
Serial.println("Time");
Serial.println(elapsedTimeSeconds, 5);
xtdotInterval = fraction * elapsedTimeSeconds;
xtdot = xtdot + xtdotInterval;
Serial.println("xtdotInterval");
Serial.println(xtdotInterval, 5);
Serial.println("xtdot");
Serial.println(xtdot, 5);
xt += xtdot * elapsedTimeSeconds;
Serial.println("xt");
Serial.println(xt, 5);
//Check if xt is calculated correctly
//Serial.println("xt Fraction");
//Serial.println(xt, 5);
xtAnalog = (xt)/10.0000;
//Check if xt is calculated correctly
Serial.println("xtAnalogOut");
Serial.println(xtAnalog, 5);
previousTime = currentTime;
}
}
void Request() {
//Converting float value to char
//The format (float, bytes, numbers of numbers after the decimal, char variable)
dtostrf(xtAnalog, 9, 5, fBuff);
delay(1000);
Wire.write(fBuff); // appx 9 bytes
Wire.write("\n");
}
void loop() {
// put your main code here, to run repeatedly:
delay(50);
}