Arduino Atelie Hacker

Sounder bike is perfect to the safety of the cyclist, because when you ride on the street a lot of times you have the Necessity of warning: “Look, I’m here!”. Whether for a car, bus or even pedestrian, we have to stay alert and make us visible. With this project you can build a siren to your bike with an Arduino. This prototype has a 12V buzzer with 3 different tones and LEDs that flash in the audio frequency.

Materials
1 Arduino
2 LEDs
3 Pushbutton
3 Resistors 10K Ohm
1 Resistors 2.2K Ohm
2 Resistors 220 Ohm
1 NPN 2222A
1 9V battery
1 9V battery socket

In this video the circuit used a buzzer 3V. Thereafter it was implemented using a buzzer 12V NPN transistor 2222A, as can be seen in the diagram below. So the sound gets louder, making it safe in certain situations where the volume is more critical.

Circuit
Sirene-de-Bike_cc

Arduino Code

/*
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);

}
}