#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
#define OLED_RESET 4 // Reset pin # (or -1 if sharing Arduino reset pin)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
#define NUMFLAKES 10 // Number of snowflakes in the animation example
#define LOGO_HEIGHT 16
#define LOGO_WIDTH 16
static const unsigned char PROGMEM logo_bmp[] =
{ B00000000, B11000000,
B00000001, B11000000,
B00000001, B11000000,
B00000011, B11100000,
B11110011, B11100000,
B11111110, B11111000,
B01111110, B11111111,
B00110011, B10011111,
B00011111, B11111100,
B00001101, B01110000,
B00011011, B10100000,
B00111111, B11100000,
B00111111, B11110000,
B01111100, B11110000,
B01110000, B01110000,
B00000000, B00110000 };
void display_init() {
// SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally
if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3D)) { // Address 0x3D for 128x64
Serial.println(F("SSD1306 allocation failed"));
for(;;); // Don't proceed, loop forever
}
// Show initial display buffer contents on the screen --
// the library initializes this with an Adafruit splash screen.
display.display();
delay(2000); // Pause for 2 seconds
// Clear the buffer
display.clearDisplay();
}
void display_count(int count) {
display.setTextSize(1); // Normal 1:1 pixel scale
display.setTextColor(SSD1306_WHITE); // Draw white text
display.setCursor(0,0); // Start at top-left corner
display.print(F("count : "));
display.setTextColor(SSD1306_BLACK, SSD1306_WHITE); // Draw 'inverse' text
display.println(count);
}
void display_set_count(int match_count) {
display.setTextSize(1); // Normal 1:1 pixel scale
display.setTextColor(SSD1306_WHITE); // Draw white text
display.setCursor(0,10); // Start at top-left corner
display.print(F("match count : "));
display.setTextColor(SSD1306_BLACK, SSD1306_WHITE); // Draw 'inverse' text
display.println(match_count);
}
void display_refresh() {
display.display();
}
void clear_display() {
display.clearDisplay();
}
//pins
const int buzzer = 8; // Buzzer is connected to pin 8
const int motor = 9; // motor is connected to pin 9
const int sensor = 2; // Button is connected to pin 2
const int inc_buttonPin = 6; // Button is connected to pin 6
const int dec_buttonPin = 5; // Button is connected to pin 5
const int reset_buttonPin = 4; // Button is connected to pin 4
const int start_buttonPin = 3; // Button is connected to pin 3
//variables
volatile int count = 0; // The count of button presses
volatile bool sensorState = HIGH; // The current state of the button
volatile bool inc_buttonState = HIGH; // The current state of the button
volatile bool dec_buttonState = HIGH; // The current state of the button
volatile bool reset_buttonState = HIGH; // The current state of the button
volatile bool start_buttonState = HIGH; // The current state of the button
volatile bool start_flag = LOW; // The current state of the button
volatile int match_count = 0;
void button_init() {
pinMode(inc_buttonPin, INPUT_PULLUP);
pinMode(dec_buttonPin, INPUT_PULLUP);
pinMode(reset_buttonPin, INPUT_PULLUP);
pinMode(start_buttonPin, INPUT_PULLUP);
}
void pin_ISR() { // Get the new button state
bool newState = digitalRead(sensor);
delay(50);
// If the button state has changed
if ((newState != sensorState) && (start_flag == HIGH)) {
// Update the button state
sensorState = newState;
//digitalWrite(buzzer, newState);
// If the button was pressed (i.e. the state is LOW)
if (sensorState == LOW) {
// Increment the count
count++;
}
}
}
void counter_init() {
pinMode(sensor, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(sensor), pin_ISR, CHANGE);
}
void button_status() {
inc_buttonState = digitalRead(inc_buttonPin);
dec_buttonState = digitalRead(dec_buttonPin);
reset_buttonState = digitalRead(reset_buttonPin);
start_buttonState = digitalRead(start_buttonPin);
// check if the button is pressed
if (inc_buttonState == LOW) {
// print a message to the serial monitor
Serial.println("inc Button is pressed!");
match_count++;
delay(250);
}
if (dec_buttonState == LOW) {
// print a message to the serial monitor
Serial.println("dec Button is pressed!");
match_count--;
delay(250);
}
if (reset_buttonState == LOW) {
// print a message to the serial monitor
Serial.println("reset Button is pressed!");
match_count = 0;
delay(250);
}
if (start_buttonState == LOW) {
Serial.println("start Button is pressed!");
start_flag = HIGH;
delay(250);
}
}
void start_motor(void) {
digitalWrite(motor, LOW);
Serial.println("start motor");
}
void stop_motor(void) {
digitalWrite(motor, HIGH);
Serial.println("stop motor");
}
void setup() {
pinMode(buzzer, OUTPUT);
pinMode(motor, OUTPUT);
stop_motor();
display_init();
counter_init();
button_init();
Serial.begin(9600);
}
void loop() {
button_status();
clear_display();
display_count(count);
display_set_count(match_count);
display_refresh();
if ((count>=match_count) && (match_count!=0)) {
digitalWrite(buzzer, HIGH);
stop_motor();
start_flag = LOW;
delay(2000);
count = 0;
digitalWrite(buzzer, LOW);
}
if (start_flag == HIGH) {
start_motor();
}
}