Arduino Code 2
Arduino Code 2
/*
piCropCircle10Sep2012_analogIN_4chBrain_intwithclock
for prototype 2 with Arduino Nano
masahiro kahata
*/
/*
String length()
Examples of how to use length() in a String.
Open the Serial Monitor and start sending characters to see the results.
created 1 Aug 2010
by Tom Igoe
http://arduino.cc/en/Tutorial/StringLengthTrim
This example code is in the public domain.
*/
/*
http://letsmakerobots.com/node/28278
Timer Overflow:
Timer overflow means the timer has reached is limit value. When a timer overflow interrupt occurs, the timer overflow bit TOVx will be set in the interrupt flag register TIFRx. When the timer overflow interrupt enable bit TOIEx in the interrupt mask register TIMSKx is set, the timer overflow interrupt service routine ISR(TIMERx_OVF_vect) will be called.
Output Compare Match:
When a output compare match interrupt occurs, the OCFxy flag will be set in the interrupt flag register TIFRx . When the output compare interrupt enable bit OCIExy in the interrupt mask register TIMSKx is set, the output compare match interrupt service ISR(TIMERx_COMPy_vect) routine will be called.
Timer Input Capture:
When a timer input capture interrupt occurs, the input capture flag bit ICFx will be set in the interrupt flag register TIFRx. When the input capture interrupt enable bit ICIEx in the interrupt mask register TIMSKx is set, the timer input capture interrupt service routine ISR(TIMERx_CAPT_vect) will be called.
PWM and timer
There is fixed relation between the timers and the PWM capable outputs. When you look in the data sheet or the pinout of the processor these PWM capable pins have names like OCRxA, OCRxB or OCRxC (where x means the timer number 0..5). The PWM functionality is often shared with other pin functionality.
The Arduino has 3Timers and 6 PWM output pins. The relation between timers and PWM outputs is:
Pins 5 and 6: controlled by timer0
Pins 9 and 10: controlled by timer1
Pins 11 and 3: controlled by timer2
*/
//timer interrupts
//by Amanda Ghassaei
//June 2012
//http://www.instructables.com/id/Arduino-Timer-Interrupts/
/*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
*/
//
// http://arduino.cc/playground/Code/Timer1
//
//timer setup for timer0, timer1.
//For arduino uno or any board with ATMEL 328/168.. diecimila, duemilanove, lilypad, nano, mini...
//this code will enable all three arduino timer interrupts.
//timer0 will interrupt at 2kHz
//timer1 will interrupt at 1Hz
//storage variables
boolean toggle0 = 0;
boolean toggle1 = 0;
boolean toggle2 = 0;
//
int timeX =0;
//
//
String txtMsg = ""; // a string for incoming text
int lastStringLength = txtMsg.length(); // previous length of the String
const int ledPin1 = 2; // the number of the LED pin
const int ledPin2 = 4; // the number of the LED pin
const int ledPin3 = 7; // the number of the LED pin
const int ledPin4 = 8; // the number of the LED pin : reverse
const int ledPin5 = 12; // the number of the LED pin
const int ledPin6 = 13; // the number of the LED pin
const int ledPinPCM7 = 3; // the number of the LED pin
const int ledPinPCM8 = 5; // the number of the LED pin
const int ledPinPCM9 = 6; // the number of the LED pin
const int ledPinPCM10 = 9; // the number of the LED pin : reverse
const int ledPinPCM11 = 10; // the number of the LED pin : reverse
const int ledPinPCM12 = 11; // the number of the LED pin : reverse
const int analogInPin = A0; // Analog input pin that the potentiometer is attached to
const int analogOutPin = 3; // Analog output pin that the LED is attached to
const int analogInPin2 = A1; // Analog input pin that the potentiometer is attached to
const int analogOutPin2 = 5; // Analog output pin that the LED is attached to
const int analogInPin3 = A2; // Analog input pin that the potentiometer is attached to
const int analogOutPin3 = 6; // Analog output pin that the LED is attached to
const int analogInPin4 = A3; // Analog input pin that the potentiometer is attached to
const int analogOutPin4 = 9; // Analog output pin that the LED is attached to
const int analogInPin5 = A4; // Analog input pin that the potentiometer is attached to
const int analogOutPin5 = 10; // Analog output pin that the LED is attached to
const int analogInPin6 = A5; // Analog input pin that the potentiometer is attached to
const int analogOutPin6 = 11; // Analog output pin that the LED is attached to
int sensorValue = 0; // value read from the pot
int outputValue = 0; // value output to the PWM (analog out)
int sensorValue2 = 0; // value read from the pot
int outputValue2 = 0; // value output to the PWM (analog out)
int sensorValue3 = 0; // value read from the pot
int outputValue3 = 0; // value output to the PWM (analog out)
int sensorValue4 = 0; // value read from the pot
int outputValue4 = 0; // value output to the PWM (analog out)
int sensorValue5 = 0; // value read from the pot
int outputValue5 = 0; // value output to the PWM (analog out)
int sensorValue6 = 0; // value read from the pot
int outputValue6 = 0; // value output to the PWM (analog out)
String channel1 = "abc";
String channel2 = "abc";
String channel3 = "abc";
String channel4 = "abc";
String channel5 = "abc";
String channel6 = "abc";
String zeroChar = "0";
int afterReciveSerial = 0;
int timeLoop=200;
void setup() {
// open the serial port:
// Serial.begin(38400);
Serial.begin(115200);
// set the digital pin as output:
pinMode(ledPin1, OUTPUT);
pinMode(ledPin2, OUTPUT);
pinMode(ledPin3, OUTPUT);
pinMode(ledPin4, OUTPUT);
pinMode(ledPin5, OUTPUT);
pinMode(ledPin6, OUTPUT);
pinMode(ledPinPCM7, OUTPUT);
pinMode(ledPinPCM8, OUTPUT);
pinMode(ledPinPCM9, OUTPUT);
pinMode(ledPinPCM10, OUTPUT);
pinMode(ledPinPCM11, OUTPUT);
pinMode(ledPinPCM12, OUTPUT);
cli();//stop interrupts
//set timer0 interrupt at 1024Hz : OCR0A = 243.14;// = (16*10^6) / (1024*64) - 1 (must be <256)
//set timer0 interrupt at 1200Hz : OCR0A = 208.33;// = (16*10^6) / (1200*64) - 1 (must be <256)
//set timer0 interrupt at 2kHz : OCR0A = 124;// = (16*10^6) / (2000*64) - 1 (must be <256)
TCCR0A = 0;// set entire TCCR2A register to 0
TCCR0B = 0;// same for TCCR2B
TCNT0 = 0;//initialize counter value to 0
// set compare match register for 2khz increments
OCR0A = 243;// = (16*10^6) / (2000*64) - 1 (must be <256)
// turn on CTC mode
TCCR0A |= (1 << WGM01);
// Set CS11 and CS10 bits for 64 prescaler
TCCR0B |= (1 << CS11) | (1 << CS10);
// enable timer compare interrupt
TIMSK0 |= (1 << OCIE0A);
//set timer1 interrupt at 1Hz : OCR1A = 15624;// = (16*10^6) / (1*1024) - 1 (must be <65536)
//set timer1 interrupt at 128Hz : OCR1A = 121.0703;// = (16*10^6) / (128*1024) - 1 (must be <65536)
//set timer1 interrupt at 256Hz : OCR1A = 60.35;// = (16*10^6) / (256*1024) - 1 (must be <65536)
//set timer1 interrupt at 256Hz : OCR1A = 975.56;// = (16*10^6) / (256*64) - 1 (must be <65536)
//set timer1 interrupt at 128Hz : OCR1A = 15624;// = (16*10^6) / (128*8) - 1 (must be <65536)
//set timer1 interrupt at 256Hz : OCR1A = 7811.5;// = (16*10^6) / (256*8) - 1 (must be <65536)
//set timer1 interrupt at 512Hz : OCR1A = 3905.25;// = (16*10^6) / (512*8) - 1 (must be <65536)
TCCR1A = 0;// set entire TCCR1A register to 0
TCCR1B = 0;// same for TCCR1B
TCNT1 = 0;//initialize counter value to 0
// set compare match register for 1hz increments
OCR1A = 7811.5;// = (16*10^6) / (256*8) - 1 (must be <65536)
// turn on CTC mode
TCCR1B |= (1 << WGM12);
// Set CS12 and CS10 bits for 1024 prescaler
// TCCR1B |= (1 << CS12) | (1 << CS10);
// Set CS11 and CS10 bits for 64 prescaler
// TCCR1B |= (1 << CS11) | (1 << CS10);
// Set CS11 bit for 8 prescaler
TCCR1B |= (1 << CS11);
// enable timer compare interrupt
TIMSK1 |= (1 << OCIE1A);
sei();//allow interrupts
AllOFF();
}
ISR(TIMER0_COMPA_vect){// set timer0 interrupt at 1024Hz
if ( afterReciveSerial == 0 ) {
runLED();
}
}
ISR(TIMER1_COMPA_vect) { // timer1 interrupt at 256Hz
send4Data();
}
void loop() {
if (Serial.available() > 0) {
int inChar = Serial.read();
Serial.println(inChar);
// print the message and a notice if it's changed:
afterReciveSerial = 1;
if ( inChar =='A' ) // 1
digitalWrite(ledPinPCM9, HIGH);
if ( inChar =='a' )
digitalWrite(ledPinPCM9, LOW);
if ( inChar =='B' ) // 2
digitalWrite(ledPin4, LOW);
if ( inChar =='b' )
digitalWrite(ledPin4, HIGH);
if ( inChar =='C' ) // 3
digitalWrite(ledPinPCM10, LOW);
if ( inChar =='c' )
digitalWrite(ledPinPCM10, HIGH);
if ( inChar =='D' ) // 4
digitalWrite(ledPin3, LOW);
if ( inChar =='d' )
digitalWrite(ledPin3, HIGH);
if ( inChar =='E' ) // 5
digitalWrite(ledPinPCM8, HIGH);
if ( inChar =='e' )
digitalWrite(ledPinPCM8, LOW);
if ( inChar =='F' ) // 6
digitalWrite(ledPin2, HIGH);
if ( inChar =='f' )
digitalWrite(ledPin2, LOW);
if ( inChar =='G' ) // 7
digitalWrite(ledPinPCM11, LOW);
if ( inChar =='g' )
digitalWrite(ledPinPCM11, HIGH);
if ( inChar =='H' ) // 8
digitalWrite(ledPinPCM12, HIGH);
if ( inChar =='h' )
digitalWrite(ledPinPCM12, LOW);
if ( inChar =='I' ) // 9
digitalWrite(ledPinPCM7, HIGH);
if ( inChar =='i' )
digitalWrite(ledPinPCM7, LOW);
if ( inChar =='J' ) // 10
digitalWrite(ledPin5, HIGH);
if ( inChar =='j' )
digitalWrite(ledPin5, LOW);
}
// read the analog in value:
sensorValue = analogRead(analogInPin);
// map it to the range of the analog out:
outputValue = map(sensorValue, 60, 690, 30, 255);
sensorValue2 = analogRead(analogInPin2);
// map it to the range of the analog out:
outputValue2 = map(sensorValue2, 60, 690, 30, 255);
// print the results to the serial monitor:
// Serial.print("A = " );
// Serial.print(outputValue);
// Serial.print("\t B = ");
// Serial.println(outputValue2);
// wait 10 milliseconds before the next loop
// for the analog-to-digital converter to settle
// after the last reading:
delay(10);
}
void AllOFF() {
digitalWrite(ledPinPCM9, LOW);
digitalWrite(ledPin4, HIGH);
digitalWrite(ledPinPCM10, HIGH);
digitalWrite(ledPin3, HIGH);
digitalWrite(ledPinPCM8, LOW);
digitalWrite(ledPinPCM7, LOW);
digitalWrite(ledPinPCM11, HIGH);
digitalWrite(ledPinPCM12, LOW);
digitalWrite(ledPin2, LOW);
digitalWrite(ledPin5, LOW);
}
void runLED() {
if ( outputValue2 >= 50 ) {
AllOFF();
// digitalWrite(ledPin4, LOW);
// digitalWrite(ledPin2, HIGH);
}
else if ( outputValue2 < 50 ) {
if ( (timeX>=0) && (timeX<timeLoop) ) // 1
digitalWrite(ledPinPCM9, HIGH);
else if ( (timeX>=timeLoop) && (timeX<timeLoop*2) )
digitalWrite(ledPinPCM9, LOW);
if ( (timeX>=timeLoop) && (timeX<timeLoop*2) ) // 2
digitalWrite(ledPin4, LOW);
else if ( (timeX>=timeLoop*2) && (timeX<timeLoop*3) )
digitalWrite(ledPin4, HIGH);
if ( (timeX>=timeLoop*2) && (timeX<timeLoop*3) ) // 3
digitalWrite(ledPinPCM10, LOW);
else if ( (timeX>=timeLoop*3) && (timeX<timeLoop*4) )
digitalWrite(ledPinPCM10, HIGH);
if ( (timeX>=timeLoop*3) && (timeX<timeLoop*4) ) // 4
digitalWrite(ledPin3, LOW);
else if ( (timeX>=timeLoop*3) && (timeX<timeLoop*5) )
digitalWrite(ledPin3, HIGH);
if ( (timeX>=timeLoop*4) && (timeX<timeLoop*5) ) // 5
digitalWrite(ledPinPCM8, HIGH);
else if ( (timeX>=timeLoop*5) && (timeX<timeLoop*6) )
digitalWrite(ledPinPCM8, LOW);
if ( (timeX>=timeLoop*5) && (timeX<timeLoop*6) ) // 6
digitalWrite(ledPin2, HIGH);
else if ( (timeX>=timeLoop*6) && (timeX<timeLoop*7) )
digitalWrite(ledPin2, LOW);
if ( (timeX>=timeLoop*6) && (timeX<timeLoop*7) ) // 7
digitalWrite(ledPinPCM11, LOW);
else if ( (timeX>=timeLoop*7) && (timeX<timeLoop*8) )
digitalWrite(ledPinPCM11, HIGH);
if ( (timeX>=timeLoop*7) && (timeX<timeLoop*8) ) // 8
digitalWrite(ledPinPCM12, HIGH);
else if ( (timeX>=timeLoop*9) && (timeX<timeLoop*10) )
digitalWrite(ledPinPCM12, LOW);
if ( (timeX>=timeLoop*9) && (timeX<timeLoop*10) ) // 9
digitalWrite(ledPinPCM7, HIGH);
else if ( (timeX>=timeLoop*10) && (timeX<timeLoop*11) )
digitalWrite(ledPinPCM7, LOW);
if ( (timeX>=timeLoop*10) && (timeX<timeLoop*11) ) // 10
digitalWrite(ledPin5, HIGH);
else if ( (timeX>=0) && (timeX<timeLoop*2) )
digitalWrite(ledPin5, LOW);
}
timeX = timeX+1;
if ( timeX > timeLoop*11 ) {
timeX=0;
if ( outputValue> 1 ) {
timeLoop=outputValue;
}
}
}
void send4Data() {
txtMsg = "";
// read the analog in value:
sensorValue = analogRead(analogInPin3);
// map it to the range of the analog out:
// outputValue = map(sensorValue, 0, 1023, 0, 255);
outputValue3 = sensorValue;
sensorValue2 = analogRead(analogInPin4);
// map it to the range of the analog out:
// outputValue2 = map(sensorValue2, 60, 690, 0, 255);
outputValue4 = sensorValue2;
// change the analog out value:
// if (outputValue2<0) outputValue2=0;
// analogWrite(ledPinPCM11, outputValue2);
// sensorValue3 = 0;
sensorValue3 = analogRead(analogInPin5);
// map it to the range of the analog out:
// outputValue3 = map(sensorValue3, 60, 690, 0, 255);
outputValue5 = sensorValue3;
// change the analog out value:
// if (outputValue3<0) outputValue3=0;
// analogWrite(ledPinPCM12, outputValue3);
// sensorValue4 = 0;
sensorValue4 = analogRead(analogInPin6);
// map it to the range of the analog out:
// outputValue4 = map(sensorValue4, 60, 690, 0, 255);
outputValue6 = sensorValue4;
// change the analog out value:
// if (outputValue4<0) outputValue4=0;
// analogWrite(ledPinPCM10, outputValue4);
// print the results to the serial monitor:
channel1 = String( outputValue3, HEX );
channel2 = String( outputValue4, HEX );
channel3 = String( outputValue5, HEX );
channel4 = String( outputValue6, HEX );
if (channel1.length() == 1) {
Serial.print(zeroChar);
Serial.print(zeroChar);
}
if (channel1.length() == 2)
Serial.print(zeroChar);
Serial.print(channel1);
Serial.print("\t");
if (channel2.length() ==1) {
Serial.print(zeroChar);
Serial.print(zeroChar);
}
if (channel2.length() ==2)
Serial.print(zeroChar);
Serial.print(channel2);
Serial.print("\t");
if (channel3.length() ==1) {
Serial.print(zeroChar);
Serial.print(zeroChar);
}
if (channel3.length() ==2)
Serial.print(zeroChar);
Serial.print(channel3);
Serial.print("\t");
if (channel4.length() ==1) {
Serial.print(zeroChar);
Serial.print(zeroChar);
}
if (channel4.length() ==2)
Serial.print(zeroChar);
Serial.print(channel4);
// Serial.println(channel4);
Serial.print("\r");
}
Arduino Code 2