Categories
Arduino BattleStar Galactica Nerdice

Meu primeiro Cylon…

E ainda no espírito de brincadeira, juntei dois dos exemplos disponíveis para estudo no site do Arduino (AnalogInput e Blink) e fiz a brincadeira abaixo:


Cylon!!!!

A lista do material necessário:

  • 1x – Arduino Duemilanove.
  • 6x – LED vermelho.
  • 6x – Resistor de 220 ohm.
  • 1x – Potenciômetro (10-kilohm).
  • 1x – Protoboard (breadboard).
  • E vários pedaços de fio!

Abaixo, o esquemático do Arduino e ProtoBoard:

Sketch da montagem do Cylon!

Abaixo, o código:



/*
Misturando dois exemplos do tutorial pra fazer
um "Cylon" com velocidade controlada via Potenciometro!
*/

int sensorPin = A0;    // select the input pin for the potentiometer
int sensorValue = 0;  // variable to store the value coming from the sensor

void setup() {
// initialize the digital pin as an output.
// Pin 2, 4, 6, 8, 10 and 12 has an LED connected on most Arduino boards:
pinMode(2, OUTPUT);
pinMode(4, OUTPUT);
pinMode(6, OUTPUT);
pinMode(8, OUTPUT);
pinMode(10, OUTPUT);
pinMode(12, OUTPUT);
}

void loop() {
// read the value from the sensor:
sensorValue = analogRead(sensorPin);

digitalWrite(2, HIGH);    // set the LED on
delay(sensorValue);       // wait for a little time!
digitalWrite(4, HIGH);    // set the LED on
delay(sensorValue);       // wait for a little time!

// read the value from the sensor:
sensorValue = analogRead(sensorPin);

digitalWrite(2, LOW);     // set the LED off
delay(sensorValue);       // wait for a little time!
digitalWrite(6, HIGH);    // set the LED on
delay(sensorValue);       // wait for a little time!

// read the value from the sensor:
sensorValue = analogRead(sensorPin);

digitalWrite(4, LOW);     // set the LED off
delay(sensorValue);       // wait for a little time!
digitalWrite(8, HIGH);    // set the LED on
delay(sensorValue);       // wait for a little time!

// read the value from the sensor:
sensorValue = analogRead(sensorPin);

digitalWrite(6, LOW);     // set the LED off
delay(sensorValue);       // wait for a little time!
digitalWrite(10, HIGH);   // set the LED on
delay(sensorValue);       // wait for a little time!

// read the value from the sensor:
sensorValue = analogRead(sensorPin);

digitalWrite(8, LOW);     // set the LED off
delay(sensorValue);       // wait for a little time!
digitalWrite(12, HIGH);   // set the LED on
delay(sensorValue);       // wait for a little time!
digitalWrite(10, HIGH);   // set the LED on
delay(sensorValue);       // wait for a little time!

// read the value from the sensor:
sensorValue = analogRead(sensorPin);

digitalWrite(12, LOW);    // set the LED off
delay(sensorValue);       // wait for a little time!
digitalWrite(8, HIGH);    // set the LED on
delay(sensorValue);       // wait for a little time!

// read the value from the sensor:
sensorValue = analogRead(sensorPin);

digitalWrite(10, LOW);    // set the LED off
delay(sensorValue);       // wait for a little time!
digitalWrite(6, HIGH);    // set the LED on
delay(sensorValue);       // wait for a little time!

// read the value from the sensor:
sensorValue = analogRead(sensorPin);

digitalWrite(8, LOW);     // set the LED off
delay(sensorValue);       // wait for a little time!
digitalWrite(4, HIGH);    // set the LED on
delay(sensorValue);       // wait for a little time!

// read the value from the sensor:
sensorValue = analogRead(sensorPin);


digitalWrite(6, LOW);     // set the LED off
delay(sensorValue);       // wait for a little time!
}

Obviamente deve ser possível fazer a mesmíssima coisa com uma codificação muito menos “tosca” que esta. Talvez usando arroz… 😉 Não sou um programador e nem tenho a pretensão de ser um… Mas me diverti bastante com isto.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.