Last active
December 21, 2015 08:38
-
-
Save brainrake/6279046 to your computer and use it in GitHub Desktop.
photonic testes
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
testies |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#define pIR 0 | |
#define pTrig 12 | |
#define pEcho 13 | |
#define pBoot 6 | |
unsigned char vals[16]; | |
int state = 0; | |
#define aIdle 0 | |
#define aLand 1 | |
#define aAct 2 | |
void setup() { | |
pinMode(pIR, OUTPUT); | |
digitalWrite(pIR, 0); | |
pinMode(pTrig, OUTPUT); | |
digitalWrite(pTrig, 0); | |
pinMode(pEcho, INPUT); | |
pinMode(11, OUTPUT); | |
digitalWrite(11,1); | |
pinMode(7, OUTPUT); | |
digitalWrite(7, 1); | |
pinMode(5, OUTPUT); | |
digitalWrite(5, 0); | |
pinMode(pBoot, INPUT); | |
//Serial.begin(9600); | |
} | |
void boot() { | |
clr(0); | |
sendall(vals); | |
delay(500); | |
clr(7); | |
sendall(vals); | |
delay(500); | |
clr(1); | |
sendall(vals); | |
delay(500); | |
clr(2); | |
sendall(vals); | |
delay(500); | |
clr(4); | |
sendall(vals); | |
delay(500); | |
clr(0); | |
vals[0] = 7; | |
for(int i=0; i<16; i++) { | |
sendall(vals); | |
delay(100); | |
sh(); | |
} | |
clr(0); | |
sendall(vals); | |
delay(500); | |
clr(0); | |
vals[0] = 1; | |
for(int i=0; i<16; i++) { | |
sendall(vals); | |
delay(100); | |
sh(); | |
} | |
clr(0); | |
sendall(vals); | |
delay(500); | |
clr(0); | |
vals[0] = 2; | |
for(int i=0; i<16; i++) { | |
sendall(vals); | |
delay(100); | |
sh(); | |
} | |
clr(0); | |
sendall(vals); | |
delay(500); | |
clr(0); | |
vals[0] = 4; | |
for(int i=0; i<16; i++) { | |
sendall(vals); | |
delay(100); | |
sh(); | |
} | |
clr(0); | |
sendall(vals); | |
delay(500); | |
} | |
void sendbit(boolean val) { | |
digitalWrite(pIR, 0); | |
delayMicroseconds(400); | |
digitalWrite(pIR, 1); | |
delayMicroseconds(400 + 400*val); | |
} | |
void sendall(unsigned char vals[16]) { | |
sendbit(1); | |
for (int i=0; i<16; i++) { | |
sendbit(bitRead(vals[i], 0)); | |
sendbit(bitRead(vals[i], 1)); | |
sendbit(bitRead(vals[i], 2)); | |
} | |
sendbit(1); | |
sendbit(1); | |
delay(3); | |
} | |
void step() { | |
if (state == aIdle) { | |
clr(0); | |
if(random() % 3 == 0) { | |
vals[random() % 16] = random() % 8; | |
sendall(vals); | |
delay(20); | |
clr(0); | |
sendall(vals); | |
delay(80); | |
} else { | |
delay(100); | |
} | |
} | |
if (state == aLand) { | |
sh(); | |
sendall(vals); | |
delay(30); | |
} | |
} | |
void init(int wat) { | |
state = wat; | |
if (state == aIdle) { | |
clr(0); | |
} | |
if (state == aLand) { | |
clr(0); | |
vals[0] = 1; | |
vals[1] = 2; | |
vals[2] = 4; | |
vals[8] = 1; | |
vals[9] = 2; | |
vals[10] = 4; | |
} | |
} | |
void clr(int val) { | |
for (int i=0; i<16; i++) {vals[i] = val;} | |
} | |
void sh() { | |
unsigned char t = vals[15]; | |
for (int ii=14; ii>=0; ii--) {vals[ii+1] = vals[ii];} | |
vals[0] = t; | |
} | |
double range() { | |
long duration; | |
digitalWrite(pTrig, LOW); | |
delayMicroseconds(2); | |
digitalWrite(pTrig, HIGH); | |
delayMicroseconds(10); | |
digitalWrite(pTrig, LOW); | |
duration = pulseIn(pEcho, HIGH, 50000); | |
return (duration/2.0) / 29.1; | |
} | |
void loop() { | |
if(digitalRead(pBoot)) { | |
boot(); | |
} else { | |
double ran = range(); | |
//Serial.println(ran); | |
//delay(500); | |
if(ran){ | |
clr(0); | |
sendall(vals); | |
delay(100); | |
//init(1); | |
//for(int i=0; i<100; i++) {step();} | |
//init(0); | |
for (int i=0; i<100; i++) { | |
delay(60); | |
int r = random()%16; | |
if (random() % 4) { | |
vals[r] = random() % 8; | |
} else { | |
vals[r] = 0; | |
} | |
//vals[random()%16] = random() % 8; | |
sh(); | |
sendall(vals); | |
} | |
for (int i=0; i<30; i++) { | |
delay(100); | |
vals[random()%16] = 0; | |
sh(); | |
sendall(vals); | |
} | |
} else { | |
step(); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Set the frequency that we will get on pin OCR1A but don't turn it on | |
void setFrequency(uint16_t freq) | |
{ | |
uint32_t requiredDivisor = (F_CPU/2)/(uint32_t)freq; | |
uint16_t prescalerVal = 1; | |
uint8_t prescalerBits = 1; | |
while ((requiredDivisor + prescalerVal/2)/prescalerVal > 256) | |
{ | |
++prescalerBits; | |
prescalerVal <<= 1; | |
} | |
uint8_t top = ((requiredDivisor + (prescalerVal/2))/prescalerVal) - 1; | |
TCCR1 = (1 << CTC1) | prescalerBits; | |
GTCCR = 0; | |
OCR1C = top; | |
} | |
// Turn the frequency on | |
void on() | |
{ | |
TCNT1 = 0; | |
TCCR1 |= (1 << COM1A0); | |
} | |
// Turn the frequency off and turn off the IR LED. | |
// We let the counter continue running, we just turn off the OCR1A pin. | |
void off() | |
{ | |
TCCR1 &= ~(1 << COM1A0); | |
} | |
void setup() | |
{ | |
pinMode(3, INPUT); | |
PORTB = 0; | |
DDRB = 0b00000010; // set PB1 (= OCR1A) to be an output | |
setFrequency(38000); | |
} | |
int ison = 0; | |
int in = 0; | |
void loop() { | |
in = ! digitalRead(3); | |
if(!ison and in){ | |
ison=1; | |
on(); | |
} else if (ison and !in) { | |
ison=0; | |
off(); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// assign a different ID to each device | |
#define ID 0 | |
#define pR 2 | |
#define pG 0 | |
#define pB 1 | |
#define pIn 3 | |
void setup(){ | |
pinMode(pR,OUTPUT); | |
pinMode(pG,OUTPUT); | |
pinMode(pB,OUTPUT); | |
pinMode(pIn,INPUT); | |
} | |
void loop(){ | |
while(1) { | |
if (!digitalRead(pIn)) { | |
break; | |
} | |
} | |
int n = 0; | |
while(1) { | |
unsigned long l = pulseIn(pIn, 1, 1500); | |
if (l == 0) { | |
break; | |
} | |
if (n == 1 + (ID * 3) + 0) { | |
if (l > 600) { | |
digitalWrite(pR,1); | |
} else { | |
digitalWrite(pR,0); | |
} | |
} | |
if (n == 1 + (ID * 3) + 1) { | |
if (l > 600) { | |
digitalWrite(pG,1); | |
} else { | |
digitalWrite(pG,0); | |
} | |
} | |
if (n == 1 + (ID * 3) + 2) { | |
if (l > 600) { | |
digitalWrite(pB,1); | |
} else { | |
digitalWrite(pB,0); | |
} | |
} | |
n += 1; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment