Skip to content

Instantly share code, notes, and snippets.

@Adirockzz95
Created July 26, 2017 18:23
Show Gist options
  • Save Adirockzz95/a330857897bbc7ca3f7080eea7ed8dc7 to your computer and use it in GitHub Desktop.
Save Adirockzz95/a330857897bbc7ca3f7080eea7ed8dc7 to your computer and use it in GitHub Desktop.
LED Matrix Count program
/**
* Program name: This program is part of the project Pen Stand: LED Enabled
* Author: Aditya K
* Date: 7/1/2017
* LICENSE: MIT
*
* Discription: This program counts how many times a Pen is removed from the
the stand and prints it on LED Matrix display.
*
* Copyright (c) 2017 Aditya K.
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include <MD_Parola.h>
#include <MD_MAX72xx.h>
#include <SPI.h>
#include <avr/interrupt.h>
#include <avr/power.h>
#include <avr/sleep.h>
#define PEN_PIN 2
#define MAX_DEVICES 3
#define CLK_PIN 13
#define DATA_PIN 11
#define CS_PIN 10
MD_Parola P = MD_Parola(CS_PIN, MAX_DEVICES);
volatile int pen_count = 1;
volatile bool int_flag = false;
char num[999] = {'\0'};
void deep_sleep() {
set_sleep_mode(SLEEP_MODE_PWR_DOWN);
sleep_enable();
sleep_mode();
sleep_cpu();
}
void animate() {
itoa(pen_count, num, 10);
P.displayShutdown(false);
P.print(num);
delay(350);
P.displayClear();
pen_count++;
P.displayShutdown(true);
if(pen_count == 999) pen_count = 0;
}
void animation_ISR() {
sleep_disable();
int_flag = true;
}
void setup() {
pinMode(PEN_PIN, INPUT_PULLUP);
power_usart0_disable();
power_twi_disable();
ADCSRA = 0;
power_adc_disable();
P.begin();
P.setIntensity(13);
P.setTextAlignment(PA_CENTER);
P.displayClear();
P.displayShutdown(true);
attachInterrupt(digitalPinToInterrupt(PEN_PIN), animation_ISR, CHANGE);
}
void loop() {
deep_sleep();
if(int_flag) {
bool pin_status = digitalRead(PEN_PIN);
delay(30);
pin_status = digitalRead(PEN_PIN);
// Pen is removed
if(pin_status == HIGH){
animate();
int_flag = false;
}
// Pen is inserted
else if(pin_status == LOW){
int_flag = false;
}
}
attachInterrupt(digitalPinToInterrupt(PEN_PIN), animation_ISR, CHANGE);
delay(50);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment