Hello all. the code below is for RGB LED fading ect.. It works and up loads to attiny ok. the pinchange interrupt works as well BUT
I need it to be interrupted via SOFTWARE, but I can't do it. As this was written for a IR remote control, well pushing a button just won't do. And yes the IR remote works, As you can see It never comes out of the while loop as I don't know how to tell it that.
The code is from many sources and will list them when it works. So how do I set up for a SOFTWARE interrupt.??? Thanks, Ron.
/*
Sony remote from many sources ATTINY 85.
3/13/2014
Ron Buchwald
I will credit them when script is finished.
*/
#include <avr/io.h>
#include <avr/interrupt.h>
#include <PinChangeInterrupt.h> // mimics attachInterrupt() but a PCI for ATtiny
//#include "TinyWireM.h" < Do I need this?
int irPin = 4;//85=Pin 3 // IRSensor pin 1 wired to Atiny pin PB4
int start_bit = 2200; //Start bit threshold (Microseconds)
int bin_1 =1000; //Binary 1 threshold (Microseconds)
int bin_0 = 400; //Binary 0 threshold (Microseconds)
const int redPin = 0; //85=0
const int grnPin = 1; //85=1
const int bluPin = 2; //85=2
const int RTS = 3;
volatile boolean trigger=CHANGE; //This is our interrupt connected to the button switch
volatile int fd = 0;
void setup() {
attachPcInterrupt(3,interrupttrigger,FALLING);
//attachInterrupt(3, interrupttrigger, CHANGE);
pinMode(irPin, INPUT);
pinMode(redPin, OUTPUT);
pinMode(grnPin, OUTPUT);
pinMode(bluPin, OUTPUT);
pinMode(RTS, INPUT);
//digitalWrite(RTS,HIGH);
}
void loop() {
int key = getIRKey(); //Fetch the key
if(key != 0) //Ignore keys that are zero
{
switch(key)
{
case 128:
RED() ;
break;
case 129:
BLUE();
break;
case 130:
GRN() ;
break;
case 132:
OFF() ;
break;
case 133:
RST() ;
break;
case 131:
FADE();
break;
}
}
}
void FADE()
{
fd = (0);
if (fd ==(0))
do
{
redtoyellow();
yellowtogreen();
greentocyan();
cyantoblue();
bluetomagenta();
magentatored();
}
while (fd ==(0)); // DO THE ABOVE A LONG AS fd = 0
else
OFF();
}
void RST()
{
digitalWrite(RTS, LOW);
delay(100);
digitalWrite(RTS, HIGH);
delay(100);
digitalWrite(RTS, LOW);
}
void OFF()
{
digitalWrite(bluPin, HIGH);
digitalWrite(redPin, HIGH);
digitalWrite(grnPin, HIGH);
}
void RED()
{
digitalWrite(bluPin, HIGH);
digitalWrite(redPin, LOW);
digitalWrite(grnPin, HIGH);
}
void BLUE()
{
digitalWrite(bluPin, LOW);
digitalWrite(redPin, HIGH);
digitalWrite(grnPin, HIGH);
}
void GRN()
{
digitalWrite(bluPin, HIGH);
digitalWrite(redPin, HIGH);
digitalWrite(grnPin, LOW);
}
void redtoyellow()
{
digitalWrite(redPin, HIGH);
digitalWrite(bluPin, LOW);
// fade up green
for(byte i=1; i<50; i++) {
byte on = i;
byte off = 50-on;
for( byte a=0; a<50; a++ ) {
digitalWrite(grnPin, HIGH);
delayMicroseconds(on);
digitalWrite(grnPin, LOW);
delayMicroseconds(off);
}
}
}
void yellowtogreen()
{
digitalWrite(grnPin, HIGH);
digitalWrite(bluPin, LOW);
// fade down red
for(byte i=1; i<50; i++) {
byte on = 50-i;
byte off = i;
for( byte a=0; a<50; a++ ) {
digitalWrite(redPin, HIGH);
delayMicroseconds(on);
digitalWrite(redPin, LOW);
delayMicroseconds(off);
}
}
}
void greentocyan()
{
digitalWrite(grnPin, HIGH);
digitalWrite(redPin, LOW);
// fade up blue
for(byte i=1; i<50; i++) {
byte on = i;
byte off = 50-on;
for( byte a=0; a<50; a++ ) {
digitalWrite(bluPin, HIGH);
delayMicroseconds(on);
digitalWrite(bluPin, LOW);
delayMicroseconds(off);
}
}
}
void cyantoblue()
{
digitalWrite(bluPin, HIGH);
digitalWrite(redPin, LOW);
// fade down green
for(byte i=1; i<50; i++) {
byte on = 50-i;
byte off = i;
for( byte a=0; a<50; a++ ) {
digitalWrite(grnPin, HIGH);
delayMicroseconds(on);
digitalWrite(grnPin, LOW);
delayMicroseconds(off);
}
}
}
void bluetomagenta()
{
digitalWrite(bluPin, HIGH);
digitalWrite(grnPin, LOW);
// fade up red
for(byte i=1; i<50; i++) {
byte on = i;
byte off = 50-on;
for( byte a=0; a<50; a++ ) {
digitalWrite(redPin, HIGH);
delayMicroseconds(on);
digitalWrite(redPin, LOW);
delayMicroseconds(off);
}
}
}
void magentatored()
{
digitalWrite(redPin, HIGH);
digitalWrite(grnPin, LOW);
// fade down blue
for(byte i=1; i<50; i++) {
byte on = 50-i;
byte off = i;
for( byte a=0; a<50 ; a++ ) {
digitalWrite(bluPin, HIGH);
delayMicroseconds(on);
digitalWrite(bluPin, LOW);
delayMicroseconds(off);
}
}
}
void interrupttrigger(){
{
fd =(1); // THIS IS TO SET FD TO 1 SO TO EXIT THE WHILE LOOP.
OFF();
digitalWrite(bluPin, HIGH);
digitalWrite(redPin, HIGH);
digitalWrite(grnPin, HIGH);
/* int key = getIRKey();
if (trigger ==HIGH){ //Checks to see what the last value was (high or low)
trigger=LOW;} //If its low it is now set to high
else{
trigger=HIGH;} //If its high it is now set to low
*/ }
}
int getIRKey() {
int data[12];
int i;
while(pulseIn(irPin, LOW) < start_bit); //Wait for a start bit
for(i = 0 ; i < 11 ; i++)
data[i] = pulseIn(irPin, LOW); //Start measuring bits, I only want low pulses
for(i = 0 ; i < 11 ; i++) //Parse them
{
if(data[i] > bin_1) //is it a 1?
data[i] = 1;
else if(data[i] > bin_0) //is it a 0?
data[i] = 0;
else
return -1; //Flag the data as invalid; I don't know what it is! Return -1 on invalid data
}
int result = 0;
for(i = 0 ; i < 11 ; i++) //Convert data bits to integer
if(data[i] == 1) result |= (1<<i);
return result; //Return key number
}