Arduino Atelie Hacker

Sirene para bicicleta é otimo para a segurança do ciclista, pois quando se pedala na rua muita vezes se tem a necessida de avisar: “Olha eu estou aqui!”. Seja para um carro, ônibus ou mesmo pedestre, temos que ficar alerta e nos fazer visíveis. Com esse projeto você pode construir uma sirene para sua bike com um Arduino. Esse protótipo conta com uma buzzer de 12V com 3 toques diferente e leds que piscam na frequência do audio.

Materiais

1 Arduino
2 LEDs
3 Pushbutton
3 Resistores 10K Ohm
1 Resistores 2.2K Ohm
2 Resistores 220 Ohm
1 Transistor NPN 2222A
1 Bateria 9V
1 Soquete de bateria 9V

Neste video o circuito usava um buzzer de 3V. Posteriormente foi implementado um buzzer 12V utilizando um transistor NPN 2222A, conforme pode ser visto no diagrama abaixo. Assim o som fica mais alto, tornando mais seguro em certas situações onde o volume é mais crítico.

Circuito

Sirene-de-Bike_cc

Código Arduino

/*
Siren

A configurable siren for Arduino, requires an 8-ohm speaker attached to
pin 8 and ground

//Copyright (c) 2012 Jeremy Fonte
//This code is released under the MIT license
//http://opensource.org/licenses/MIT
*/

int pitchLow = 800; //200
int pitchHigh = 1600; //1000
int pitchStep = 10;
int currentPitch;

const int speakerPin = 8;

const int button1Pin = 2; // the number of the pushbutton pin
const int button2Pin = 3;
const int button3Pin = 4;

const int led1Pin = 13;
const int led2Pin = 12;

// variables will change:
int button1State = 0; // variable for reading the pushbutton status
int button2State = 0;
int button3State = 0;

///LED
// Variables will change:
int ledState = LOW; // ledState used to set the LED
long previousMillis = 0; // will store last time LED was updated

// the follow variables is a long because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
//long interval = 500;

///Music
#include "pitches.h"

void playArp(int notes[]);

// available chord to arpeggiate
//C major
int c[] = {
NOTE_C4, NOTE_E4,NOTE_G4, NOTE_C5, NOTE_E5};

//D minor
int d[] = {
NOTE_D4, NOTE_F4,NOTE_A4, NOTE_D5, NOTE_F5};

//E minor
int e[] = {
NOTE_E4, NOTE_G4,NOTE_B4, NOTE_E5, NOTE_G5};

//F major
int f[] = {
NOTE_F4, NOTE_A4,NOTE_C5, NOTE_F5, NOTE_A5};

//G major
int g[] = {
NOTE_G4, NOTE_B4,NOTE_D5, NOTE_G5, NOTE_B5};

//A minor
int a[] = {
NOTE_A4, NOTE_C5,NOTE_E5, NOTE_A5, NOTE_C6};

//B diminished
int b[] = {
NOTE_B4, NOTE_D5,NOTE_F5, NOTE_B5, NOTE_D6};

void setup() {
currentPitch = pitchLow;

// initialize the LED pin as an output:
pinMode(led1Pin, OUTPUT);
pinMode(led2Pin, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(button1Pin, INPUT);
pinMode(button2Pin, INPUT);
pinMode(button3Pin, INPUT);

}

void loop() {

// read the state of the pushbutton value:
button1State = digitalRead(button1Pin);
button2State = digitalRead(button2Pin);
button3State = digitalRead(button3Pin);

// check if the pushbutton is pressed.
// if it is, the buttonState is HIGH:
if (button1State == HIGH) {
sirene(0);
piscaled(40);

} else if (button2State == HIGH) {
sirene(1);
piscaled(100);

} else if (button3State == HIGH) {
// turn LED on:
//digitalWrite(ledPin, HIGH);
//delayTime = 3 ;
//sirene();
piscaled(250);
playArp(b);
//playArp(c);
//playArp(b);
//playArp(a);
//playArp(e);

} else {
// turn LED off:
digitalWrite(led1Pin, LOW);
digitalWrite(led2Pin, LOW);
noTone(8);
}

}

int sirene (int delayTime){

tone(speakerPin, currentPitch, 10);
currentPitch += pitchStep;
if(currentPitch >= pitchHigh) {
pitchStep = -pitchStep;
}
else if(currentPitch <= pitchLow) { pitchStep = -pitchStep; } delay(delayTime); } int piscaled (long velocidade){ // here is where you'd put code that needs to be running all the time. // check to see if it's time to blink the LED; that is, if the // difference between the current time and last time you blinked // the LED is bigger than the interval at which you want to // blink the LED. unsigned long currentMillis = millis(); if(currentMillis - previousMillis > velocidade) {
// save the last time you blinked the LED
previousMillis = currentMillis;

// if the LED is off turn it on and vice-versa:
if (ledState == LOW)
ledState = HIGH;
else
ledState = LOW;

// set the LED with the ledState of the variable:
digitalWrite(led1Pin, ledState);
digitalWrite(led2Pin, !ledState);
}
}

void playArp(int notes[]) {
// iterate over the notes of the melody:
for (int thisNote = 0; thisNote <= 3; thisNote++) { tone(8, notes[thisNote],100); // to distinguish the notes, set a minimum time between them. // the note's duration + 30% seems to work well: int pauseBetweenNotes = 100; delay(pauseBetweenNotes); // stop the tone playing: noTone(8); } for(int thisNote = 4; thisNote >= 1; thisNote--) {

tone(8, notes[thisNote],100);

// to distinguish the notes, set a minimum time between them.
// the note's duration + 30% seems to work well:
int pauseBetweenNotes = 100;
delay(pauseBetweenNotes);

}
}