"No light Home" - Quinta fase - Etapa intermediária
Autor: - | Publicado: 2012-06-04T13:02:00-07:00








Etapa intermediária
Terceira semana(16/04-20/04): Pausa para a revisão e a P1 de Física.








Quarta semana(24/04-27/04): Etapa final para o desenvolvimento da maquete e do protoboard.


Quinta semana(30/04-04/05): Desenvolvimento inicial do código.


Sexta semana(07/05-11/05): Desenvolvimento intermediário do código, dúvidas sobre o projeto(em sala) e ajustes.




int s0,s1,s2;

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

int ledPin = 12,ledPin1 = 13;

int ctl_1 = 0,ctl_2 = 0;

void setup()



{



&nbsp; //4051 digital control pins



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



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



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





&nbsp; Serial.begin(9600);



}

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 LDR5) { &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; int luz = 100;

&nbsp; &nbsp; &nbsp;

&nbsp; &nbsp; if(media &gt; luz ) //LUZ

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

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

&nbsp; &nbsp;&nbsp;

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

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

&nbsp; &nbsp;&nbsp;

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

&nbsp; &nbsp;&nbsp;

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

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

&nbsp; &nbsp;&nbsp;

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

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

&nbsp; &nbsp;&nbsp;

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

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

&nbsp; &nbsp;&nbsp;

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

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

&nbsp; &nbsp;&nbsp;

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

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

&nbsp; &nbsp;&nbsp;

&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(9,LOW);

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

&nbsp; &nbsp; &nbsp;

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

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

&nbsp; &nbsp; &nbsp; }

&nbsp; &nbsp; else

&nbsp; &nbsp; {

&nbsp; &nbsp; &nbsp; &nbsp; if ((desvio0 &lt; 900) ||(desvio2 &lt; 700) ||(desvio1 &lt; 900) ||(desvio3 &lt; 600)) {&nbsp;

&nbsp; &nbsp;&nbsp;

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

&nbsp; &nbsp;&nbsp;

&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; &nbsp; &nbsp; &nbsp; &nbsp;

&nbsp; &nbsp; }

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

&nbsp; &nbsp;&nbsp;

} &nbsp;&nbsp;



void luz(int ldr1,int ldr2){&nbsp;

&nbsp;&nbsp;

&nbsp; if(ldr1&lt;200) ctl_1 = (ctl_1+1)%2;

&nbsp; if(ctl_1==0) ctl_2=0;

&nbsp; if(ctl_1==1 &amp;&amp;ldr2&lt;200){

&nbsp; &nbsp; ctl_2 = 1;

&nbsp; &nbsp; }

&nbsp;&nbsp;

&nbsp; if( ctl_2 ==1) {

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

&nbsp; &nbsp; delay(100);

&nbsp; }

&nbsp; else digitalWrite(ledPin1, LOW);

&nbsp;&nbsp;

&nbsp; &nbsp;

}

&nbsp; &nbsp;&nbsp;

void loop()

&nbsp; &nbsp;

{



&nbsp; //Read Value of 4051 analog-in 0 by setting the values of s0,s1 and s2



&nbsp; digitalWrite(8, LOW);



&nbsp; digitalWrite(9, LOW);



&nbsp; digitalWrite(10, LOW);



&nbsp; delay(10); //not sure if this delay is strictly necessary



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



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



&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;digitalWrite(9, s1);//s1



&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; L4 = analogRead(A3);

&nbsp; &nbsp; L5 = &nbsp;analogRead(A5);

&nbsp; &nbsp; L6 = &nbsp;analogRead(A4);

&nbsp; &nbsp; luz(L5,L6);

&nbsp; &nbsp; comodo(L0,L1,L2,L3,L4,L5);

&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;}





}




Sétima semana(14/05-18/05): Desenvolvimento final do código.