I have to combine these two code together.
microphone to LED value code:
#include <inttypes.h>
#include <avr/io.h>
#include <util/delay.h>
#define LED_BIT PD4
/*
* get_adc
* Return the 10bit value of the selected adc channel.
*/
uint16_t get_adc() {
uint16_t value;
// warm up the ADC, discard the first conversion
ADCSRA |= (1 << ADSC);
while (ADCSRA & (1 << ADSC));
value = ADCW;
ADCSRA |= (1 << ADSC); // start single conversion
while (ADCSRA & (1 << ADSC)); // wait until conversion is done
return ADCW;
}
int main(void) {
uint8_t i = 0;
DDRD |= 0x1c; // PD2-PD3: col 6-7, PD4: debug LED
// select channel
ADMUX = 5;
// ADC setup
ADCSRA =
(1 << ADEN) | // enable ADC
(1 << ADPS1) | (1 << ADPS0); // set prescaler to 8
// say hello
for (i = 0; i < 5; i++) {
PORTD |= (1 << LED_BIT);
_delay_ms(10);
_delay_ms(10);
_delay_ms(10);
_delay_ms(10);
_delay_ms(10);
PORTD &= ~(1 << LED_BIT);
_delay_ms(10);
_delay_ms(10);
_delay_ms(10);
_delay_ms(10);
_delay_ms(10);
}
_delay_ms(10);
_delay_ms(10);
while (1) {
if (get_adc() > 180) {
PORTD |= (1 << LED_BIT);
_delay_ms(10);
PORTD &= ~(1 << LED_BIT);
}
}
return 0;
}
Shift Register code:
//Pin connected to latch pin (ST_CP) of 74HC595 const int latchPin = 8; //Pin connected to clock pin (SH_CP) of 74HC595 const int clockPin = 12; ////Pin connected to Data in (DS) of 74HC595 const int dataPin = 11; char inputString[2]; void setup() { //set pins to output because they are addressed in the main loop pinMode(latchPin, OUTPUT); pinMode(dataPin, OUTPUT); pinMode(clockPin, OUTPUT); Serial.begin(9600); Serial.println("reset"); } void loop() { // iterate over the 16 outputs of the two shift registers for (int thisLed = 0; thisLed < 16; thisLed++) { // write data to the shift registers: registerWrite(thisLed, HIGH); // if this is not the first LED, turn off the previous LED: if (thisLed > 0) { registerWrite(thisLed - 1, LOW); } // if this is the first LED, turn off the highest LED: else { registerWrite(15, LOW); } // pause between LEDs: delay(250); } } // This method sends bits to the shift registers: void registerWrite(int whichPin, int whichState) { // the bits you want to send. Use an unsigned int, // so you can use all 16 bits: unsigned int bitsToSend = 0; // turn off the output so the pins don't light up // while you're shifting bits: digitalWrite(latchPin, LOW); // turn on the next highest bit in bitsToSend: bitWrite(bitsToSend, whichPin, whichState); // break the bits into two bytes, one for // the first register and one for the second: byte registerOne = highByte(bitsToSend); byte registerTwo = lowByte(bitsToSend); // shift the bytes out: shiftOut(dataPin, clockPin, MSBFIRST, registerTwo); shiftOut(dataPin, clockPin, MSBFIRST, registerOne); // turn on the output so the LEDs can light up: digitalWrite(latchPin, HIGH); }Yu Wei Chiang