#include<avr/io.h>
#include <avr/interrupt.h>
#include <TinyWireM.h>
#include <Tiny4kOLED.h>
#include "font16x32digits.h"
// Define pins for switch the LED, plus the chosen interrupt for reacting to
#define INTERRUPT_PIN PCINT3 // This is PB3 per the schematic
#define INT_PIN PB3 // Interrupt pin of choice: PB3 (same as PCINT3) - Pin 3
#define LED_PIN PB1 // PB4 - Pin 3
#define clock 1 // Clock sur PB1
#define RESET_PIN PB4
volatile int cpt = 0;
int var = 0;
String nombre = "000";
/*
* Alias for the ISR: "PCINT_VECTOR" (Note: There is only one PCINT ISR.
* PCINT0 in the name for the ISR was confusing to me at first,
* hence the Alias, but it's how the datasheet refers to it)
*/
#define PCINT_VECTOR PCINT0_vect // This step is not necessary - it's a naming thing for clarity
ISR(PCINT_VECTOR)
{
if( digitalRead(INT_PIN) == HIGH ) {
digitalWrite(LED_PIN, HIGH);
cpt++;
}else if( digitalRead(RESET_PIN) == LOW ) {
digitalWrite(LED_PIN, LOW);
cpt=0;
}
/*
cpt++;
//cli();
if( digitalRead(clock) == LOW ) {
digitalWrite(LED_PIN, HIGH);
cpt = cpt/2; // On divise cpt par 2 car ISR(PCINT_VECTOR) est déclenché sur le front montant et descendant de l'interruption
// Le nombre d'impulsions est donc multiplé par deux
//Affich();
_delay_ms( 450 ); // délai de 200 ms
cpt=0;
}else{
digitalWrite(LED_PIN, LOW);
}
// sei();
*/
}
void setup()
{
pinMode(LED_PIN, OUTPUT); // Set our chosen LED visual feedback as an output pin (PB4 / Pin 4)
//pinMode(pulses, INPUT);
pinMode(RESET_PIN, INPUT_PULLUP);
pinMode(clock, OUTPUT);
oled.begin(128, 64, sizeof(tiny4koled_init_128x64br), tiny4koled_init_128x64br);
// Two fonts are supplied with this library, FONT8X16 and FONT6X8
oled.setFont(FONT6X8);
// To clear all the memory
oled.clear();
oled.on();
cli(); // Disable interrupts during setup
PCMSK |= (1 << INTERRUPT_PIN); // Enable interrupt handler (ISR) for our chosen interrupt pin (PCINT3/PB3/pin 3)
PCMSK |= (1 << RESET_PIN);
//MCUCR |= (1 << ISC01)|(1 << ISC00); // The rising edge of INT0 generates an interrupt request.
GIMSK |= (1 << PCIE); // Enable PCINT interrupt in the general interrupt mask
pinMode(INT_PIN, INPUT); // Set our interrupt pin as input with a pullup to keep it stable
sei(); //last line of setup - enable interrupts after setup
init_Display();
}
void loop()
{
Affich();
}
void init_Display() {
oled.clear();
oled.setFont(FONT8X16P);
oled.setCursor(5, 0);
oled.print("Blue Ring Tester");
oled.setCursor(8, 1);
oled.setFont(FONT16X32DIGITS);
oled.setCursor(42, 4);
oled.print(cpt);
}
void Affich() {
nombre = String(cpt);
var = nombre.length();
switch (var) {
case 1:
nombre = "00" + String(cpt);
break;
case 2:
nombre = "0" + String(cpt);
break;
default:
nombre = String(cpt);
break;
}
oled.setFont(FONT16X32DIGITS);
oled.setCursor(42, 4);
oled.clearToEOL();
//oled.clear();
oled.setCursor(42, 4);
oled.print(nombre);
}