Stap 3: Fake TV-Attiny code
De code voor de Attiny85 is als volgt:
Opmerking: zoals instructables is bekend om te verknoeien code met de "kleiner dan teken" (en blijkbaar deed hier zo goed) ik plaatst u de programmacode in codebender:
/* Four PWM Outputs */ //http://www.technoblogy.com/show?LE0 // ATtiny85 outputs // code adapted for Fake TV
const int Red = 0; const int Green = 1; const int Blue = 2; const int BluePin=4; const int White = 3; int randpwm=0; volatile uint8_t* Port[] = { &OCR0A, &OCR0B, &OCR1A, &OCR1B};
void setup() { pinMode(Red, OUTPUT); pinMode(Green, OUTPUT); pinMode(BluePin, OUTPUT); pinMode(White, OUTPUT); //digitalWrite(3,HIGH); // Configure counter/timer0 for fast PWM on PB0 and PB1
TCCR0A = 3<<COM0A0 | 3<<COM0B0 | 3<<WGM00; TCCR0B = 0<<WGM02 | 3<<CS00; // Optional; already set // Configure counter/timer1 for fast PWM on PB4 TCCR1 = 1<<CTC1 | 1<<PWM1A | 3<<COM1A0 | 7<<CS10; GTCCR = 1<<PWM1B | 3<<COM1B0; // Interrupts on OC1A match and overflow TIMSK = TIMSK | 1<<OCIE1A | 1<<TOIE1; }
ISR(TIMER1_OVF_vect) { bitClear(PORTB, White); }
// Sets colour Red=0 Green=1 Blue=2 White=3 // to specified intensity 0 (off) to 255 (max) void SetColour (int colour, int intensity) { *Port[colour] = 255 - intensity; }
void loop() { for(int i=0;i<10;i++) //play scene 1 multiple times { scene1(); } if (random(2) == 1) //Possibly call scene 2 { scene2(); } if (random(2) == 1) //Possibly call scene 3 { scene3(); } if (random(2) == 1) //Possibly call scene 4 { scene4(); } if (random(4) == 1) //Possibly do a commercial break { commercial(); } }
// These are the main scene algorithms
void scene1() // Changes random light levels and linger-times // of all colors to simulate "normal" TV action { randpwm = random(20,255); SetColour(Red,randpwm); randpwm = random(20,255); SetColour(Green,randpwm); randpwm=random(10,225); SetColour(Blue, randpwm); randpwm=random(10,175); SetColour(White,randpwm); delay(random(500,2000)); }
void scene2() // Increases intensity of wht,blu (fade-in) { delay(1000); for(int i=2;i<255;i++) { // analogWrite(blu,i); // analogWrite(wht,i); SetColour(Blue,i); SetColour(White,i); delay(20); } }
void scene3() // Flickers wht,blu for a flickeriing scene effect { // Serial.println("Scene 3"); boolean sw = HIGH; for(int i=0;i<30;i++) { // digitalWrite(3,sw); digitalWrite(BluePin,sw); digitalWrite(White,sw); sw = !sw; delay(random(50,300)); } }
void scene4() // Changes red/grn light levels only // wht/blu are off { //don't use wht/blu digitalWrite(White,LOW); digitalWrite(BluePin,LOW); for(int i=0;i<12;i++) { randpwm = random(20,255); SetColour(Red,randpwm); randpwm = random(20,255); SetColour(Green,randpwm); delay(random(200,2000)); } }
void commercial() // Simulates a switch to or from a commercial break { SetColour(Red,2); SetColour(Green,2); SetColour(Blue,0); SetColour(White,0); delay(random(1000,2500)); }