"No light Home" - Oitava fase - Etapa final
Autor: - | Publicado: 2012-06-07T15:54:00-07:00



Nona semana(28/05-01/06):&nbsp;Aprimoramento final da maquete e do código.















Código :



int s0,s1,s2;

int L0=0, L1=0,L2=0, L3=0,L4=0,L5 =0,L6 = 0;

int ledPin = 1,ledPin1 = 13,ledPin2 = 12;

int ctl_1 = 0,ctl_2 = 0;

int A=0;

int calibL4 = 400, calibL5=200,calibL6=300;//valores de base para sensores da porta

int calibD0=1000,calibD1=800, calibD2=800,calibD3=600;//valores de base para disparo de sensores da parede

void setup()

&nbsp;

{

&nbsp;

&nbsp; //4051 digital control pins

&nbsp;

&nbsp; pinMode(8, OUTPUT); &nbsp; &nbsp;// s0

&nbsp;

&nbsp; pinMode(9, OUTPUT); &nbsp; &nbsp;// s1

&nbsp;

&nbsp; pinMode(10, OUTPUT); &nbsp; &nbsp;// s2

&nbsp;

&nbsp;

&nbsp; Serial.begin(9600);

&nbsp;

}

int binario(int n, int b){

&nbsp; &nbsp; int r[3],a[3];

&nbsp; &nbsp; int m10=1;

&nbsp; &nbsp; int z=0;

&nbsp; &nbsp;&nbsp;

&nbsp; &nbsp; int q=n,c=2;

&nbsp; &nbsp; for(int i=0;i&lt;3;i++) r[i] = 0;

&nbsp; &nbsp;&nbsp;

&nbsp; &nbsp; do{

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;r[c] = q%2;

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;q = q /2;

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;z = (r[c]*m10)+z;

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;m10 = m10 * 10;

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;c = c - 1;

&nbsp; &nbsp; &nbsp; &nbsp; }while(q!= 0);





&nbsp; &nbsp; return (r[b]); &nbsp; &nbsp;

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;

&nbsp; &nbsp; }

&nbsp; &nbsp;&nbsp;

