Project

General

Profile

Wiki » _3DInterface.ino

Ómar Þór Sigfússon, 2016-05-20 13:46

 
//
// By Kyle McDonald
// From the instructables project at:
// http://www.instructables.com/id/DIY-3D-Controller/

#define resolution 8
#define mains 50 // 60: north america, japan; 50: most other places

#define refresh 2 * 1000000 / mains

const long V_GRUNN = 5845;

const long B_GRUNN = 6090;

const long H_GRUNN = 5845;

void setup() {
Serial.begin(9600);

// Utgangar fyrir LED
// settir i LOW
for(int i = 0; i < 14; i++) {
pinMode(i, OUTPUT);
digitalWrite(i, LOW);
}

// Inngangar fra thettum
for(int i = 8; i < 11; i++) {
pinMode(i, INPUT);
digitalWrite(i, HIGH);
}

pinMode(A0, OUTPUT); // Breyta Analog inputs to Digital
digitalWrite(A0, HIGH); // Jord fyrir 1 haed
pinMode(A1, OUTPUT);
digitalWrite(A1, HIGH); // Jord fyrir 2 haed
pinMode(A2, OUTPUT);
digitalWrite(A2, HIGH); // Jord fyrir 3 haed
pinMode(A3, OUTPUT);
digitalWrite(A3, LOW); // utgangur led
pinMode(A4, OUTPUT);
digitalWrite(A4, LOW); // utgangur led
pinMode(A5, OUTPUT);
digitalWrite(A5, LOW); // utgangur led
startTimer();
}

