Last active
November 13, 2015 08:13
-
-
Save raphaelschaad/54e43602602c19bdac50 to your computer and use it in GitHub Desktop.
Arduino code. See: http://fab.cba.mit.edu/classes/863.15/section.CBA/people/Schaad/week8-embedded-programming.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// att44_arduino_schaad1.ino | |
// | |
// Created by Raphael Schaad on 2015-11-03. | |
// This is free and unencumbered software released into the public domain. | |
// | |
// Hardware: | |
// Based on Neil Gershenfeld's board: http://academy.cba.mit.edu/classes/embedded_programming/index.html#echo | |
// With my redesign and enhancements: http://fab.cba.mit.edu/classes/863.15/section.CBA/people/Schaad/week6-electronics-design.html | |
const int LED1 = 8; // (red) on Pin 8 (PWM) physical pin 5 | |
const int LED2 = 2; // (green) on Pin 2 (Analog Input 2) physical pin 11 | |
const int LED3 = 3; // (green) on Pin 3 (Analog Input 3) physical pin 10 | |
const int BTN1 = 7; // on Pin 7 (PWM, Analog Input 7) physical pin 6 | |
// capacitive touch: touch an input pin + GND http://highlowtech.org/?p=1653 | |
//int calibration = 0; | |
//int previous = 0; | |
int buttonState = LOW; | |
void setup() { | |
// capacitive touch | |
// for (int i = 0; i < 8; i++) { | |
// calibration += chargeTime(BTN1); | |
// delay(20); | |
// } | |
// calibration = (calibration + 4) / 8; | |
// Configure digital I/O pins | |
pinMode(LED1, OUTPUT); | |
pinMode(LED2, OUTPUT); | |
pinMode(LED3, OUTPUT); | |
// If this was just INPUT, digitalRead(BTN1) would *always* be LOW. | |
// With INPUT_PULLUP, digitalRead(BTN1) will be HIGH by default, and LOW when the button is pressed. | |
// https://www.arduino.cc/en/Reference/Constants | |
// https://www.arduino.cc/en/Tutorial/DigitalPins | |
pinMode(BTN1, INPUT_PULLUP); | |
} | |
void loop() { | |
// capacitive touch | |
// int n = chargeTime(BTN1); | |
// // (n > calibration) indicates touch | |
// if (previous <= calibration && n > calibration) { | |
// // toggle | |
// if (buttonState == LOW) { | |
// buttonState = HIGH; | |
// } else { | |
// buttonState = LOW; | |
// } | |
// } | |
// previous = n; | |
// delay(100); | |
buttonState = digitalRead(BTN1); | |
if (buttonState == HIGH) { | |
// Clear | |
digitalWrite(LED2, LOW); | |
digitalWrite(LED3, LOW); | |
// Fading: https://www.arduino.cc/en/Tutorial/PWM (only pint at LED1 supports this) | |
// The real max is 255 but the difference in the higher values is visually not as discernible | |
const int brightnessMax = 100; | |
for (int i = 0; i <= brightnessMax; i++) { | |
analogWrite(LED1, i); | |
delay(10); | |
} | |
for (int i = brightnessMax - 1; i >= 0; i--) { | |
analogWrite(LED1, i); | |
delay(10); | |
} | |
} else { | |
// Blinking | |
digitalWrite(LED2, HIGH); | |
digitalWrite(LED3, LOW); | |
delay(200); // in ms | |
digitalWrite(LED2, LOW); | |
digitalWrite(LED3, HIGH); | |
delay(200); | |
} | |
} | |
// capacitive touch | |
byte chargeTime(byte pin) { | |
byte mask = (1 << pin); | |
byte i; | |
DDRA &= ~mask; // input | |
PORTA |= mask; // pull-up on | |
for (i = 0; i < 16; i++) { | |
if (PINA & mask) break; | |
} | |
PORTA &= ~mask; // pull-up off | |
DDRA |= mask; // discharge | |
return i; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment