Macropad - Arduino mini pro I2C communication

Hi i'm doing a project where i use an adafruit macropad that sends a code (integer) to an arduino mini pro using I2C (sda+scl).

Transmitter Macropad code:

#include <Adafruit_SH110X.h>
#include <Adafruit_NeoPixel.h>
#include <RotaryEncoder.h>
#include <Wire.h>


// Create the neopixel strip with the built in definitions NUM_NEOPIXEL and PIN_NEOPIXEL

Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUM_NEOPIXEL, PIN_NEOPIXEL, NEO_GRB + NEO_KHZ800);

// Create the OLED display
Adafruit_SH1106G display = Adafruit_SH1106G(128, 64, &SPI1, OLED_DC, OLED_RST, OLED_CS);

// Create the rotary encoder
RotaryEncoder encoder(PIN_ROTA, PIN_ROTB, RotaryEncoder::LatchMode::FOUR3);
void checkPosition() {  encoder.tick(); } // just call tick() to check the state.
// our encoder position state
int encoder_pos = 0;
int flag = 0; //flag per determinare se si era già in quella pagina e per pulirla.

void setup() {
  //delay(100);  // RP2040 delay is not a bad idea
  Serial.println("Adafruit Macropad with RP2040");

  // start pixels!
  pixels.begin();
  pixels.setBrightness(255);
  pixels.show(); // Initialize all pixels to 'off'

  // Start OLED
  display.begin(0, true); // we dont use the i2c address but we will reset!
  display.display();
 
  // set all mechanical keys to inputs
  for (uint8_t i=0; i<=12; i++) {
    pinMode(i, INPUT_PULLUP);
  }

  // set rotary encoder inputs and interrupts
  pinMode(PIN_ROTA, INPUT_PULLUP);
  pinMode(PIN_ROTB, INPUT_PULLUP);
  attachInterrupt(digitalPinToInterrupt(PIN_ROTA), checkPosition, CHANGE);
  attachInterrupt(digitalPinToInterrupt(PIN_ROTB), checkPosition, CHANGE);  

  // I2C Start
  Wire.begin();

  // text display tests
  display.setTextSize(1);
  display.setTextWrap(false);
  display.setTextColor(SH110X_WHITE, SH110X_BLACK);// white text, black background

  display.setCursor(0,0);
  display.println("* Led Controller *");
  
  // Enable speaker
  pinMode(PIN_SPEAKER_ENABLE, OUTPUT);
  digitalWrite(PIN_SPEAKER_ENABLE, HIGH);
  // Play some tones
  pinMode(PIN_SPEAKER, OUTPUT);
  digitalWrite(PIN_SPEAKER, LOW);
  tone(PIN_SPEAKER, 988, 100);  // tone1 - B5
  delay(100);
  tone(PIN_SPEAKER, 1319, 200); // tone2 - E6
  delay(200);
  display.clearDisplay();
}

uint8_t j = 0;
bool i2c_found[128] = {false};