void loop() {
long V, B, H;
long temp = 0;
for(int i = 0; i < 4; i++)
{
V = time(8, B00000001);
temp = temp + V;
}
temp = temp/4;
V = temp;
temp = 0;
Serial.print("V ");
Serial.print(V, DEC);

for(int i = 0; i < 4; i++)
{
B = time(9, B00000010);
temp = temp + B;
}
temp = temp/4;
B = temp;
temp = 0;

Serial.print(" B ");
Serial.print(B, DEC);

for(int i = 0; i < 4; i++)
{
H = time(10, B00000100);
temp = temp + H;
}
temp = temp/4;
H = temp;
temp = 0;
Serial.print(" H ");
Serial.print(H, DEC);
Serial.print('\n');
// delay(500);

// HAED 1
//LED 1.1
if(V > 6080 && B > 6350 && (5850 < H && H < 5900)){
digitalWrite(2, HIGH);
digitalWrite(3, LOW);
digitalWrite(4, LOW);
digitalWrite(5, LOW);
digitalWrite(6, LOW);
digitalWrite(7, LOW);
digitalWrite(A3, LOW);
digitalWrite(A4, LOW);
digitalWrite(A5, LOW);
digitalWrite(A0, LOW);
digitalWrite(A1, HIGH);
digitalWrite(A2, HIGH);
}
//LED 1.2
else if(V > 6080 && B > 6350 && (5900 < H && H < 6000)){
digitalWrite(2, LOW);
digitalWrite(3, HIGH);
digitalWrite(4, LOW);
digitalWrite(5, LOW);
digitalWrite(6, LOW);
digitalWrite(7, LOW);
digitalWrite(A3, LOW);
digitalWrite(A4, LOW);
digitalWrite(A5, LOW);
digitalWrite(A0, LOW);
digitalWrite(A1, HIGH);
digitalWrite(A2, HIGH);
}
//LED 1.3
else if(V > 6080 && B > 6350 && H > 6000){
digitalWrite(2, LOW);
digitalWrite(3, LOW);
digitalWrite(4, HIGH);
digitalWrite(5, LOW);
digitalWrite(6, LOW);
digitalWrite(7, LOW);
digitalWrite(A3, LOW);
digitalWrite(A4, LOW);
digitalWrite(A5, LOW);
digitalWrite(A0, LOW);
digitalWrite(A1, HIGH);
digitalWrite(A2, HIGH);
}

//LED 1.4
else if((V > 5930 && V < 6080) && B > 6350 && (5850 < H && H < 5900)){
digitalWrite(2, LOW);
digitalWrite(3, LOW);
digitalWrite(4, LOW);
digitalWrite(5, HIGH);
digitalWrite(6, LOW);
digitalWrite(7, LOW);
digitalWrite(A3, LOW);
digitalWrite(A4, LOW);
digitalWrite(A5, LOW);
digitalWrite(A0, LOW);
digitalWrite(A1, HIGH);
digitalWrite(A2, HIGH);
}

//LED 1.5
else if((V > 5930 && V < 6080) && B > 6350 && (5900 < H && H < 6000)){
digitalWrite(2, LOW);
digitalWrite(3, LOW);
digitalWrite(4, LOW);
digitalWrite(5, LOW);
digitalWrite(6, HIGH);
digitalWrite(7, LOW);
digitalWrite(A3, LOW);
digitalWrite(A4, LOW);
digitalWrite(A5, LOW);
digitalWrite(A0, LOW);
digitalWrite(A1, HIGH);
digitalWrite(A2, HIGH);
}

//LED 1.6
else if((V > 5930 && V < 6080) && B > 6350 && H > 6000){
digitalWrite(2, LOW);
digitalWrite(3, LOW);
digitalWrite(4, LOW);
digitalWrite(5, LOW);
digitalWrite(6, LOW);
digitalWrite(7, HIGH);
digitalWrite(A3, LOW);
digitalWrite(A4, LOW);
digitalWrite(A5, LOW);
digitalWrite(A0, LOW);
digitalWrite(A1, HIGH);
digitalWrite(A2, HIGH);
}
//LED 1.7
else if((V > 5850 && V < 5930) && B > 6350 && (5850 < H && H < 5900)){
digitalWrite(2, LOW);
digitalWrite(3, LOW);
digitalWrite(4, LOW);
digitalWrite(5, LOW);
digitalWrite(6, LOW);
digitalWrite(7, LOW);
digitalWrite(A3, HIGH);
digitalWrite(A4, LOW);
digitalWrite(A5, LOW);
digitalWrite(A0, LOW);
digitalWrite(A1, HIGH);
digitalWrite(A2, HIGH);
}
//LED 1.8
else if((V > 5850 && V < 5930) && B > 6350 && (5900 < H && H < 6000)){
digitalWrite(2, LOW);
digitalWrite(3, LOW);
digitalWrite(4, LOW);
digitalWrite(5, LOW);
digitalWrite(6, LOW);
digitalWrite(7, LOW);
digitalWrite(A3, LOW);
digitalWrite(A4, HIGH);
digitalWrite(A5, LOW);
digitalWrite(A0, LOW);
digitalWrite(A1, HIGH);
digitalWrite(A2, HIGH);
}
//LED 1.9
else if((V > 5850 && V < 5930) && B > 6350 && H > 6000){
digitalWrite(2, LOW);
digitalWrite(3, LOW);
digitalWrite(4, LOW);
digitalWrite(5, LOW);
digitalWrite(6, LOW);
digitalWrite(7, LOW);
digitalWrite(A3, LOW);
digitalWrite(A4, LOW);
digitalWrite(A5, HIGH);
digitalWrite(A0, LOW);
digitalWrite(A1, HIGH);
digitalWrite(A2, HIGH);
}

// HAED 2
//LED 2.1
else if(V > 6085 && (B > 6150 && B < 6350) && (5850 < H && H < 5900)){
digitalWrite(2, HIGH);
digitalWrite(3, LOW);
digitalWrite(4, LOW);
digitalWrite(5, LOW);
digitalWrite(6, LOW);
digitalWrite(7, LOW);
digitalWrite(A3, LOW);
digitalWrite(A4, LOW);
digitalWrite(A5, LOW);
digitalWrite(A0, HIGH);
digitalWrite(A1, LOW);
digitalWrite(A2, HIGH);
}
//LED 2.2
else if(V > 6085 && (B > 6150 && B < 6350) && (5900 < H && H < 6000)){
digitalWrite(2, LOW);
digitalWrite(3, HIGH);
digitalWrite(4, LOW);
digitalWrite(5, LOW);
digitalWrite(6, LOW);
digitalWrite(7, LOW);
digitalWrite(A3, LOW);
digitalWrite(A4, LOW);
digitalWrite(A5, LOW);
digitalWrite(A0, HIGH);
digitalWrite(A1, LOW);
digitalWrite(A2, HIGH);
}
//LED 2.3
else if(V > 6085 && (B > 6150 && B < 6350) && H > 6000){
digitalWrite(2, LOW);
digitalWrite(3, LOW);
digitalWrite(4, HIGH);
digitalWrite(5, LOW);
digitalWrite(6, LOW);
digitalWrite(7, LOW);
digitalWrite(A3, LOW);
digitalWrite(A4, LOW);
digitalWrite(A5, LOW);
digitalWrite(A0, HIGH);
digitalWrite(A1, LOW);
digitalWrite(A2, HIGH);
}
//LED 2.4
else if((V > 5900 && V < 6085) && (B > 6150 && B < 6350) && (5850 < H && H < 5900)){
digitalWrite(2, LOW);
digitalWrite(3, LOW);
digitalWrite(4, LOW);
digitalWrite(5, HIGH);
digitalWrite(6, LOW);
digitalWrite(7, LOW);
digitalWrite(A3, LOW);
digitalWrite(A4, LOW);
digitalWrite(A5, LOW);
digitalWrite(A0, HIGH);
digitalWrite(A1, LOW);
digitalWrite(A2, HIGH);
}
//LED 2.5
else if((V > 5900 && V < 6085) && (B > 6150 && B < 6350) && (5900 < H && H < 6000)){
digitalWrite(2, LOW);
digitalWrite(3, LOW);
digitalWrite(4, LOW);
digitalWrite(5, LOW);
digitalWrite(6, HIGH);
digitalWrite(7, LOW);
digitalWrite(A3, LOW);
digitalWrite(A4, LOW);
digitalWrite(A5, LOW);
digitalWrite(A0, HIGH);
digitalWrite(A1, LOW);
digitalWrite(A2, HIGH);
}
//LED 2.6
else if((V > 5900 && V < 6085) && (B > 6150 && B < 6350) && H > 6000){
digitalWrite(2, LOW);
digitalWrite(3, LOW);
digitalWrite(4, LOW);
digitalWrite(5, LOW);
digitalWrite(6, LOW);
digitalWrite(7, HIGH);
digitalWrite(A3, LOW);
digitalWrite(A4, LOW);
digitalWrite(A5, LOW);
digitalWrite(A0, HIGH);
digitalWrite(A1, LOW);
digitalWrite(A2, HIGH);
}
//LED 2.7
else if((V > 5850 && V < 5900) && (B > 6150 && B < 6350) && (5850 < H && H < 5900)){
digitalWrite(2, LOW);
digitalWrite(3, LOW);
digitalWrite(4, LOW);
digitalWrite(5, LOW);
digitalWrite(6, LOW);
digitalWrite(7, LOW);
digitalWrite(A3, HIGH);
digitalWrite(A4, LOW);
digitalWrite(A5, LOW);
digitalWrite(A0, HIGH);
digitalWrite(A1, LOW);
digitalWrite(A2, HIGH);
}
//LED 2.8
else if((V > 5850 && V < 5900) && (B > 6150 && B < 6350) && (5900 < H && H < 6000)){
digitalWrite(2, LOW);
digitalWrite(3, LOW);
digitalWrite(4, LOW);
digitalWrite(5, LOW);
digitalWrite(6, LOW);
digitalWrite(7, LOW);
digitalWrite(A3, LOW);
digitalWrite(A4, HIGH);
digitalWrite(A5, LOW);
digitalWrite(A0, HIGH);
digitalWrite(A1, LOW);
digitalWrite(A2, HIGH);
}
//LED 2.9
else if((V > 5850 && V < 5900) && (B > 6150 && B < 6350) && H > 6000){
digitalWrite(2, LOW);
digitalWrite(3, LOW);
digitalWrite(4, LOW);
digitalWrite(5, LOW);
digitalWrite(6, LOW);
digitalWrite(7, LOW);
digitalWrite(A3, LOW);
digitalWrite(A4, LOW);
digitalWrite(A5, HIGH);
digitalWrite(A0, HIGH);
digitalWrite(A1, LOW);
digitalWrite(A2, HIGH);
}

//HAED 3
//LED 3.1
else if(V > 6090 && (B > 6091 && B < 6150) && (5850 < H && H < 5900)){
digitalWrite(2, HIGH);
digitalWrite(3, LOW);
digitalWrite(4, LOW);
digitalWrite(5, LOW);
digitalWrite(6, LOW);
digitalWrite(7, LOW);
digitalWrite(A3, LOW);
digitalWrite(A4, LOW);
digitalWrite(A5, LOW);
digitalWrite(A0, HIGH);
digitalWrite(A1, HIGH);
digitalWrite(A2, LOW);
}
//LED 3.2
else if(V > 6090 && (B > 6091 && B < 6150) && (5900 < H && H < 6000)){
digitalWrite(2, LOW);
digitalWrite(3, HIGH);
digitalWrite(4, LOW);
digitalWrite(5, LOW);
digitalWrite(6, LOW);
digitalWrite(7, LOW);
digitalWrite(A3, LOW);
digitalWrite(A4, LOW);
digitalWrite(A5, LOW);
digitalWrite(A0, HIGH);
digitalWrite(A1, HIGH);
digitalWrite(A2, LOW);
}
//LED 3.3
else if(V > 6090 && (B > 6091 && B < 6150) && H > 6000){
digitalWrite(2, LOW);
digitalWrite(3, LOW);
digitalWrite(4, HIGH);
digitalWrite(5, LOW);
digitalWrite(6, LOW);
digitalWrite(7, LOW);
digitalWrite(A3, LOW);
digitalWrite(A4, LOW);
digitalWrite(A5, LOW);
digitalWrite(A0, HIGH);
digitalWrite(A1, HIGH);
digitalWrite(A2, LOW);
}
//LED 3.4
else if((V > 5930 && V < 6090) && (B > 6091 && B < 6150) && (5850 < H && H < 5900)){
digitalWrite(2, LOW);
digitalWrite(3, LOW);
digitalWrite(4, LOW);
digitalWrite(5, HIGH);
digitalWrite(6, LOW);
digitalWrite(7, LOW);
digitalWrite(A3, LOW);
digitalWrite(A4, LOW);
digitalWrite(A5, LOW);
digitalWrite(A0, HIGH);
digitalWrite(A1, HIGH);
digitalWrite(A2, LOW);
}
//LED 3.5
else if((V > 5930 && V < 6090) && (B > 6091 && B < 6150) && (5900 < H && H < 6000)){
digitalWrite(2, LOW);
digitalWrite(3, LOW);
digitalWrite(4, LOW);
digitalWrite(5, LOW);
digitalWrite(6, HIGH);
digitalWrite(7, LOW);
digitalWrite(A3, LOW);
digitalWrite(A4, LOW);
digitalWrite(A5, LOW);
digitalWrite(A0, HIGH);
digitalWrite(A1, HIGH);
digitalWrite(A2, LOW);
}
//LED 3.6
else if((V > 5930 && V < 6090) && (B > 6091 && B < 6150) && H > 6000){
digitalWrite(2, LOW);
digitalWrite(3, LOW);
digitalWrite(4, LOW);
digitalWrite(5, LOW);
digitalWrite(6, LOW);
digitalWrite(7, HIGH);
digitalWrite(A3, LOW);
digitalWrite(A4, LOW);
digitalWrite(A5, LOW);
digitalWrite(A0, HIGH);
digitalWrite(A1, HIGH);
digitalWrite(A2, LOW);
}
//LED 3.7
else if((V > 5850 && V < 5930) && (B > 6091 && B < 6150) && (5850 < H && H < 5900)){
digitalWrite(2, LOW);
digitalWrite(3, LOW);
digitalWrite(4, LOW);
digitalWrite(5, LOW);
digitalWrite(6, LOW);
digitalWrite(7, LOW);
digitalWrite(A3, HIGH);
digitalWrite(A4, LOW);
digitalWrite(A5, LOW);
digitalWrite(A0, HIGH);
digitalWrite(A1, HIGH);
digitalWrite(A2, LOW);
}
//LED 3.8
else if((V > 5850 && V < 5930) && (B > 6091 && B < 6150) && (5900 < H && H < 6000)){
digitalWrite(2, LOW);
digitalWrite(3, LOW);
digitalWrite(4, LOW);
digitalWrite(5, LOW);
digitalWrite(6, LOW);
digitalWrite(7, LOW);
digitalWrite(A3, LOW);
digitalWrite(A4, HIGH);
digitalWrite(A5, LOW);
digitalWrite(A0, HIGH);
digitalWrite(A1, HIGH);
digitalWrite(A2, LOW);
}
//LED 3.9
else if((V > 5850 && V < 5930) && (B > 6091 && B < 6150) && H > 6000){
digitalWrite(2, LOW);
digitalWrite(3, LOW);
digitalWrite(4, LOW);
digitalWrite(5, LOW);
digitalWrite(6, LOW);
digitalWrite(7, LOW);
digitalWrite(A3, LOW);
digitalWrite(A4, LOW);
digitalWrite(A5, HIGH);
digitalWrite(A0, HIGH);
digitalWrite(A1, HIGH);
digitalWrite(A2, LOW);
}
/*
else
{
digitalWrite(2, LOW);
digitalWrite(3, LOW);
digitalWrite(4, LOW);
digitalWrite(5, LOW);
digitalWrite(6, LOW);
digitalWrite(7, LOW);
digitalWrite(A3, LOW);
digitalWrite(A4, LOW);
digitalWrite(A5, LOW);
digitalWrite(A0, HIGH);
digitalWrite(A1, HIGH);
digitalWrite(A2, HIGH);
}
*/

}

long time(int pin, byte mask) {
unsigned long count = 0, total = 0;
while(checkTimer() < refresh) {
// pinMode is about 6 times slower than assigning
// DDRB directly, but that pause is important
pinMode(pin, OUTPUT);
PORTB = 0;
pinMode(pin, INPUT);
while((PINB & mask) == 0)
count++;
total++;
}
startTimer();
return (count << resolution) / total;
}

extern volatile unsigned long timer0_overflow_count;

void startTimer() {
timer0_overflow_count = 0;
TCNT0 = 0;
}

unsigned long checkTimer() {
return ((timer0_overflow_count << 8) + TCNT0) << 2;
}



(4-4/9)