Hi, all. This is my first post here, so please bear with me.
I have some months of experience with Arduino, so not much. But I have a background in Electronics and do a lot of coding for work, so I've learnt relatively quickly.
I am working on a low-power project. I'm planning to use an ATTiny85 which controls a DS3231 real-time clock (RTC) using I2C and is woken by the RTC's alarms. I2C needs SDA
(PB0
) and SCL
(PB2
). The only hardware interrupt is INT0
(PB2
).
This means that I have two options (I think):
- Use
INT0
(PB2
) for the alarm and bit-banging for I2C (using, e.g., BitBang_I2C) - Use
PB0
andPB2
for I2C and a pin-change interrupt (PCINT
) for the alarm
I haven't been able to make option 1 work: I'm not even able to set and read the time correctly.
Option 2 should be easier. I have been having some issues with the PCINT
s, so first I want to make my testing code with INT0
. I'll probably ask about PCINT
s soon, in different post.
I've written a sketch that adds 1 to a counter
and sets a flag when the signal at PB2
falls to LOW
. In the loop, an LED blinks counter
times and unsets the flag:
#include <Arduino.h>
const byte LED = 3;
const byte LED_FLAG = 0;
const byte SWITCH = 2;
volatile byte count;
boolean flag = false;
void blink(int);
void isr() {
count++;
flag = true;
}
void setup () {
pinMode(LED, OUTPUT);
pinMode(LED_FLAG, OUTPUT);
pinMode(SWITCH, INPUT);
attachInterrupt(digitalPinToInterrupt(SWITCH), isr, FALLING);
blink(5);
count = 2;
}
void loop () {
// digitalWrite(LED_FLAG, flag);
if (flag) {
blink(count);
flag = false;
}
}
void blink(int n) {
for (int i = 0; i < n; i++) {
digitalWrite(LED, HIGH);
delay (100);
digitalWrite(LED, LOW);
delay (100);
}
}
I'm using PlatformIO to upload the code through an Arduino Nano as ISP. The signal from SWITCH
is actually from pin 13 of an Arduino Mega with a blink sketch modified to change every five seconds, instead of one. All grounds are connected, and power comes from the 5V pin of the Nano, which is connected to the computer through USB. The ATTiny85 clock is set to 8 MHz (fuses: E:FF, H:D7, L:E2
).
PB3
is connected to a white LED (the one that blinks), which is connected to GND through a 330 ohm resistor.
PB0
is connected to a red LED (the one that shows the state of flag
), which is connected to GND through a 330 ohm resistor.
The mystery is that the sketch works only if I uncomment the commented line, i.e., if I write to the flag
(red) LED. The white LED blinks 5 times at the beginning, and then blinks counter
times every time the built-in LED of the Mega turns off.
If I comment that line, the white LED blinks 5 times at setup
and never again.
Here's what the breadboard looks like. The yellow cable is connected to pin 13 of the Mega. Not sure why the white LED looks funny.
Do you know what could be happening? Am I doing anything obviously wrong? I feel like I need to understand what's going on here before I move forward to the PCINT
s.