In the project that I'm currently pursuing, I have a D1 Mini that I am using as a master for I2C communications, while the ATtiny is the slave. I am using a 4x3 matrix keypad on the ATtiny and sending the entered keys via I2C.
In this case, the D1 Mini constantly asks the ATtiny for a bit, and waits for the bits between 0 to 9.
However, after a few (random number, sometimes 35, sometimes 105, sometimes 170) entries are sent, ATtiny stops transmitting. I see a repeated string of 'Byte requested.' on the serial of the master without any byte being received.
I can't figure out what's happening and would really appreciate some help to solve this problem.
Thanks, in advance!
Master code:
#include <Wire.h>
#define S1_ADDR 0x14
int buzzPin = 5; //Buzzer D1
int keyent = 0;
int keycount = 0;
int keytries = 0;
void setup()
{
Serial.begin(115200);
Wire.begin(0, 2); //D3, D4
Serial.println("");
Serial.println("Door module.");
pinMode(buzzPin, OUTPUT);
}
void loop()
{
keyReq();
delay(150);
if(keycount == 4)
{
longBuzz();
keytries++;
Serial.println("Key obtained.");
Serial.println(keyent);
keycount = 0;
keyent = 0;
}
}
//Key request function
void keyReq()
{
Serial.println("Byte requested.");
Wire.requestFrom(S1_ADDR, 1);
if(Wire.available())
{
byte byteReceived = Wire.read();
Serial.println(byteReceived);
if(byteReceived < 10)
{
//Serial.println(byteReceived);
keyent = keyent * 10 + byteReceived;
blinkBuzz();
Serial.println(keyent);
keycount++;
}
}
}
//Buzzer short beep
void blinkBuzz()
{
digitalWrite(buzzPin, HIGH);
delay(50);
digitalWrite(buzzPin, LOW);
delay(50);
}
Slave code:
#include "TinyWireS.h"
#define SLAVE_ADDR 0x14
//byte byteToSend = 0;
int r1=0;
int r2=1;
int r3=4;
int r4=5;
int c1=6;
int c2=7;
int c3=8;
int i = 33;
void setup()
{
TinyWireS.begin(SLAVE_ADDR);
TinyWireS.onRequest(requestEvent);
pinMode(r1,OUTPUT);
pinMode(r2,OUTPUT);
pinMode(r3,OUTPUT);
pinMode(r4,OUTPUT);
pinMode(c1,INPUT);
pinMode(c2,INPUT);
pinMode(c3,INPUT);
}
void loop()
{
digitalWrite(c1,HIGH);
digitalWrite(c2,HIGH);
digitalWrite(c3,HIGH);
//checking everything one by one
//case 1: row1 =0 while other rows are 1
digitalWrite(r1,LOW);
digitalWrite(r2,HIGH);
digitalWrite(r3,HIGH);
digitalWrite(r4,HIGH);
//checking each column for row1 one by one
if(digitalRead(c1)==0)
{
i = 1;
}
else if(digitalRead(c2)==0)
{
i = 2;
}
else if(digitalRead(c3)==0)
{
i = 3;
}
//case 2: row2 =0 while other rows are 1
digitalWrite(r1,HIGH);
digitalWrite(r2,LOW);
digitalWrite(r3,HIGH);
digitalWrite(r4,HIGH);
//checking each column for row1 one by one
if(digitalRead(c1)==0)
{
i = 4;
}
else if(digitalRead(c2)==0)
{
i = 5;
}
else if(digitalRead(c3)==0)
{
i = 6;
}
//case 3: row3 =0 while other rows 1
digitalWrite(r1,HIGH);
digitalWrite(r2,HIGH);
digitalWrite(r3,LOW);
digitalWrite(r4,HIGH);
//checking each column for row1 one by one
if(digitalRead(c1)==0)
{
i = 7;
}
else if(digitalRead(c2)==0)
{
i = 8;
}
else if(digitalRead(c3)==0)
{
i = 9;
}
//case 4: row4 =0 while other rows are 1
digitalWrite(r1,HIGH);
digitalWrite(r2,HIGH);
digitalWrite(r3,HIGH);
digitalWrite(r4,LOW);
//checking each column for row4 one by one
if(digitalRead(c1)==0)
{
i = 11;
}
else if(digitalRead(c2)==0)
{
i = 0;
}
else if(digitalRead(c3)==0)
{
i = 12;
}
//giving delay between keypress
delay(50);
TinyWireS_stop_check();
}
void requestEvent()
{
TinyWireS.send(i);
i=33;
}