Skip to content

Instantly share code, notes, and snippets.

Created March 8, 2014 05:35
Show Gist options
  • Save anonymous/9425825 to your computer and use it in GitHub Desktop.
Save anonymous/9425825 to your computer and use it in GitHub Desktop.
#include "quotefile.h"
//3 is interrupt for Timing
//4 through 11 are Time dependent receivers
//3-11 Pins 12-19
//Curent Serial Byte(Letter or Number)
char incomingByte = 0;
//Previous Byte in the Serial Chain
char previousByte = 0;
int m=0;
char incomingChar = 0;
bool enableout = false;
int randon = 0;
//String for Storing Data
String inputString="";
int istringsize;
byte icounter;
//lets us do multiple cycles without interruption
bool busy = false;
bool processing = false;
//Iterator for Interrupt Values
volatile unsigned int maincount = 0;
volatile unsigned int n=0;
//Time of High Period or Timer Duration
volatile unsigned int timerhigh;
//Output Pin for Timer Waveform
volatile unsigned int waveout;
//Time for Timer Delay in milliseconds, default tim
volatile unsigned int groundtime = 0;
//Receiver Pins in an Array, except for 3, which serves as 0 source reference
int pins[9] =
{
3,4,5,6,7,8,9,10,11
};
//Prototype
void offset_output(char incomingByte);
void rising()
{
// set up Timer 1
TCCR1A = 0; // normal mode
TCCR1B = bit(WGM12) | bit(CS11); // CTC, scale to clock / 8
OCR1A = timerhigh; // time before timer fires
TIMSK1 = bit (OCIE1A); // interrupt on Compare A Match
}
//Sets Pin Low, then sets up another timer interrupt for the jump back to high
ISR(TIMER1_COMPA_vect)
{
//Brings Pin Low for Keypress
//Serial.println("We're in Timer 1");
pinMode(waveout,OUTPUT);
digitalWrite(waveout, LOW);
//Stop Timer 1
TCCR1B = 0; // stop timer
TIMSK1 = 0; // cancel timer interrupt
//Disables Interrupts Before Messing
// set up Timer 2
TCCR2A = 0; // normal mode
TCCR2B = 0;
// TCCR2A = bit(WGM21) | bit(CS21); // CTC, scale to clock / 8
TCCR2A = (1 << WGM21); //Enables CTC for timer
TCCR2B |= (1 << CS21);
TCCR2B |= (1 << CS22); //Sets 256 bit prescaler
TCCR2B |= (0 << CS20);
OCR2A = groundtime; // time before rising edge
TIMSK2 = (1 << OCIE2A);
//Cancel Rising Interrupt on D3
EIFR = bit (INTF1);
//Reinables interrupts
}
//Timer Interrupt for Low Period
ISR(TIMER2_COMPA_vect)
{
//Serial.println("WE'RE IN TIMER2");
//Brings Output pin high
digitalWrite(waveout, HIGH);
//Stop Timer 2
TCCR2A = 0;
TCCR2B = 0; // stop timer
TIMSK2 = 0; // cancel timer interrupt
//Cancel Rising Interrupt on D3
EIFR = bit (INTF1);
detachInterrupt(1);
n++;
busy = true;
if(n >= 5)
{
detachInterrupt(1);
busy = false;
n = 0;
}
}
void setup() {
// put your setup code here, to run once:
//Sets all the pins for output use
for(int i=0;i<8;i++)
{
pinMode(pins[i], INPUT);
}
//Sets Reference Pin as Input
pinMode(3,INPUT);
//Begins Serial
Serial.begin(9600);
//Cancels Timer 1
TCCR1A = 0; // normal mode
TCCR1B = 0; // stop timer
TIMSK1 = 0; // cancel timer interrupt
//Cancels Timer 2
TCCR2A = 0;
TCCR2B = 0; // stop timer
TIMSK2 = 0; // cancel timer interrupt
randomSeed(analogRead(0));
}
void loop() {
//Time of Ground Drop(2 mS), Universal for all letters and offsets
groundtime = 125; // spark time (125 * 16uS) = 2 mS
if (busy == true)
{
attachInterrupt(1,rising,RISING);
}
//We've received a prompt to send a quote
if( Serial.available() && !processing && !busy){
Serial.read();
//create the string
int rn = random(33);
inputString = quotes[rn];
Serial.println(inputString);
istringsize = inputString.length();
icounter = 0;
Serial.println(istringsize);
// enableout == false;
processing = true;
}
if(processing && !busy)
{
if(icounter >= istringsize)
{
processing = false;
//enableout=false;
//randon = 0;
//
}
if(m>20000 && !busy)
{
incomingChar = inputString[icounter++];
Serial.print("Sending: ");
// Serial.print(icounter);
Serial.println(incomingChar);
offset_output(incomingChar);
m=0;
}
m++;
}
/*
if(Serial.available() && busy == false){
while(Serial.available() && busy == false)
{
//Reads serial data byte
incomingByte = (char)Serial.read();
//adds serial data byte to string
inputString += incomingByte;
}
inputString += '\0';
sizeinput = inputString.length();
inputcounter = 0;
}
if((inputcounter < sizeinput) && busy == false)
{
//Reads Serial Value and assigns to incomingByte
incomingChar = inputString[inputcounter++];
//calls typewriter printing function one letter at a time
offset_output(incomingChar)
//Attaches Interrupt to Send Letter!
// noInterrupts (); // atomic change of the time amount
//In this area, we'll set the output pin to be used by the timing interrupt,
//and the time delay off the default waveform present on pin 4
//We can use switch case arrangement, don't know if that's the optimal setup though
}*/
}
void offset_output(char incomingByte){
switch(incomingByte)
{
default:
//Do Nothing
break;
//Cases FOR ALL THE LETTERS
case 'a':
timerhigh=16000;
waveout=7;
break;
case 'b':
timerhigh=20000;
waveout=7;
break;
case 'c':
timerhigh=8000;
waveout=7;
break;
case 'd':
timerhigh=4000;
waveout=7;
break;
case 'e':
timerhigh=4000;
waveout=8;
break;
case 'f':
timerhigh=8000;
waveout=6;
break;
case 'g':
timerhigh=12000;
waveout=7;
break;
case 'h':
timerhigh=12000;
waveout=8;
break;
case 'i':
timerhigh=20000;
waveout=9;
break;
case 'j':
timerhigh=20000;
waveout=10;
break;
case 'k':
timerhigh=0;
waveout=8;
break;
case 'l':
timerhigh=0;
waveout=10;
break;
case 'm':
timerhigh=24000;
waveout=7;
break;
case 'n':
timerhigh=20000;
waveout=6;
break;
case 'o':
timerhigh=24000;
waveout=8;
break;
case 'p':
timerhigh=24000;
waveout=9;
break;
case 'q':
timerhigh=16000;
waveout=9;
break;
case 'r':
//Output pin 17 Frequency Pin 10
//microseconds
timerhigh=8000;
waveout=9;
break;
case 's':
timerhigh=16000;
waveout=6;
break;
case 't':
timerhigh=8000;
waveout=8;
break;
case 'u':
timerhigh=20000;
waveout=8;
break;
case 'v':
timerhigh=12000;
waveout=6;
break;
case 'w':
timerhigh=4000;
waveout=9;
break;
case 'x':
timerhigh=4000;
waveout=6;
break;
case 'y':
timerhigh=12000;
waveout=9;
break;
case 'z':
timerhigh=16000;
waveout=8;
break;
//Cases FOR ALL THE NUMBERS
case '0':
timerhigh=24000;
waveout=10;
break;
case '1':
timerhigh=16000;
waveout=11;
break;
case '2':
timerhigh=16000;
waveout=10;
break;
case '3':
timerhigh=4000;
waveout=10;
break;
case '4':
timerhigh=4000;
waveout=11;
break;
case '5':
timerhigh=8000;
waveout=11;
break;
case '6':
timerhigh=8000;
waveout=10;
break;
case '7':
timerhigh=12000;
waveout=10;
break;
case '8':
timerhigh=12000;
waveout=11;
break;
case '9':
timerhigh=20000;
waveout=11;
break;
//Cases FOR ALL THE PUNCUATION
case ' ':
timerhigh=32000;
waveout=5;
break;
case '.':
timerhigh=0;
waveout=7;
break;
case '\'':
timerhigh=0;
waveout=5;
break;
case ',':
timerhigh=24000;
waveout=6;
break;
case '!':
break;
case '"':
timerhigh = 32000;
waveout=11;
break;
case '#':
timerhigh=32000;
waveout=9;
break;
case '$':
break;
case '%':
break;
case '+/-':
timerhigh=0;
waveout=9;
break;
case '/':
timerhigh=0;
waveout=6;
break;
case ';':
timerhigh=28000;
waveout=10;
break;
case '=':
timerhigh=0;
waveout=11;
break;
/*case '-':
timerhigh=24000;
waveout=11;
break;*/
case '\r':
timerhigh = 32000;
waveout=9;
break;
}
//Starts the cycle after offet is established
attachInterrupt(1,rising,RISING);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment