Hello. My system uses an Arduino micro to read and measure the data from an MPU6050 sensor and FSR sensors and send it through Bluetooth(FB155BC) to the mainboard(Arduino Mega). My problem is that when I receive the data from the slave it looks live an overflow is happening(the number 255 appears instead of the actual data sometimes, but not constantly). I don't know how to fix this problem. Here are the codes:
Master code:
#include <stdio.h>
#include<movingAvg.h>
int buttonState = 0;
uint8_t buf_r[50], buf_l[50];
uint8_t old_buf_r[50], old_buf_l[50];
uint8_t pack_r[50], pack_l[50];
uint8_t intact_pack_r[50], intact_pack_l[50];
movingAvg encvel(10);
movingAvg enc(10);
int Encoder_pin[10] = {25, 27, 29, 31, 33, 35, 37, 39, 41, 43};
int Encoder_state[10];
int Encoder_sum = 0;
double Encoder_deg_new = 0, Encoder_deg_old = 0;
double E_vel_deg_new, E_vel_deg_old, Encoder_vel, Encoder_vel_rpm;
int Encoder_revcnt = 0;
double A_encoder = 0;
char Value[10];
unsigned int count = 0;
volatile byte state = 0;
double encavg=0;
double torque=0;
double Encoder_offset =0;
float seno;
int frequencia;
unsigned long previousMicro = 0;
const long interval = 1000;
int flagr=0;
int flagl=0;
boolean newData = false;
void setup() {
for (int i = 0; i < 10; i++) {
pinMode(Encoder_pin[i], INPUT);
}
Serial.begin(115200);
Serial3.begin(115200);
Serial2.begin(115200);
encvel.begin();
enc.begin();
Serial3.write('A');
Serial2.write('B');
}
void loop() {
unsigned long currentMicro = micros();
if ((unsigned long)(micros() - previousMicro) >= interval) {
previousMicro = micros(); // keep in sync with the time
Encoder_deg_old = Encoder_deg_new;
E_vel_deg_old = E_vel_deg_new;
for (int i = 0; i < 10; i++) {
Encoder_state[i] = digitalRead(Encoder_pin[i]);
}
Encoder_sum = 0;
int Encoder_cnt = 0;
for (int Encoder_cnt = 0; Encoder_cnt < 10; Encoder_cnt++) {
Encoder_sum += Encoder_state[Encoder_cnt] << Encoder_cnt;
}
int encavg=enc.reading(Encoder_sum);
Encoder_deg_new = ((long)((((double)Encoder_sum*0.3515625) - Encoder_offset + 360)*(long)(100)))%36000/100.0;
int i, j, val, k1_r, k2_r, k1_l, k2_l = 0;
int t1_r, t2_r, t1_l, t2_l = 0;
char checknumberL = '?';
char checknumberR = '&';
if(Serial3.available() && flagr==0){
for (j = 0; j < 6; j++)
{
buf_r[j] = Serial3.read();
}
if((buf_r[0]=='
Cont. on the next post because of character limit.)&&(buf_r[5] == checknumberR)){
flagr=1;
}
}
if(Serial2.available()>0 && flagl==0){
for (j = 0; j < 6; j++)
{
buf_l[j] = Serial2.read();
}
if((buf_l[0]=='!')&&(buf_l[5] == checknumberL)){
flagl=1;
}
}
// /////////////////raw data check
for (int i = 0; i < 6; i++) {
Serial.print(buf_r[i]);
Serial.print('\t');
}
Serial.print('\n');
for (int i = 0; i <6; i++) {
Serial.print(buf_l[i]);
Serial.print('\t');
}
Serial.print('\n');
//data processing
/////////////////Right//////////////////////
if(flagr==1){
if (buf_r[5] == checknumberR) {
for (j = 0; j < 6; j++) {
pack_r[j] = buf_r[j];
}
for ( i = 0; i < 6 - 1; i++) {
if ( pack_r[i] == checknumberR ) {
for (j = 0; j < 6; j++) {
pack_r[j] = intact_pack_r[j];
}
}
}
for (j = 0; j < 6; j++) {
intact_pack_r[j] = pack_r[j];
}
}
else {
for (int k = 0; k < 6; k++) {
if (buf_r[k] == checknumberR) {
k1_r = k;
k2_r = 4 - k;
while (k1_r >= 0) {
pack_r[5 - t1_r] = buf_r[k1_r];
k1_r--; t1_r++;
}
while (k2_r >= 0) {
pack_r[k2_r] = old_buf_r[5 - t2_r];
k2_r--; t2_r++;
}
break;
}
else {
;
}
}
for ( i = 0; i < 6 - 1; i++) {
if ( pack_r[i] == checknumberR ) {
for (j = 0; j < 6; j++) {
pack_r[j] = intact_pack_r[j];
}
}
}
for (j = 0; j < 6; j++) {
intact_pack_r[j] = pack_r[j];
}
}
if (pack_r[0] != '
Cont. on the next post because of character limit.) {
for (j = 0; j < 6; j++) {
pack_r[j] = intact_pack_r[j];
}
}
for (j = 0; j < 6; j++) {
old_buf_r[j] = buf_r[j];
}
Serial3.write('A');
// for (j = 0; j < 6; j++)
// {
// buf_r[j] = ' ';
// }
flagr=0;
}
///////////////////left////////////////////
if(flagl==1){
if (buf_l[5] == checknumberL) {
for (j = 0; j < 6; j++) {
pack_l[j] = buf_l[j];
}
for ( i = 0; i < 6 - 1; i++) {
if ( pack_l[i] == checknumberL ) {
for (j = 0; j < 6; j++) {
pack_l[j] = intact_pack_l[j];
}
}
}
for (j = 0; j < 6; j++) {
intact_pack_l[j] = pack_l[j];
}
}
else {
for (int k = 0; k < 6; k++) {
if (buf_l[k] == checknumberL) {
k1_l = k;
k2_l = 4 - k;
while (k1_l >= 0) {
pack_l[5 - t1_l] = buf_l[k1_l];
k1_l--; t1_l++;
}
while (k2_l >= 0) {
pack_l[k2_l] = old_buf_l[5 - t2_l];
k2_l--; t2_l++;
}
break;
}
else {
;
}
}
for ( i = 0; i < 6 - 1; i++) {
if ( pack_l[i] == checknumberL ) {
for (j = 0; j < 6; j++) {
pack_l[j] = intact_pack_l[j];
}
}
}
for (j = 0; j < 6; j++) {
intact_pack_l[j] = pack_l[j];
}
}
if (pack_l[0] != '?') {
for (j = 0; j < 6; j++) {
pack_l[j] = intact_pack_l[j];
}
}
for (j = 0; j < 6; j++) {
old_buf_l[j] = buf_l[j];
}
Serial2.write('B');
for (j = 0; j < 6; j++)
{
buf_l[j] = ' ';
}
flagl=0;
}
float pi=3.14159;
float encorderrad = Encoder_deg_new*(pi/180);
float AR=((pack_r[1] << 8) | pack_r[2]);
float FR=((pack_r[3] << 8) | pack_r[4])*0.01;
float AL=((pack_l[1] << 8) | pack_l[2])*0.01;
float FL=((pack_l[3] << 8) | pack_l[4])*0.01;
float Rangrad=AR*(pi/180);
float Langrad=AL*(pi/180);
float torque = (0.17*0.022539446/0.089013718)*(FR*cos(encorderrad-Rangrad)+FL*cos((encorderrad+pi)-Langrad));
//Serial.print(Encoder_deg_new);
//Serial.print(" ");
//Serial.println(360);
// if((pack_l[0] == 200) &&
// (pack_r[0] == 200)
// &&(pack_l[1] < 180) &&
// (pack_r[1] < 180) &&
// (pack_l[2] != pack_l[1]||pack_l[0]) &&
// (pack_r[2] != pack_r[1]||pack_r[0])
// )
// {
//
//
//
//// Serial.print(Encoder_deg_new);
//// Serial.print(" ");
//
// for (int i = 0; i < 6; i++) {
// Serial.print(pack_l[i]);
// Serial.print("\t");
// }
// Serial.print(" ");
// for (int i = 0; i < 6; i++) {
// Serial.print(pack_r[i]);
// Serial.print("\t");
// }
// Serial.println("");
// Serial.println(AL);
//// Serial.print(" ");
//// Serial.print(FL);
//// Serial.print(" ");
// Serial.println(AR*0.01);
// Serial.print(" ");
// Serial.println(FR);
//// Serial.print(" ");
//// Serial.println(torque);
// }
//
// else if((pack_l[0] != 200) ||
// (pack_r[0] != 200)){
// Serial.print("");
// }
}
}
Cont. on the next post because of character limit.