Projeto 1: “Olá Mundo”
int val;//define variable val
int ledpin=13;// define digital interface 13
void setup()
{
Serial.begin(9600);// set the baud rate at 9600 to match the software set up. When connected to a specific device, (e.g. bluetooth), the baud rate needs to be the same with it.
pinMode(ledpin,OUTPUT);// initialize digital pin 13 as output. When using I/O ports on an Arduino, this kind of set up is always needed.
}
void loop()
{
val=Serial.read();// read the instruction or character from PC to Arduino, and assign them to Val.
if(val=='R')// determine if the instruction or character received is “R”.
{ // if it’s “R”,
digitalWrite(ledpin,HIGH);// set the LED on digital pin 13 on.
delay(500);
digitalWrite(ledpin,LOW);// set the LED on digital pin 13 off.
delay(500);
Serial.println("Hello World!");// display“Hello World!”string.
}
}
Projeto 2: LED intermitente
int ledPin = 10; // define o pino digital 10.
void setup ()
{
pinMode (ledPin, OUTPUT); // define o pino com o LED conectado como saída.
}
loop void ()
{
digitalWrite (ledPin, ALTO); // ajuste o LED.
atraso (1000); // espera por um segundo.
digitalWrite (ledPin, BAIXO); // define o LED desligado.
atraso (1000); // espera por um segundo
}
Projeto 3: PWM
int potpin = 0; // inicializa o pino analógico 0
int ledpin = 11; // inicializa o pino digital 11 (saída PWM)
int val = 0; // Temporariamente armazena o valor das variáveis do sensor
void setup ()
{
pinMode (ledpin, OUTPUT); // define o pino digital 11 como “output”
Serial.begin (9600); // definir taxa de transmissão em 9600
// atenção: para portas analógicas, elas são configuradas automaticamente como “entrada”
}
loop void ()
{
val = analogRead (potpin); // ler o valor analógico do sensor e atribuí-lo a val
Serial.println (val); // valor de exibição de val
analogWrite (ledpin, val / 4); // ligar o LED e configurar o brilho; a saída máxima de PWM é de 255)
atraso (10); // espera por 0,01 segundo
}
Projeto 4: Luzes de semáforo
int redled =10; // initialize digital pin 8.
int yellowled =7; // initialize digital pin 7.
int greenled =4; // initialize digital pin 4.
void setup()
{
pinMode(redled, OUTPUT);// set the pin with red LED as “output”
pinMode(yellowled, OUTPUT); // set the pin with yellow LED as “output”
pinMode(greenled, OUTPUT); // set the pin with green LED as “output”
}
void loop()
{
digitalWrite(greenled, HIGH);//// turn on green LED
delay(5000);// wait 5 seconds
digitalWrite(greenled, LOW); // turn off green LED
for(int i=0;i<3;i++)// blinks for 3 times
{
delay(500);// wait 0.5 second
digitalWrite(yellowled, HIGH);// turn on yellow LED
delay(500);// wait 0.5 second
digitalWrite(yellowled, LOW);// turn off yellow LED
}
delay(500);// wait 0.5 second
digitalWrite(redled, HIGH);// turn on red LED
delay(5000);// wait 5 second
digitalWrite(redled, LOW);// turn off red LED
}
Projeto 5: Efeito de perseguição de LEDs
int BASE = 2 ; // the I/O pin for the first LED
int NUM = 6; // number of LEDs
void setup()
{
for (int i = BASE; i < BASE + NUM; i ++)
{
pinMode(i, OUTPUT); // set I/O pins as output
}
}
void loop()
{
for (int i = BASE; i < BASE + NUM; i ++)
{
digitalWrite(i, LOW); // set I/O pins as “low”, turn off LEDs one by one.
delay(200); // delay
}
for (int i = BASE; i < BASE + NUM; i ++)
{
digitalWrite(i, HIGH); // set I/O pins as “high”, turn on LEDs one by one
delay(200); // delay
}
}
Projeto 6: LEDs controlados por botão
int ledpin = 11; // inicializa o pin 11
int inpin = 7; // inicializa o pin 7
int val; // define val
void setup ()
{
pinMode (ledpin, OUTPUT); // define o pino do LED como "saída"
pinMode (inpin, INPUT); // define o pino do botão como “entrada”
}
loop void ()
{
val = digitalRead (inpin); // lê o valor do nível do pino 7 e atribui se val
if (val == LOW) // verifica se o botão está pressionado, se sim, liga o LED
{digitalWrite (ledpin, LOW);}
outro
{digitalWrite (ledpin, HIGH);}
}
Projeto 7: Experiência de resposta
int redled = 8; // definir LED vermelho como "saída"
int yellowled = 7; // definir LED amarelo como "saída"
int greenled = 6; // definir LED verde como "saída"
int redpin = 5; // inicializa o pino para o botão vermelho
int yellowpin = 4; // inicializa o pino para o botão amarelo
int greenpin = 3; // inicializa o pino para o botão verde
int restpin = 2; // inicializa o pino para o botão reset
int vermelho;
int amarelo;
int green;
void setup ()
{
pinMode (redled, OUTPUT);
pinMode (amarelado, OUTPUT);
pinMode (greenled, OUTPUT);
pinMode (redpin, INPUT);
pinMode (yellowpin, INPUT);
pinMode (greenpin, INPUT);
}
void loop () // repetidamente lê pinos para botões
{
vermelho = digitalRead (redpin);
amarelo = digitalRead (yellowpin);
verde = digitalRead (greenpin);
if (vermelho == BAIXO) RED_YES ();
if (amarelo == BAIXO) YELLOW_YES ();
if (verde == BAIXO) GREEN_YES ();
}
void RED_YES () // executa o código até que a luz vermelha esteja acesa; end cycle quando o botão de reset é pressionado
{
while (digitalRead (restpin) == 1)
{
digitalWrite (redled, ALTA);
digitalWrite (verde, baixo);
digitalWrite (amarelado, BAIXO);
}
clear_led ();
}
anule YELLOW_YES () // execute o código até que a luz amarela acenda; end cycle quando o botão de reset é pressionado
{
while (digitalRead (restpin) == 1)
{
digitalWrite (redled, LOW);
digitalWrite (verde, baixo);
digitalWrite (amarelado, ALTO);
}
clear_led ();
}
void GREEN_YES () // executa o código até que a luz verde esteja acesa; end cycle quando o botão de reset é pressionado
{
while (digitalRead (restpin) == 1)
{
digitalWrite (redled, LOW);
digitalWrite (verde, alto);
digitalWrite (amarelado, BAIXO);
}
clear_led ();
}
void clear_led () // todos os LED apagados
{
digitalWrite (redled, LOW);
digitalWrite (verde, baixo);
digitalWrite (amarelado, BAIXO);
}
Projeto 8: Alarme ativo
int buzzer=8;// initialize digital IO pin that controls the buzzer
void setup()
{
pinMode(buzzer,OUTPUT);// set pin mode as “output”
}
void loop()
{
digitalWrite(buzzer, HIGH); // produce sound
}
Projeto 9: Alarme passivo
int buzzer=8;// select digital IO pin for the buzzer
void setup()
{
pinMode(buzzer,OUTPUT);// set digital IO pin pattern, OUTPUT to be output
}
void loop()
{ unsigned char i,j;//define variable
while(1)
{ for(i=0;i<80;i++)// output a frequency sound
{ digitalWrite(buzzer,HIGH);// sound
delay(1);//delay1ms
digitalWrite(buzzer,LOW);//not sound
delay(1);//ms delay
}
for(i=0;i<100;i++)// output a frequency sound
{ digitalWrite(buzzer,HIGH);// sound
digitalWrite(buzzer,LOW);//not sound
delay(2);//2ms delay
}
}
}
Projeto 10: Módulo LED branco
int led = 3;
void setup()
{
pinMode(led, OUTPUT); //Set Pin3 as output
}
void loop()
{
digitalWrite(led, HIGH); //Turn on led
delay(2000);
digitalWrite(led, LOW); //Turn off led
delay(2000);
}
Projeto 11: Módulo buzzer passivo
int buzzer=8;//set digital IO pin of the buzzer
void setup()
{
pinMode(buzzer,OUTPUT);// set digital IO pin pattern, OUTPUT to be output
}
void loop()
{ unsigned char i,j;//define variable
while(1)
{ for(i=0;i<80;i++)// output a frequency sound
{ digitalWrite(buzzer,HIGH);// sound
delay(1);//delay1ms
digitalWrite(buzzer,LOW);//not sound
delay(1);//ms delay
}
for(i=0;i<100;i++)// output a frequency sound
{
digitalWrite(buzzer,HIGH);// sound
digitalWrite(buzzer,LOW);//not sound
delay(2);//2ms delay
}
}
}
Projeto 12: Módulo sensor de choque
int Led=13;//define LED interface
int Shock=3//define knock sensor interface
;int val;//define digital variable val
void setup()
{
pinMode(Led,OUTPUT);//define LED to be output interface
pinMode(Shock,INPUT);//define knock sensor to be output interface
}
void loop()
{
val=digitalRead(Shock);//read the value of interface3 and evaluate it to val
if(val==HIGH)//when the knock sensor detect a signal, LED will be flashing
{
digitalWrite(Led,LOW);
}
else
{
digitalWrite(Led,HIGH);
}
}
Projeto 13: Módulo sensor de inclinação
int ledPin = 13; // Connect LED to pin 13
int switcher = 3; // Connect Tilt sensor to Pin3
void setup()
{
pinMode(ledPin, OUTPUT); // Set digital pin 13 to output mode
pinMode(switcher, INPUT); // Set digital pin 3 to input mode
}
void loop()
{
if(digitalRead(switcher)==HIGH) //Read sensor value
{
digitalWrite(ledPin, HIGH); // Turn on LED when the sensor is tilted
}
else
{
digitalWrite(ledPin, LOW); // Turn off LED when the sensor is not triggered
}
}
Projeto 14: Módulo sensor de temperatura 18B20
// http://www.pjrc.com/teensy/arduino_libraries/OneWire.zip
#include
int DS18S20_Pin = 2; //DS18S20 Signal pin on digital pin 2
//Temperature chip i/o
OneWire ds(DS18S20_Pin); // on digital pin 2
void setup(void) {
Serial.begin(9600);
}
void loop(void) {
float temperature = getTemp();
Serial.println(temperature);
delay(100); //to slow down the output so it is easier to read
}
float getTemp(){
//returns the temperature from one DS18S20 in DEG Celsius
byte data[12];
byte addr[8];
if ( !ds.search(addr)) {
//no more sensors on chain, reset search
ds.reset_search();
return -1000;
}
if ( OneWire::crc8( addr, 7) != addr[7]) {
Serial.println("CRC is not valid!");
return -1000;
}
if ( addr[0] != 0x10 && addr[0] != 0x28) {
Serial.print("Device is not recognized");
return -1000;
}
ds.reset();
ds.select(addr);
ds.write(0x44,1); // start conversion, with parasite power on at the end
byte present = ds.reset();
ds.select(addr);
ds.write(0xBE); // Read Scratchpad
for (int i = 0; i < 9; i++) { // we need 9 bytes
data[i] = ds.read();
}
ds.reset_search();
byte MSB = data[1];
byte LSB = data[0];
float tempRead = ((MSB << 8) | LSB); //using two's compliment
float TemperatureSum = tempRead / 16;
return TemperatureSum;
}
Projeto 15: Módulo recetor infravermelho
#include
int RECV_PIN = 11;
IRrecv irrecv(RECV_PIN);
decode_results results;
void setup()
{
Serial.begin(9600);
irrecv.enableIRIn(); // Start the receiver
}
void loop() {
if (irrecv.decode(&results)) {
Serial.println(results.value, HEX);
irrecv.resume(); // Receive the next value
}
}
Projeto 16: Módulo transmissor infravermelho
int led = 3;
void setup() {
pinMode(led, OUTPUT);
}
void loop() {
digitalWrite(led, HIGH);
delay(1000);
digitalWrite(led, LOW);
delay(1000); }
Projeto 17: Módulo sensor de toque
int ledPin = 13; // Connect LED on pin 13, or use the onboard one
int KEY = 2; // Connect Touch sensor on Digital Pin 2
void setup(){
pinMode(ledPin, OUTPUT); // Set ledPin to output mode
pinMode(KEY, INPUT); //Set touch sensor pin to input mode
}
void loop(){
if(digitalRead(KEY)==HIGH) { //Read Touch sensor signal
digitalWrite(ledPin, HIGH); // if Touch sensor is HIGH, then turn on
}
else{
digitalWrite(ledPin, LOW); // if Touch sensor is LOW, then turn off the led
}
}
Projeto 18: Módulo sensor de álcool M3
///Arduino Sample Code
void setup()
{
Serial.begin(9600); //Set serial baud rate to 9600 bps
}
void loop()
{
int val;
val=analogRead(0);//Read Gas value from analog 0
Serial.println(val,DEC);//Print the value to serial port
delay(100);
}
Projeto 19: Módulo sensor de contorno de obstáculos
const int sensorPin = 2; // the number of the sensor pin
const int ledPin = 13; // the number of the LED pin
int sensorState = 0; // variable for reading the sensor status
void setup() {
pinMode(ledPin, OUTPUT);
pinMode(sensorPin, INPUT); }
void loop(){
// read the state of the sensor value:
sensorState = digitalRead(sensorPin);
// if it is, the sensorState is HIGH:
if (sensorState == HIGH) {
digitalWrite(ledPin, HIGH);
}
else {
digitalWrite(ledPin, LOW);
}
}
Não há comentários ainda.