Skip to content
Browse files

Add a speed control pot connected to ADC0

  • Loading branch information...
1 parent 2f7fbfb commit 24859cd8181bcbb1a30b8298074b9975ae98512d @karlp committed Feb 14, 2011
Showing with 45 additions and 14 deletions.
  1. +45 −14 hearts.c
View
59 hearts.c
@@ -16,25 +16,56 @@
#define HEART_OFF(x) (PORTB &= ~(1<<(x)))
#define HEART_CONFIG (DDRB |= (1<<HEART1) | (1<<HEART2) | (1<<HEART3))
+#define ADC_ENABLE (ADCSRA |= (1<<ADEN))
+#define ADC_DISABLE (ADCSRA &= ~(1<<ADEN))
+#define VREF_VCC 0
+
+unsigned int delay = 100;
+
+void init_adc_regular(uint8_t muxBits) {
+ ADMUX = VREF_VCC | muxBits;
+}
+
+unsigned int adc_read(void)
+{
+ ADCSRA |= (1<<ADSC); // begin the conversion
+ while (ADCSRA & (1<<ADSC)) ; // wait for the conversion to complete
+ unsigned char lsb = ADCL; // read the LSB first
+ return (ADCH << 8) | lsb; // read the MSB and return 10 bit result
+}
+
void init(void) {
- HEART_CONFIG;
- clock_prescale_set(0);
+ HEART_CONFIG;
+ clock_prescale_set(0);
+ ADC_ENABLE;
+}
+
+void play_pattern_1() {
+ HEART_ON(HEART1);
+ HEART_OFF(HEART3);
+ _delay_ms(delay);
+ HEART_ON(HEART2);
+ HEART_OFF(HEART1);
+ _delay_ms(delay);
+ HEART_OFF(HEART2);
+ HEART_ON(HEART3);
+ _delay_ms(delay);
+}
+
+// scale the adc reading to 0ms-500ms (ish)
+unsigned int read_scaled_pot() {
+ init_adc_regular(0);
+ unsigned int tmp = adc_read();
+ return tmp >> 1;
}
int main(void) {
- init();
- while (1) {
- HEART_ON(HEART1);
- HEART_OFF(HEART3);
- _delay_ms(100);
- HEART_ON(HEART2);
- HEART_OFF(HEART1);
- _delay_ms(100);
- HEART_OFF(HEART2);
- HEART_ON(HEART3);
- _delay_ms(100);
- }
+ init();
+ while (1) {
+ delay = read_scaled_pot();
+ play_pattern_1();
+ }
}

0 comments on commit 24859cd

Please sign in to comment.
Something went wrong with that request. Please try again.