0

I've been trying to store data in an external I2C EEPROM but it doesn't seem to work, and i don't know why.

When i run the i2c scanner it founds my memory but i can't write in it, or at least it seems like i can´t

Please help me :(

(I'm using a 24C512 btw)

#include <SoftwareSerial.h>
SoftwareSerial BT1(3, 4); // RX | TX
#include <Wire.h>
#include <SPI.h>
#include <Adafruit_GFX.h>
#include <Max72xxPanel.h>
#include <EEPROM.h>

#define EEPROM_I2C_ADDRESS 0x50


//Vcc - Vcc
//Gnd - Gnd
//Din - Mosi (Pin 11)
//Cs  - SS (Pin 10)
//Clk - Sck (Pin 13)

String inicio="";
const int pinCS = 10;
const int numberOfHorizontalDisplays = 8;
const int numberOfVerticalDisplays = 1;
String bt;
//String cadena[]={"holamundooooooooo"};  //textos
int i=0;
Max72xxPanel matrix = Max72xxPanel(pinCS, numberOfHorizontalDisplays, numberOfVerticalDisplays);

const int wait = 25; // Velocidad a la que realiza el scroll(Velocidad a la que se muestra la frase)

const int spacer = 1;
const int width = 5 + spacer; // Ancho de la fuente a 5 pixeles



void setup()
   {
       Serial.begin(9600);
       Serial.println("Enter AT commands:");
       BT1.begin(9600);

        matrix.setIntensity(15); // Ajustar el brillo entre 0 y 15

 // Ajustar segun las necesidades
 matrix.setPosition(0, 0, 0); // El primer display esta en <0, 0>
 matrix.setPosition(1, 1, 0); // El segundo display esta en <1, 0>
 matrix.setPosition(2, 2, 0); // El tercer display esta en <2, 0>
 matrix.setPosition(3, 3, 0); // El cuarto display esta en <3, 0>
 matrix.setPosition(4, 4, 0); // El quinto display esta en <4, 0>
 matrix.setPosition(5, 5, 0); // El sexto display esta en <5, 0>
 matrix.setPosition(6, 6, 0); // El séptimo display esta en <6, 0>
 matrix.setPosition(7, 7, 0); // El octavo display esta en <7, 0>
 matrix.setPosition(8, 8, 0); // El noveno display esta en <8, 0>

 matrix.setRotation(0, 1);    // Posición del display
 matrix.setRotation(1, 1);    // Posición del display
 matrix.setRotation(2, 1);    // Posición del display
 matrix.setRotation(3, 1);    // Posición del display
 matrix.setRotation(4, 1);    // Posición del display
 matrix.setRotation(5, 1);    // Posición del display
 matrix.setRotation(6, 1);    // Posición del display
 matrix.setRotation(7, 1);    // Posición del display
 matrix.setRotation(8, 1);    // Posición del display


 char bt[]={"prueba"} ;
   }

void loop()
   {
 //bluetooth recoger datos
 if(BT1.available()){
  bt="";
 }
 int i=0;
  while(BT1.available()){
       // Serial.write(BT1.read());
        char c=(char)BT1.read();
        if((c=='a')||(c=='b')||(c=='c')||(c=='d')||(c=='e')||(c=='f')||(c=='g')||(c=='h')||(c=='i')||(c=='j')||(c=='k')||(c=='l')||(c=='m')||(c=='n')||(c=='ñ')
        ||(c=='o')||(c=='p')||(c=='q')||(c=='r')||(c=='s')||(c=='t')||(c=='u')||(c=='v')||(c=='w')||(c=='x')||(c=='y')||(c=='z')||
          (c=='A')||(c=='B')||(c=='C')||(c=='D')||(c=='E')||(c=='F')||(c=='G')||(c=='H')||(c=='I')||(c=='J')||(c=='K')||(c=='L')||(c=='M')||(c=='N')||(c=='Ñ')
        ||(c=='O')||(c=='P')||(c=='Q')||(c=='R')||(c=='S')||(c=='T')||(c=='U')||(c=='V')||(c=='W')||(c=='X')||(c=='Y')||(c=='Z')||(c==' ')){
          bt=bt+char(c);

          writeByte(EEPROM_I2C_ADDRESS,i, (byte)c);

          i++;
          delay(50);


        }else{
          bt=bt;
        }
  }
  Serial.println(bt);



String palabrasacada;
for(int i=0; i<bt.length(); i++){
  byte z=readByte(EEPROM_I2C_ADDRESS,i);
  char a=char(z);
  palabrasacada=palabrasacada+String(a);
}

String cadena[]={palabrasacada};
int n = random(0,1);

 for (int i = 0; i < width * cadena[n].length() + matrix.width() - 1 - spacer; i++) {

 matrix.fillScreen(LOW);

 int letter = i / width;
 int x = (matrix.width() - 1) - i % width;
 int y = (matrix.height() - 8) / 2; // Centrar el texto

 while (x + width - spacer >= 0 && letter >= 0) {
 if (letter < cadena[n].length()) {
 matrix.drawChar(x, y, cadena[n][letter], HIGH, LOW, 1);
 }

 letter--;
 x -= width;
 }
 matrix.write(); // Muestra loscaracteres
 delay(wait);
 }
}


void writeByte(int device, unsigned int add, byte data)
{
  Wire.beginTransmission(device);
  Wire.write((int)(add >> 8)); // left-part of pointer address
  Wire.write((int)(add & 0xFF)); // and the right
  Wire.write(data);
  Wire.endTransmission();
  delay(5);//small delay for eeprom to save data
}


// reads a byte of data to the I2C address 'device', in memory location 'add'
byte readByte(int device, unsigned int add)
{
  Wire.beginTransmission(device); // I2C address
  Wire.write((int)(add >> 8)); // bit shift for high byte of pointer address
  Wire.write((int)(add & 0xFF)); // mask for the low byte
  Wire.endTransmission();
  Wire.requestFrom(device, 1); 
  return Wire.read();
}

The led matrix and bluetooth receiving actually works, but i'm adding them anyways, just in case

  • I don't know the answer, but you can change the big if statement: ` if((c=='a')||(c=='b')||(c=='c')||(c=='d')||(c=='e')||(c=='f')||(c=='g')||(c=='h')||(c=='i')||(c=='j')||(c=='k')||(c=='l')||(c=='m')||(c=='n')||(c=='ñ') ||(c=='o')||(c=='p')||(c=='q')||(c=='r')||(c=='s')||(c=='t')||(c=='u')||(c=='v')||(c=='w')||(c=='x')||(c=='y')||(c=='z') .... == ' '))` – Michel Keijzers Jul 15 at 15:13
  • by something like if (((c >= 'a') && (c <= 'z')) || (c=='ñ') || ((c >= 'A') && (c <= 'Z')) || (c=='Ñ') || (c == ' ')) – Michel Keijzers Jul 15 at 15:13
  • @MichelKeijzers You overlooked 'ñ' which actually isn't an ASCII character anyway... – Majenko Jul 15 at 15:18
  • @Majenko I fixed it before reading your comment but you are absolutely right – Michel Keijzers Jul 15 at 15:21
  • You're right, thank you – Draken Jul 15 at 15:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.