Buongiorno mi sto dedicando da pochi mesi ad arduino.
fatti esperimenti più semplici ora sto cercando di usare un encoder rotativo con una eeprom esterna. ho fatto il codice non mi da errore ma il monitor non mi rimanda nessun dato ed è fermo a zero.
di seguito posto il codice da me creato, spero che qualcuno riesca a darmi una mano.
grazie mille.
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define SCREEN_WIDTH 128 // OLED display dimensioni laghezza
#define SCREEN_HEIGHT 64 // OLED display dimensioni altezza
#define OLED_RESET -1 // Reset pin
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
#define eeprom 0x50
// encoder
#define encoderPinA 2 //filo verde
#define encoderPinB 3 // filo bianco
#define BT_HOME 6
//conversione
float STEP2MM = 1.5;
volatile long pos = 0;
void setup() {
Serial.begin(9600);
pinMode(encoderPinA, INPUT_PULLUP);
pinMode(encoderPinB, INPUT_PULLUP);
pinMode(BT_HOME, INPUT);
//variabile tipo
attachInterrupt(0, doEncoderA, CHANGE);
attachInterrupt(1, doEncoderB, CHANGE);
//display setup
if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F("SSD1306 allocation failed")); //messaggio di errore
for(;;);
}
display.setTextSize(2); // dimensione testo
display.setTextColor(WHITE); // colore testo
display.cp437(true);
display.clearDisplay();
}
unsigned long dt, t1;
float dist = 0;
void loop() {
Serial.begin(9600);
Wire.begin();
dist = (float)pos * STEP2MM;
Serial.print(pos);
Serial.print(" - ");
Serial.println(dist);
dt = millis() - t1;
if (dt > 50) {
char somedata = dist; // data to write
Wire.begin(); // initialise the connection
i2c_eeprom_write_page(0x50, 0, (byte *)somedata, sizeof(somedata)); // write to EEPROM
delay(100); //add a small delay
Serial.println("Memory written");
int addr=0; //first address
byte b = i2c_eeprom_read_byte(0x50, 0); // access the first address from the memory
while (b!=0)
{
Serial.print((char)b); //print content to serial port
addr++; //increase address
b = i2c_eeprom_read_byte(0x50, addr); //access an address from the memory
}
Serial.println(" ");
display.clearDisplay();
display.setCursor(0, 0);
display.print(somedata);
display.print(" mm");
display.display();
t1 = millis();
if (digitalRead(BT_HOME)) {
pos = 0;
}
}
}
//funzione calcolo encoder
void doEncoderA() {
if (digitalRead(encoderPinA) == HIGH) {
if (digitalRead(encoderPinB) == LOW) {
pos = pos + 1;
} else {
pos = pos - 1;
}
} else {
if (digitalRead(encoderPinB) == HIGH) {
pos = pos + 1;
} else {
pos = pos - 1;
}
}
}
void doEncoderB() {
if (digitalRead(encoderPinB) == HIGH) {
if (digitalRead(encoderPinA) == HIGH) {
pos = pos + 1;
} else {
pos = pos - 1;
}
} else {
if (digitalRead(encoderPinA) == LOW) {
pos = pos + 1;
} else {
pos = pos - 1;
}
}
}
void i2c_eeprom_write_byte( int deviceaddress, unsigned int eeaddress, byte data ) {
int rdata = data;
Wire.beginTransmission(deviceaddress);
Wire.write((int)(eeaddress >> 8)); // MSB
Wire.write((int)(eeaddress & 0xFF)); // LSB
Wire.write(rdata);
Wire.endTransmission();
}
// WARNING: address is a page address, 6-bit end will wrap around
// also, data can be maximum of about 30 bytes, because the Wire library has a buffer of 32 bytes
void i2c_eeprom_write_page( int deviceaddress, unsigned int eeaddresspage, byte* data, byte length ) {
Wire.beginTransmission(deviceaddress);
Wire.write((int)(eeaddresspage >> 8)); // MSB
Wire.write((int)(eeaddresspage & 0xFF)); // LSB
byte c;
for ( c = 0; c < length; c++)
Wire.write(data[c]);
Wire.endTransmission();
}
byte i2c_eeprom_read_byte( int deviceaddress, unsigned int eeaddress ) {
byte rdata = 0xFF;
Wire.beginTransmission(deviceaddress);
Wire.write((int)(eeaddress >> 8)); // MSB
Wire.write((int)(eeaddress & 0xFF)); // LSB
Wire.endTransmission();
Wire.requestFrom(deviceaddress,1);
if (Wire.available()) rdata = Wire.read();
return rdata;
}
// maybe let's not read more than 30 or 32 bytes at a time!
void i2c_eeprom_read_buffer( int deviceaddress, unsigned int eeaddress, byte *buffer, int length ) {
Wire.beginTransmission(deviceaddress);
Wire.write((int)(eeaddress >> 8)); // MSB
Wire.write((int)(eeaddress & 0xFF)); // LSB
Wire.endTransmission();
Wire.requestFrom(deviceaddress,length);
int c = 0;
for ( c = 0; c < length; c++ )
if (Wire.available()) buffer[c] = Wire.read();
}