Hi everyone
I am interfacing a 4Duino LCD to an Arduino Micro
I am trying to implement the I2C protocol.
I want a variable X to be shown on the 4Duino LCD, incrementing from 0 to 5, then resetting and starting again. I found the code online, quotes are in the code. the variable X is sent to the 4Duino from its master, the Arduino Micro.
this is the code compiled on the A Micro (master):
// Chapter 7 - Communications
// I2C Master
// By Cornel Amariei for Packt Publishing
// Include the required Wire library for I2C
#include <Wire.h>
int x = 0;
void setup() {
// Start the I2C Bus as Master
Wire.begin();
}
void loop() {
Wire.beginTransmission(9); // transmit to device #9
Wire.write(x); // sends x
Wire.endTransmission(); // stop transmitting
x++; // Increment x
if (x > 5) x = 0; // `reset x once it gets 6
delay(500);
}
and this is the code compiled on the 4Duino 2.4" (slave):
//
#define RESETLINE 30
#define DisplaySerial Serial1
// uncomment to log SPE errors messages to Serial (serial monitor)
#define LOG_MESSAGES
#include "Picaso_Serial_4DLib.h"
#include "Picaso_Const4D.h"
Picaso_Serial_4DLib Display(&DisplaySerial);
/* the following code is taken from I2C_Slave */
#include <Wire.h>
#define led 13
int x = 0;
// routine to handle ESP Serial errors
void mycallback(int ErrCode, unsigned char Errorbyte){
#ifdef LOG_MESSAGES
const char *Error4DText[] = {"OK\0", "Timeout\0", "NAK\0", "Length\0", "Invalid\0"} ;
Serial.print(F("Serial 4D Library reports error ")) ;
Serial.print(Error4DText[ErrCode]) ;
if (ErrCode == Err4D_NAK){
Serial.print(F(" returned data= ")) ;
Serial.println(Errorbyte) ;
}else{
Serial.println(F("")) ;
}while (1) ; /* you can return here, or you can loop */
#else
/* Pin 13 has an LED connected on most Arduino boards. Just give it a name*/
#define led 13
while (1){
digitalWrite(led, HIGH);
delay(200);
digitalWrite(led, LOW);
delay(200);
}
#endif
} /* end of routine to handle Serial errors */
void setup(){
/*Uncomment to use the Serial link to the PC for debugging*/
/*serial to USB port*/
// Serial.begin(115200) ;
/* Note! The next statement will stop the sketch from running until the serial monitor is started
If it is not present the monitor will be missing the initial writes*/
/* wait for serial to be established*/
// while (!Serial) ;
pinMode(RESETLINE, OUTPUT); /* Picaso reset pin */
digitalWrite(RESETLINE, 1); /* Reset Display, using shield */
delay(100); /* wait for it to be recognised */
digitalWrite(RESETLINE, 0); /* Release Display Reset, using shield */
delay(3000) ; /* give display time to startup */
/* now start display and ESP as Serial lines should have 'stabilised' */
DisplaySerial.begin(200000) ; /* Hardware serial to Display, same as SPE on display is set to */
Display.TimeLimit4D = 5000 ; /* 5 second timeout on all commands */
Display.Callback4D = mycallback ;
Display.gfx_ScreenMode(PORTRAIT) ; /*change manually if orientation change*/
/* put your setup code here, to run once: */
/*code from I2C_Slave*/
pinMode (led, OUTPUT);
/* Start the I2C Bus as Slave on address 9 */
Wire.begin(9);
/* Attach a function to trigger when something is received. */
Wire.onReceive(receiveEvent);
/*-------------------*/
} /* end Setup **do not alter, remove or duplicate this line** */
/* from I2C_Slave */
void receiveEvent(int bytes) {
x = Wire.read(); /* read one character from the I2C */
}
void loop(){
// put your main code here, to run repeatedly:
Display.txt_MoveCursor (1,0);
Display.println ("Current value of");
Display.println ("variable X is:");
Display.txt_MoveCursor (4,0);
Display.println(x);
// Display.println ("This is the second line");
//
// Display.txt_MoveCursor (6,0);
// Display.print ("This is the fourth line");
}
If the 4Duino is connected to the arduino only using the SDA and SCL cables, while the power is supplied from the USB port, the code works fine.
If I am feeding 5Vdc to the 4duino Vin pin using the 5V output present on the Arduino Micro, and connecting the GND of the 4duino to the gnd pin on the Micro, the code doesn't work. The 4Duino powers up, but the value on screen don't change.
any good link to look at or any suggestion?
could be the 4Duino requires more current than what is supplied from the A.Micro?
thank you!!