void comodo(float LDR0,float LDR1,float LDR2,float LDR3,float LDR4, float LDR6) { &nbsp; &nbsp;

&nbsp; &nbsp; int media, desvio0, desvio1, desvio2, desvio3,desvio4;

&nbsp; &nbsp;&nbsp;

&nbsp; &nbsp; media = (LDR0 + LDR1 + LDR2 + LDR3)/4;

&nbsp; &nbsp;&nbsp;

&nbsp; &nbsp;&nbsp;

&nbsp; &nbsp; Serial.print(" &nbsp; ");&nbsp;

&nbsp; &nbsp; Serial.print(" media: ");&nbsp;

&nbsp; &nbsp; Serial.print(media);&nbsp;

&nbsp; &nbsp;&nbsp;

&nbsp; &nbsp; desvio0 = (LDR0/media)*1000;

&nbsp; &nbsp; desvio1 = (LDR1/media)*1000;

&nbsp; &nbsp; desvio2 = (LDR2/media)*1000;

&nbsp; &nbsp; desvio3 = (LDR3/media)*1000;

&nbsp; &nbsp; desvio4 = (LDR4/media)*1000;

&nbsp; &nbsp;&nbsp;

&nbsp; &nbsp; Serial.print(" &nbsp;");&nbsp;

&nbsp; &nbsp; Serial.print(" D0: ");&nbsp;

&nbsp; &nbsp; Serial.print(desvio0);&nbsp;

&nbsp; &nbsp; Serial.print(" D1: ");&nbsp;

&nbsp; &nbsp; Serial.print(desvio1);&nbsp;

&nbsp; &nbsp; Serial.print(" D2: ");&nbsp;

&nbsp; &nbsp; Serial.print(desvio2);&nbsp;

&nbsp; &nbsp; Serial.print(" D3: ");&nbsp;

&nbsp; &nbsp; Serial.print(desvio3);&nbsp;

&nbsp; &nbsp;&nbsp;

&nbsp; &nbsp;&nbsp;

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if ((desvio0 &lt; calibD0) ) {&nbsp;

&nbsp; &nbsp;&nbsp;

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; digitalWrite(ledPin, HIGH);&nbsp;

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; tone(5,261.626);

&nbsp; &nbsp;&nbsp;

&nbsp; &nbsp; &nbsp; &nbsp; }else if ((desvio1 &lt; calibD1) ) {&nbsp;

&nbsp; &nbsp;&nbsp;

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; digitalWrite(ledPin, HIGH);&nbsp;

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; tone(5,329.628);

&nbsp; &nbsp;&nbsp;

&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp;

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;else if ((desvio2 &lt; calibD2) ) {&nbsp;

&nbsp; &nbsp;&nbsp;

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; digitalWrite(ledPin, HIGH);&nbsp;

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; tone(5,391.995);

&nbsp; &nbsp;&nbsp;

&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp;

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else if ((desvio3 &lt; calibD3)) {&nbsp;

&nbsp; &nbsp;&nbsp;

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; digitalWrite(ledPin, HIGH);&nbsp;

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; tone(5,440);

&nbsp; &nbsp;&nbsp;

&nbsp; &nbsp; &nbsp; &nbsp; }else if(LDR6&lt;calibL6){

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; tone(5,220);

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; tone(5,440);&nbsp;

&nbsp; &nbsp; &nbsp; &nbsp; }

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else {

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; digitalWrite(ledPin, LOW);

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; tone(5,LOW);

&nbsp; &nbsp; &nbsp; &nbsp; }

&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;

&nbsp; &nbsp; &nbsp;&nbsp;

&nbsp; &nbsp;&nbsp;

} &nbsp;&nbsp;



int luz(int a){

&nbsp; &nbsp;&nbsp;

&nbsp; &nbsp; int n1,n2,c1,c2;

&nbsp; &nbsp; ////////////////////

&nbsp; &nbsp;&nbsp;

&nbsp; &nbsp; if (a==0){





 &nbsp;&nbsp; &nbsp; n1 = &nbsp;analogRead(A4); //le L5 &nbsp;

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;



&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if(n1&lt;calibL5){

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; c1= 1;

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if(c1==1){

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;

   &nbsp;&nbsp;n2 = &nbsp;analogRead(A5);//le L6



&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(n2&lt;calibL6){

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;c2 =1;

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}else c2 = 0;

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (c2 == 1){

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;

    &nbsp;&nbsp; &nbsp; &nbsp;digitalWrite(ledPin1, HIGH); //ascende a luz &nbsp;

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;  &nbsp;&nbsp; &nbsp; &nbsp;digitalWrite(ledPin, HIGH); //ascende a luz

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; c2=0;

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; c1 = 0;

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; a=1;

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } &nbsp;

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }

&nbsp; &nbsp; }

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;



/////////////

&nbsp; &nbsp; if(a==1){

 &nbsp;&nbsp; &nbsp; n2 = &nbsp;analogRead(A5);//le L6



&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if(n2&lt;calibL6){

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; c2= 1; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if(c2==1){

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //c2=0;



   &nbsp;&nbsp;n1 = &nbsp;analogRead(A4); //le L5

&nbsp;&nbsp;



&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(n1&lt;calibL5){

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;c1 =1; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}else c1=0;

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (c1 == 1){

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; digitalWrite(ledPin1, LOW);//desliga a luz

    &nbsp;&nbsp; &nbsp; &nbsp;digitalWrite(ledPin, LOW);//desliga a luz

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; c1=0;

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; c2=0;

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; a=0;

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } &nbsp;

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }

&nbsp; &nbsp; }

/////////

&nbsp; &nbsp; return a;

&nbsp; &nbsp;&nbsp;

&nbsp; &nbsp;&nbsp;

&nbsp; &nbsp;&nbsp;

}



&nbsp; &nbsp;&nbsp;

void loop()

&nbsp; &nbsp;

{

&nbsp;

&nbsp;

&nbsp; digitalWrite(8, LOW);

&nbsp;

&nbsp; digitalWrite(9, LOW);

&nbsp;

&nbsp; digitalWrite(10, LOW);

&nbsp;&nbsp;

&nbsp; int readInZero = analogRead(0); &nbsp;// read the input pin

&nbsp;

&nbsp; //Serial.print(readInZero); //use the result

&nbsp;

&nbsp; &nbsp;for(int j=0; j&lt;=7;j++){

&nbsp; &nbsp; &nbsp;

&nbsp; &nbsp; &nbsp;s0 = binario(j,0);

&nbsp; &nbsp; &nbsp;s1 = binario(j,1);

&nbsp; &nbsp; &nbsp;s2 = binario(j,2);

&nbsp; &nbsp; &nbsp;

&nbsp; &nbsp; &nbsp;digitalWrite(8, s0);//s0

&nbsp;

&nbsp; &nbsp; &nbsp;digitalWrite(9, s1);//s1

&nbsp;

&nbsp; &nbsp; &nbsp;digitalWrite(10, s2);//s2

&nbsp; &nbsp; &nbsp;

&nbsp; &nbsp; &nbsp;

&nbsp; &nbsp; &nbsp;

&nbsp; &nbsp; &nbsp;int readInTwo= analogRead(0);

&nbsp; &nbsp; &nbsp;

&nbsp; &nbsp; &nbsp;switch(j){

&nbsp; &nbsp; &nbsp; case 0:&nbsp;

&nbsp; &nbsp; &nbsp; &nbsp; L0 = readInTwo;

&nbsp; &nbsp; &nbsp; &nbsp; break;

&nbsp; &nbsp; &nbsp; case 4:&nbsp;

&nbsp; &nbsp; &nbsp; &nbsp; L1 = readInTwo;

&nbsp; &nbsp; &nbsp; &nbsp; break;

&nbsp; &nbsp; &nbsp; case 2:&nbsp;

&nbsp; &nbsp; &nbsp; &nbsp; L2 = readInTwo;

&nbsp; &nbsp; &nbsp; &nbsp; break;

&nbsp; &nbsp; &nbsp; case 6:&nbsp;

&nbsp; &nbsp; &nbsp; &nbsp; L3 = readInTwo;

&nbsp; &nbsp; &nbsp; &nbsp; break;

&nbsp; &nbsp; &nbsp; default:

&nbsp; &nbsp; &nbsp; &nbsp; break;

&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;

&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;

&nbsp; &nbsp; &nbsp; }

&nbsp; &nbsp; &nbsp;&nbsp;

&nbsp; &nbsp; L4 = analogRead(A3);

&nbsp; &nbsp; L5 = &nbsp;analogRead(A4);//n1

&nbsp; &nbsp; L6 = &nbsp;analogRead(A5);//n2



&nbsp; &nbsp; if(L4&lt;calibL4){

&nbsp; &nbsp; &nbsp; A = luz(A);

&nbsp; &nbsp; &nbsp; &nbsp;

&nbsp; &nbsp; }

&nbsp; &nbsp;&nbsp;

&nbsp; &nbsp; comodo(L0,L1,L2,L3,L4,L6);//controla sinais sonoros no comodo

&nbsp; &nbsp;&nbsp;

&nbsp; &nbsp; Serial.print("\n &nbsp;L0 ");

&nbsp; &nbsp; Serial.print(L0);

&nbsp; &nbsp; Serial.print(" &nbsp;L1 ");

&nbsp; &nbsp; Serial.print(L1);

&nbsp; &nbsp; Serial.print(" &nbsp;L2 ");

&nbsp; &nbsp; Serial.print(L2);

&nbsp; &nbsp; Serial.print(" &nbsp;L3 ");

&nbsp; &nbsp; Serial.print(L3);

&nbsp; &nbsp; Serial.print(" &nbsp;L4 ");

&nbsp; &nbsp; Serial.print(L4);

&nbsp; &nbsp; Serial.print(" &nbsp;L5 ");

&nbsp; &nbsp; Serial.print(L5);

&nbsp; &nbsp; Serial.print(" &nbsp;L6 ");

&nbsp; &nbsp; Serial.print(L6);

&nbsp; &nbsp;&nbsp;

&nbsp; &nbsp;&nbsp;

&nbsp; &nbsp; &nbsp;

&nbsp; &nbsp;}

&nbsp;



&nbsp;

}

//luz

//som







Décima semana(04/06-08/06): Testes.