Friday, November 4, 2016

8. Programa com while e controle de entrada serial

Leds controlados por comando while e comando de entrada.



int pinLed[10] = {13,12,11,10,9,8,7,6,5,4};
int numLed;
int x;
char digitado; 

  void setup(){
    
  for(x = 0; x<=9; x = x+1){
      pinMode(pinLed[x], OUTPUT);
  }
  Serial.begin(9600);
}
  
  void loop() {
  digitado = ' ';
  numLed = 0;
  while( digitado !='P'){
      digitalWrite(pinLed[numLed], LOW);
      
      numLed = numLed+1;
    if (numLed>9){
        numLed = 0;
    }
    digitalWrite(pinLed[numLed], HIGH);  
    if (Serial.available()){
        digitado = Serial.read();
    }
    delay(400);
  }
delay(3000);
 }


Exercício:

Entenda todos os passos do programa acima. Troque a ordem de acendimento dos leds, invente padrões que alternam o padrão de acendimento dos leds.

Wednesday, November 2, 2016

7. Tipos de variáveis

Esta atividade envolve somente prática com os diferentes tipos de variáveis, utilizando somente a saída serial da placa Uno para o monitor. Assista o vídeo,  simule os exemplos em Autodesk Circuits e finalmente implemente na prática.

Referências adicionais:
1. Operadores booleanos
2. Arduino language reference
3. Tipos de variáveis: (Sparkfun)
  • boolean (8 bit) - simple logical true/false
  • byte (8 bit) - unsigned number from 0-255
  • char (8 bit) - signed number from -128 to 127. The compiler will attempt to interpret this data type as a character in some circumstances, which may yield unexpected results
  • unsigned char (8 bit) - same as ‘byte’; if this is what you’re after, you should use ‘byte’ instead, for reasons of clarity
  • word (16 bit) - unsigned number from 0-65535
  • unsigned int (16 bit)- the same as ‘word’. Use ‘word’ instead for clarity and brevity
  • int (16 bit) - signed number from -32768 to 32767. This is most commonly what you see used for general purpose variables in Arduino example code provided with the IDE
  • unsigned long (32 bit) - unsigned number from 0-4,294,967,295. The most common usage of this is to store the result of the millis() function, which returns the number of milliseconds the current code has been running
  • long (32 bit) - signed number from -2,147,483,648 to 2,147,483,647
  • float (32 bit) - signed number from -3.4028235E38 to 3.4028235E38. Floating point on the Arduino is not native; the compiler has to jump through hoops to make it work. If you can avoid it, you should. 
Um exemplo de código usado na simulação envolvendo diferentes tipos de dados:
int x;
int y;
int z;

boolean A;
boolean B;

void setup(){

Serial.begin(9600);
Serial.println("Eu sou o Arduino");
  
x = 2;
y = 33000;
z =-x+y;

Serial.print("x =");
Serial.print(x);
A = 0;
B = 1;
  
Serial.print("  y =");
Serial.print(y,BIN);
  
Serial.print("  z =");
Serial.println(z, BIN);

Serial.print("A AND B =");
Serial.println(A && B);
  
Serial.print("A OR B =");
Serial.println(A || B);
  
Serial.print("NOT(A OR B) AND B =");
Serial.println(!(A || B) && B);

}

void loop(){
}  


Exercícios

1. Use variáveis booleanas no Arduino para determinar a tabela-verdade para (AB+C)+B+A'C, onde a multiplicação representa o operador AND, a soma representa OR e A' significa NOT(A).

2. Escreva um programa no Arduíno no qual,  dados A, B, C inteiros, ele verifica se a igualdade A^2+B^2=C^2 é satisfeita (ou seja, A,B, C formam uma tripla pitagórica), respondendo com "verdadeiro" ou "falso".

Tuesday, November 1, 2016

6. Laços (loops) com for e while

Assista o vídeo e entenda o funcionamento do comando for.   O código sketch é mostrado abaixo.

int pinLed[10] = {13,12,11,10,9,8,7,6,5,4};
int numLed;
int x;
  void setup(){
  for(x = 0; x<=9; x = x+1){
      pinMode(pinLed[x], OUTPUT);
  }
}
  
  void loop() {
  for(numLed = 0; numLed<=9; numLed = numLed+1){
      digitalWrite(pinLed[numLed], HIGH);
      delay(50);
  }
  for (numLed = 9; numLed>=0; numLed = numLed-1){
      digitalWrite(pinLed[numLed], LOW);
      delay(200);
  }
}

Este é código usando while: 

int pinLed[10] = {13,12,11,10,9,8,7,6,5,4};
int numLed;
int x;
  
  void setup(){

    
  for(x = 0; x<=9; x = x+1){
      pinMode(pinLed[x], OUTPUT);
  }
}
  
  void loop() {
  numLed = 0;
  while( numLed<=9){
      digitalWrite(pinLed[numLed], HIGH);
      delay(50);
      ; numLed = numLed+1;
  }
    numLed = 9;
    while (numLed>=0){
      digitalWrite(pinLed[numLed], LOW);
      delay(200);
      ; numLed = numLed-1;
  }
}

Aqui o correspondente diagrama do circuito desenhado em Tinkercad e a simulação:



Exercícios:

1. Construa a simulação proposta e implemente-a na prática.
2. Modifique o padrão sequencial acima de modo a gerar diferentes padrões de luzes.