Saturday, December 10, 2016

Arduino conversion.

Using the Arduino development boards can be cumbersome and a bit more expensive. But for simple projects, a standalone Arduino chip just just the right choice. We will convert a typical circuit for a board to a standalone chip setup.



int piezoPin = 8; // Declaring Piezo Buzzer on Pin 8
int ldrPin = 0; // Declaring LDR on Analog Pin 0
int ldrValue = 0; // Reading different values from the LDR
void setup()
{ 
}
void loop() 
{ // Starting the cycle functions below
   ldrValue = analogRead(ldrPin); // read the value from the LDR
   tone(piezoPin,1000); // Play a 1000Hz tone from the piezo (beep)
   delay(25); // wait a bit, change the delay for fast response.
   noTone(piezoPin); // stop the tone after 25 ms in this case 
   delay(ldrValue); // wait the amount of milliseconds in ldrValue
} // End of cycle functions 
 
 


Or

 


// variable declaration
// sensor value
int value;
// low sensor value calibration
int low;
// high sensor value calibration
int high;
// digital ouput pin where the buzzer is connected to
int buzpin = 8;
// LED pin alert calibration
int led = 13;

void setup() {
pinMode (buzpin, OUTPUT);
pinMode (led, OUTPUT);
// turn LED on
digitalWrite(led, HIGH);
// calibration for the first 3 seconds after program runs
while (millis() < 3000) {
// record the maximum sensor value
value = analogRead(A0);
if (value > high) {
high = value;
}
// record the minimum sensor value
if (value < low) {
low = value;
}
}
// turn LED off
digitalWrite (led, LOW);
}

void loop() {
//read the input from A0 and store it in a variable
value = analogRead(A0);
// map the sensor values to a frequency range 50 Hz - 4000 Hz
int pitch = map(value, low, high, 50, 4000);
// play the tone for 20 ms on buzzer pin 8
tone(8, pitch, 20);
// wait for a moment
delay(10);
}

No comments:

Post a Comment