Brake Lights Tester - Software | |
Programming the ATtiny85The ATtiny85 is programmed using the Arduino environment with either an Arduino Uno or a dedicated programmer.It's necessary to define the ATtiny85 core so the Arduino IDE knows about its pins. Be aware there are a couple of different libraries for the ATtiny85 that treat the ATting85's internal registers differently. The library I used in the Arduino brakelight Sketch is arduino-tiny. The 'README' included in the downloaded zip package contains full instructions for installing it into the Arduino IDE. There are dozens of websites detailing how to use the Arduino Uno as a programmer - particularly for the ATtiny85 - so I won't repeat them in detail here. In brief, the steps are:
If you use an external stand-alone programmer, such as the one shown here, most of the steps are the same except, as you won't be using the Arduino Uno as the programmer, the 10uF capacitor isn't used and the wiring to the ATtiny85 is already in place. Also, the programmer will already have the ArduinoISP loaded.
If you want to try some of the simpe Arduino examples before you program the ATtiny85 with the brakelights sketch (for example, the Blink example), remember that the pinout on the ATtiny85 is completely different from the ATmega328 used in the Uno.
/* Brake Light Tester (c) 2014 vwlowen.co.uk Uses the Arduino-tiny 'core' https://code.google.com/p/arduino-tiny/ This project uses the ATtiny85 */ #include <avr/sleep.h> const int sensor = A2; const int testButton = 2; //PB2 is INT0 pin const int redLED = 1; const int greenLED = 0; int activeLED = redLED; int ambientLight; int delta = 50; unsigned long timeoutTimer; unsigned long timeoutInterval = 30000; // 30 seconds void setup() { pinMode(testButton, INPUT); digitalWrite(testButton, HIGH); // Enable internal pullup pinMode(redLED, OUTPUT); pinMode(greenLED, OUTPUT); digitalWrite(greenLED, LOW); GIMSK |= 1<<PCIE; // Enable Pin Change Interrupt PCMSK |= 1<<PCINT2; // Watch for Pin Change (Falling) on D2 (PB2) } void sleep() { ADCSRA = 0; // ADC to off set_sleep_mode(SLEEP_MODE_PWR_DOWN); sleep_enable(); // Set the Sleep Enable bit sleep_mode(); sleep_disable(); // Waking up. ADCSRA = 0x80; // ADC to on. } // ATtiny85 returns to setup() ? ISR(PCINT2_vect) { // Interrupt Service Routine } void loop() { activeLED = redLED; timeoutTimer = millis(); digitalWrite(activeLED, HIGH); digitalWrite(greenLED, LOW); for(int i=0; i<5; i++) { delay(200); digitalWrite(activeLED, LOW); delay(500); digitalWrite(activeLED, HIGH); } ambientLight = analogRead(sensor); while(timeoutTimer + timeoutInterval > millis() ) { digitalWrite(activeLED, HIGH); if (analogRead(sensor) > (ambientLight + delta)) { // light seen. greenLED becomes active LED activeLED = greenLED; digitalWrite(redLED, LOW); } } digitalWrite(redLED, LOW); digitalWrite(greenLED, LOW); sleep(); }
|