Monday, 17 November 2014

ATTiny85 – Tutorial 11 – Light Input Controlling Sound Output

This is the final Freetronics 11 – Experimenters Kit tutorial. The other 10 tutorials have been covered and, I’m please to say, I have now completed all 11 tutorials with the ATTiny85.

This tutorial makes use of the Freetronics Light module (a light detecting resistor or LDR) with the Freetronics Sound module (a piezo electric module). The light level is read by the analog LDR and the light level value is passed as a tone to the piezo.

Once again, we are using a web sourced tone function that makes use of the ATTiny85 timers to produce an output tone value. This time, however, we are not using predefined wave cycle values, but using analog light level values to drive the tone.

ATTiny85 Pin - Lesson 11

The Piezo is connected on pin 1 (ATTiny85 pin 6), while the LDR is connected on A2 (ATTiny85 pin 3).

Tutorial11

The LDR also connects VCC to the positive rail and GND to the negative rail.

The modified sketch:

/* Tutorial 11: Light Input Controlling Sound Output */

int piezo = 1;
int lightLevel = 0;
int duration = 300;

void setup()
{
  pinMode(piezo, OUTPUT);
}

void loop()
{
  lightLevel = analogRead(A2);
  TinyTone(lightLevel, 4, duration);
}

void TinyTone(unsigned char divisor, unsigned char octave, unsigned long duration)
{
  TCCR1 = 0x90 | (8-octave); // for 1MHz clock
  OCR1C = divisor-1;         // set the OCR
  delay(duration);
  TCCR1 = 0x90;              // stop the counter

}

I believe that TCCR1 and OCR1C both point to pin 1 (but I’d have to research that a little to make sure). Reason tells me that this is so … reason and I are not always particularly good friends.

It would be nice to reduce the delay so that the tones are more smooth, it would be kinda like a light driven theramin.

Check out the rest of the tutorials here.

No comments:

Post a Comment

Paypal Donations

Donations to help me to keep up the lunacy are greatly appreciated, but NOT mandatory.