Buenos días, buenas tardes y buenas noches.
En mi proyecto quiero conectar varios arduinos(un maestro y varios esclavos), se trata de unos motores que estoy controlando y al dar una vuelta le mandan a un solo arduino (maestro) el dato de que el motor giró una, dos, tres etc y necesito que guarde ese registro con fecha entonces el maestro tendra un rtc y una pantalla, el problema es que las pantallas LCD 16x2 que tienen los esclavos tambien las estoy conectando por I2C y los datos que me llegan al monitor serial y a la pantalla LCD se dañan una vez el motor se enciende alguien me puede ayudar?
Esclavo
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27,16,2);
int Revolutions=1 ; //
String a;
int b=0;
int c=0;
int t=62;
int d=24;
int i=0;
const int motor_power =255; // PWM 0-255
const int counts_per_rev = 1; // (CPR 64/2 =32) * (305:1 Relacion de la Caja) = 5376
//const int enc_l_pin = 2;
const int enc_r_pin = 3;
const int pwma_pin = 4;
const int redbutton = 6;
const int yellowbutton = 7;
const int greenbutton = 8;
const int whitebutton = 9;
const int led_pin =13;
int buttonvalue = 0;
//unsigned int enc_l = 0;
unsigned int enc_r = 0;
//////////////////////////////////////////////////////
unsigned int enc_r_prev = enc_r;
unsigned int target_count = Revolutions * counts_per_rev ;
unsigned int num_ticks_r;
/////////////////////////////////////////////////////
//void countLeft() {
//enc_l++;
//}
void countRight() {
enc_r++;
//enviar(String(enc_r));
}
void setup() {
Wire.begin(1);
Wire.onRequest(Enviapeso);
Serial.begin(9600);
//pinMode(enc_l_pin, INPUT_PULLUP);
pinMode(enc_r_pin, INPUT_PULLUP);
pinMode(pwma_pin, OUTPUT);
pinMode(redbutton,INPUT);
pinMode(yellowbutton,INPUT);
pinMode(greenbutton,INPUT);
pinMode(whitebutton,INPUT);
pinMode(6,INPUT_PULLUP);
pinMode(7,INPUT_PULLUP);
pinMode(8,INPUT_PULLUP);
pinMode(9,INPUT_PULLUP);
pinMode(13,OUTPUT);
//attachInterrupt(digitalPinToInterrupt(enc_l_pin), countLeft, CHANGE);
attachInterrupt(digitalPinToInterrupt(enc_r_pin), countRight, FALLING);
lcd.init();
lcd.home ();
lcd.backlight();
lcd.setCursor(1,0);
lcd.print("Dispensador de cuido");
lcd.setCursor(14,1);
lcd.print("por tercios de lactancia");
for (int j=0; j<t; j++){
lcd.scrollDisplayLeft();
delay(250);
}
delay(200);
lcd.clear();
lcd.setCursor(1,0);
lcd.print("Presione el color");
lcd.setCursor(14,1);
lcd.print("del tercio correspondiente");
for (int j=0; j<d; j++){
lcd.scrollDisplayLeft();
delay(250);
}
delay(200);
lcd.clear();
}
void Enviapeso(){
Wire.write((i+1)/10);
}
void brake() {
digitalWrite(pwma_pin, LOW);
//delay(c);
//lcd.home ();
//lcd.print("Presione el color del tercio de lactancia correspondiente");
return;
}
//void enviar(String w){
// Serial.println(w);
// return;
// }
void drive(int power_a, int c) {
power_a = constrain(power_a, -255, 255);
digitalWrite(pwma_pin, abs(power_a));
//delay(100);
//digitalWrite(pwma_pin, 0);
// delay(c);
return;
}
void turn () {
int j = 1;
b = a.toInt()*1;
for (int i=0; i<b; i++){
//unsigned int num_ticks_l;
int power_l = motor_power;
//enc_l = 0;
/////////////////////////////////////////////////////////////////
if (enc_r == target_count){
enc_r = 0;
}
//////////////////////////////////////////////////////////////////////
//unsigned int enc_l_prev = enc_l;
while (enc_r < target_count) {
//num_ticks_l = enc_l;
num_ticks_r = enc_r;
drive (power_l,c);
//enc_l_prev = num_ticks_l;
enc_r_prev = num_ticks_r;
//Serial.println(target_count);
lcd.setCursor(1,1);
//lcd.print(j++);
brake();
}
delay (c);
lcd.setCursor(0,1);
lcd.print((i+1)*100);
lcd.setCursor(5,1);
lcd.print("Gramos");
Enviapeso;
}
lcd.clear();
return;
}
void loop(){
lcd.setCursor(0,0);
lcd.print("Esperando Color");
if (digitalRead(whitebutton) == LOW) {
a=4;
c=2000;
lcd.clear();
lcd.setCursor(1,0);
lcd.print("Blanco");
turn();
}
else if (digitalRead(greenbutton) == LOW) {
a=8;
c=1000;
lcd.clear();
lcd.setCursor(1,0);
lcd.print("Verde");
turn();
}
else if (digitalRead(yellowbutton) == LOW) {
a=12;
c=500;
lcd.clear();
lcd.setCursor(1,0);
lcd.print("Amarillo");
turn();
}
else if (digitalRead(redbutton) == LOW) {
a=16;
c=100;
lcd.clear();
lcd.setCursor(1,0);
lcd.print("Rojo");
turn();
}
}
Maestro
#include <Wire.h>
float Peso = 0;
void setup(){
Wire.begin();
Serial.begin(9600);
}
void loop(){
Wire.requestFrom(1,1);
while (Wire.available()){
Peso = Wire.read();
Peso = Peso++;
Serial.print(Peso);
}
delay(2000);
}
Muchas Gracias de Antemano si alguien me puede ayudar