void loop() {
  display.display();

  encoder.tick();          // check the encoder
  int newPos = -encoder.getPosition();
  if (encoder_pos != newPos) {
    Serial.print("Encoder:");
    Serial.print(newPos);
    Serial.print(" Direction:");
    Serial.println((int)(encoder.getDirection()));
    encoder_pos = newPos;
  }

  

  switch (newPos) {
  case 0:
      if(flag!=1) display.clearDisplay();
      display.setTextWrap(false);
      display.setCursor( 2, 0);
      display.println("Rosso");
      display.setCursor( 40, 0);
      display.println("Verde");
      display.setCursor( 90, 0);
      display.println("Blu");
      display.setCursor( 2, 10);
      display.println("4");
      display.setCursor( 44, 10);
      display.println("5");
      display.setCursor( 80, 10);
      display.println("6");
      display.setCursor( 2, 20);
      display.println("7");
      display.setCursor( 40, 20);
      display.println("8");
      display.setCursor( 90, 20);
      display.println("9");
      display.setCursor( 2, 40);
      display.println("10");
      display.setCursor( 44, 40);
      display.println("11");
      display.setCursor( 80, 40);
      display.println("12");
      flag=1;
    break;
  case 1:
          if(flag!=2) display.clearDisplay();
      display.setTextWrap(false);
      display.setCursor( 2, 0);
      display.println("Giallo");
      display.setCursor( 40, 0);
      display.println("2B");
      display.setCursor( 90, 0);
      display.println("3B");
      display.setCursor( 2, 10);
      display.println("4B");
      display.setCursor( 44, 10);
      display.println("5B");
      display.setCursor( 80, 10);
      display.println("6B");
      display.setCursor( 2, 20);
      display.println("7B");
      display.setCursor( 40, 20);
      display.println("8B");
      display.setCursor( 90, 20);
      display.println("9B");
      display.setCursor( 2, 40);
      display.println("10B");
      display.setCursor( 44, 40);
      display.println("11B");
      display.setCursor( 80, 40);
      display.println("12B");
      flag=2;
    break;

 case 2:
          if(flag!=3) display.clearDisplay();
      display.setTextWrap(false);
      display.setCursor( 2, 0);
      display.println("Giallo");
      display.setCursor( 40, 0);
      display.println("2B");
      display.setCursor( 90, 0);
      display.println("3B");
      display.setCursor( 2, 10);
      display.println("4B");
      display.setCursor( 44, 10);
      display.println("5B");
      display.setCursor( 80, 10);
      display.println("6B");
      display.setCursor( 2, 20);
      display.println("7B");
      display.setCursor( 40, 20);
      display.println("meow");
      display.setCursor( 90, 20);
      display.println("9B");
      display.setCursor( 2, 40);
      display.println("10B");
      display.setCursor( 44, 40);
      display.println("11B");
      display.setCursor( 80, 40);
      display.println("12B");
      flag=3;
    break;
    
  default:
    flag = 0;
    for (int i=1; i<=12; i++) pixels.setPixelColor(i-1, 0);
    display.clearDisplay();
    break;
}

  
    
//  for(int i=0; i< pixels.numPixels(); i++) {
 //   pixels.setPixelColor(i, Wheel(((i * 256 / pixels.numPixels()) + j) & 255));
 //                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            }

  if (newPos == 0){
    pixels.setPixelColor(0, 255, 0, 255);
    Serial.println("pos 0");
    if (!digitalRead(1)) {
      Wire.beginTransmission(10); // transmit to device #1
      Wire.write(1);              // sends x 
      Serial.println("Trasmissione I2C");
      Wire.endTransmission();    // stop transmitting
    }
    pixels.setPixelColor(1, 255, 0, 255);
    pixels.setPixelColor(2, 255, 0, 255);
    pixels.setPixelColor(3, 255, 0, 255);
    pixels.setPixelColor(4, 255, 0, 255);
    pixels.setPixelColor(5, 255, 0, 255);
    pixels.setPixelColor(6, 255, 0, 255);
    pixels.setPixelColor(7, 255, 0, 255);
    pixels.setPixelColor(8, 255, 0, 255);
    pixels.setPixelColor(9, 255, 0, 255);
    pixels.setPixelColor(10, 255, 0, 255);
    pixels.setPixelColor(11, 255, 0, 255);
    }
    
  if (newPos == 1){
    pixels.setPixelColor(0, 0, 255, 255);
    pixels.setPixelColor(1, 255, 0, 255);
    pixels.setPixelColor(2, 255, 255, 0);
    pixels.setPixelColor(3, 255, 0, 255);
    pixels.setPixelColor(4, 255, 0, 255);
    pixels.setPixelColor(5, 255, 0, 255);
    pixels.setPixelColor(6, 255, 0, 255);
    pixels.setPixelColor(7, 255, 0, 255);
    pixels.setPixelColor(8, 255, 0, 255);
    pixels.setPixelColor(9, 255, 0, 255);
    pixels.setPixelColor(10, 255, 0, 255);
    pixels.setPixelColor(11, 255, 0, 255);
  }

    if (newPos == 2){
    pixels.setPixelColor(0, 0, 255, 255);
    pixels.setPixelColor(1, 255, 0, 255);
    pixels.setPixelColor(2, 255, 255, 0);
    pixels.setPixelColor(3, 255, 0, 255);
    pixels.setPixelColor(4, 40, 0, 255);
    pixels.setPixelColor(5, 255, 0, 255);
    pixels.setPixelColor(6, 255, 0, 255);
    pixels.setPixelColor(7, 255, 0, 255);
    pixels.setPixelColor(8, 100, 0, 90);
    pixels.setPixelColor(9, 255, 0, 255);
    pixels.setPixelColor(10, 255, 0, 255);
    pixels.setPixelColor(11, 80, 90, 100);
  }

 
  for (int i=1; i<=12; i++) {
    if (!digitalRead(i)) { // switch pressed!
      tone(PIN_SPEAKER, 20, 100);  // tone1 - B5
      //delay(100);
      Serial.print("Switch "); Serial.println(i);
      pixels.setPixelColor(i-1, 0xFFFFFF);  // make white
      // move the text into a 3x4 grid
    }
  }


  // show neopixels, incredment swirl
  pixels.show();

  // display oled
  display.display();
}

Reciver arduini pro mini code:

#include <Wire.h>
#include <LoRa.h> 
#include <SPI.h>

int x = 0;
 
void setup() {
  Wire.begin(10);                // join I2C bus with address #8
  Wire.onReceive(receiveEvent); // register event
  Serial.begin(9600);           // start serial for output
}

void loop() {
    Serial.println(x);
    delay(100);
  }
//  LoRa.beginPacket();
//  LoRa.print(recived);
//  LoRa.endPacket();

void receiveEvent(int bytes) {
  x = Wire.read();    // read one character from the I2C
}

When i click the button, the reciver obtain squares and question marks; can you help me to find the mistake.

I moved your topic to an appropriate forum category @andreadp271.

In the future, please take some time to pick the forum category that best suits the subject of your topic. There is an "About the _____ category" topic at the top of each category that explains its purpose.

This is an important part of responsible forum usage, as explained in the "How to get the best out of this forum" guide. The guide contains a lot of other useful information. Please read it.

Thanks in advance for your cooperation.

thank you

1 Like

Edit: Are you certain there is a pullup resistor on the SDA and SCL lines?

I don't know, in the adafruit documentation I didn't find the pull-up resistor. The i2c port is used for their system STEMMA QT: they connect directly master ti slaves

void loop() {
   // delay(100);
  }
//  LoRa.beginPacket();
//  LoRa.print(recived);
//  LoRa.endPacket();

void receiveEvent(int bytes) {
  x = Wire.read();    // read one byte from the I2C, not character
    Serial.println(x, HEX);
}

it doesen't work, same error

it can physically not be same error

Try this (thanks to jim-p)
volatile int x = 0;