User:Yoana Buzova/mrSTOCK2: Difference between revisions
Yoana Buzova (talk | contribs) No edit summary |
Yoana Buzova (talk | contribs) No edit summary |
||
(7 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
===intro=== | |||
Idea to transform light into sound ---> therefore be able to make self-generated sound of different brightness patterns | Idea to transform light into sound ---> therefore be able to make self-generated sound of different brightness patterns | ||
(initially we wanted to transform an old slide projector (thank you for the donation Roel) into a film loop machine, with a self-generated sound | (initially we wanted to transform an old slide projector (thank you for the donation Roel) into a film loop machine, with a self-generated sound | ||
Line 12: | Line 13: | ||
DC motor speed control with IR sensor | ===DC motor speed control with IR sensor=== | ||
code: | code: | ||
Line 51: | Line 52: | ||
8 LDRs making some noise 1st code | ===8 LDRs making some noise 1st code=== | ||
[[File:Nano_ldr_sound.jpg|500px]] | |||
code: | |||
int myInts[8]; | |||
const int myPins[] = {A0,A1,A2,A3,A4,A5,A6,A7}; | |||
int mySensVals[8]; | |||
int myMaxValues[8]; | |||
int myMinValues[8]; | |||
const int speakerPin = 9; | |||
void setup() | |||
{ | |||
Serial.begin(115200); | |||
// using those pins as output | |||
pinMode(speakerPin, OUTPUT); | |||
for (int n = 0; n < 8; n++) | |||
{ | |||
pinMode(myPins[n], INPUT); | |||
digitalWrite(myPins[n], HIGH); | |||
myMinValues[n] = 1023; | |||
myMaxValues[n] = 0; | |||
} | |||
} | |||
void readSensor(int n) | |||
{ | |||
int val = analogRead(myPins[n]); | |||
if (val > myMaxValues[n]) | |||
{ | |||
myMaxValues[n] = val; | |||
} | |||
if (val < myMinValues[n]) | |||
{ | |||
myMinValues[n] = val; | |||
} | |||
mySensVals[n] = map(val, myMinValues[n], myMaxValues[n], 0, 255); | |||
} | |||
void readSensors(void) | |||
{ | |||
for (int n = 0; n < 8; n++) | |||
{ | |||
readSensor(n); | |||
} | |||
} | |||
void printSensors(void) | |||
{ | |||
for (int n = 0; n < 8; n++) | |||
{ | |||
Serial.print(n, DEC); | |||
Serial.print(": "); | |||
Serial.print(mySensVals[n], DEC); | |||
Serial.print(", "); | |||
} | |||
Serial.println(); | |||
} | |||
void loop() | |||
{ | |||
readSensors(); | |||
printSensors(); | |||
} | |||
void makeTone(int freq){ | |||
digitalWrite(speakerPin, HIGH); | |||
delayMicroseconds(freq); | |||
digitalWrite(speakerPin, LOW); | |||
delayMicroseconds(freq); | |||
} | |||
===8 LDRs making some noise with tones=== | |||
[[File:Nano_ldr_sound3.png|500px]] | |||
code: | |||
include <stdint.h> | |||
include <avr/io.h> | |||
include <avr/interrupt.h> | |||
static uint8_t pin_bv[2]; volatile uint8_t * port[2]; volatile uint8_t * ddr[2]; | |||
static volatile uint16_t interval[2]; | |||
void setup_tone_pins(uint8_t pin_a, uint8_t pin_b) { // setup output pins if (pin_a < 8) { pin_bv[0] = _BV(pin_a); port[0] = &PORTD; ddr[0] = &DDRD; } else if (pin_a < 14) { pin_bv[0] = _BV(pin_a - 8); port[0] = &PORTB; ddr[0] = &DDRB; } | |||
if (pin_b < 8) { pin_bv[1] = _BV(pin_b); port[1] = &PORTD; ddr[1] = &DDRD; } else if (pin_b < 14) { pin_bv[1] = _BV(pin_b - 8); port[1] = &PORTB; ddr[1] = &DDRB; } | |||
* port[0] &= (uint8_t)~pin_bv[0]; * port[1] &= (uint8_t)~pin_bv[1]; * ddr[0] |= pin_bv[0]; * ddr[1] |= pin_bv[1]; interval[0] = 0; interval[1] = 0; | |||
// setup Timer 1 TCCR1A = 0; TCCR1B = _BV(CS11); // prescaler at F_CPU / 8 = 2MHz TIMSK1 = _BV(OCIE1B) | _BV(OCIE1A); // enable both OCR interrupts } | |||
// Interrupt Service Routines ISR(TIMER1_COMPA_vect) { if (interval[0]) { * port[0] ^= pin_bv[0]; // toggle output pin polarity OCR1A = TCNT1 + interval[0]; // schedule next interrupt } else { * port[0] &= (uint8_t)~pin_bv[0]; // set output pin low } } | |||
ISR(TIMER1_COMPB_vect) { if (interval[1]) { * port[1] ^= pin_bv[1]; // toggle output pin polarity OCR1B = TCNT1 + interval[1]; // schedule next interrupt } else { * port[1] &= (uint8_t)~pin_bv[1]; // set output pin low } } | |||
void tone_a(uint16_t freq) { // calculate interval from given freq if ((freq > 15) && (freq < 62500)) { interval[0] = 1000000L / freq; OCR1A = TCNT1 + interval[0]; } else { interval[0] = 0; } } | |||
void tone_b(uint16_t freq) { // calculate interval from given freq if ((freq > 15) && (freq < 62500)) { interval[1] = 1000000L / freq; OCR1B = TCNT1 + interval[0]; } else { interval[1] = 0; } } | |||
int myInts[8]; const int myPins[] = {A0,A1,A2,A3,A4,A5,A6,A7}; int mySensVals[8]; int myMaxValues[8]; int myMinValues[8]; | |||
const int speakerPin = 9; | const int speakerPin = 9; | ||
const int tones[] = {33, 37, 41, 46, 49, 55, 61}; | |||
void setup() { | void setup() { | ||
setup_tone_pins(9, 10); | |||
// configure Timer2 for HIGH FREQUENCY PWM (62.5kHz) on pins 3 and 11 | |||
pinMode(3, OUTPUT); | |||
pinMode(11, OUTPUT); | |||
TCCR2B &= (uint8_t)~(_BV(CS20) | _BV(CS21) | _BV(CS22)); | |||
TCCR2B |= _BV(CS20); // run Timer2 at 16MHz | |||
Serial.begin(115200); | Serial.begin(115200); | ||
// using those pins as output | // using those pins as output | ||
pinMode(speakerPin, OUTPUT); | // pinMode(speakerPin, OUTPUT); | ||
for (int n = 0; n < 8; n++) | for (int n = 0; n < 8; n++) | ||
Line 84: | Line 199: | ||
myMinValues[n] = val; | myMinValues[n] = val; | ||
} | } | ||
mySensVals[n] = map(val, myMinValues[n], myMaxValues[n], 0, 255); | mySensVals[n] = map(val, myMinValues[n], myMaxValues[n], 0, 255); | ||
} | } | ||
Line 106: | Line 220: | ||
} | } | ||
Serial.println(); | Serial.println(); | ||
} | |||
int calc_freq(int first_sens, int num_sens) { | |||
int n, val, tone_nr, freq; | |||
//int oct = 0; | |||
int avg = 0; | |||
int count = 0; | |||
for (int i = 0; i < num_sens; i++) | |||
{ | |||
n = first_sens + i; | |||
val = mySensVals[n]; | |||
if (val > 16) | |||
{ | |||
// oct += 1 << i; | |||
avg += val; | |||
count++; | |||
} | |||
} | |||
if (count > 0) | |||
{ | |||
avg = avg / count; | |||
tone_nr = map(avg, 0, 255, 0, 5); | |||
// freq = tones[tone_nr] * (1 << oct); | |||
freq = tones[tone_nr] * (1 << count); | |||
return freq; | |||
} else { | |||
return 0; | |||
} | |||
} | } | ||
Line 111: | Line 260: | ||
readSensors(); | readSensors(); | ||
printSensors(); | |||
// set frequency on pin 9ß | |||
uint16_t freq = calc_freq(0, 3); | |||
tone_a(freq); | |||
// set volume on pin 3 | |||
if (freq > 15) | |||
{ | |||
analogWrite(3,255-mySensVals[3]); | |||
} else { | |||
analogWrite(3, 255); | |||
} | |||
// set frequency on pin 10 | |||
freq = calc_freq(4, 3); | |||
tone_b(freq); | |||
// set volume on pin 11 | |||
if (freq > 15) | |||
{ | |||
analogWrite(11,255-mySensVals[7]); | |||
} else { | |||
analogWrite(11, 255); | |||
} | |||
delay(133); | |||
} | } | ||
void makeTone(int freq){ | //void makeTone(int freq){ | ||
// digitalWrite(speakerPin, HIGH); | |||
//delayMicroseconds(freq); | |||
// digitalWrite(speakerPin, LOW); | |||
// delayMicroseconds(freq); | |||
//} | |||
=== Amplifying speaker with transistors === | |||
[[File:Amplifiy_with_2transistors.JPG|500px]] [[File:Amplify_with_two_transistors.jpg|500px]] | |||
===Mr. Stock explaining how to connect ldr without a resistor, using the internal chip resistors === | |||
[[File:Chip_internal_resistors.JPG|500px]] | |||
Latest revision as of 21:18, 16 March 2013
intro
Idea to transform light into sound ---> therefore be able to make self-generated sound of different brightness patterns (initially we wanted to transform an old slide projector (thank you for the donation Roel) into a film loop machine, with a self-generated sound this is the slide projector..or what is left of it now.....we did not have time to think of the mechanical mechanism during the workshop, due to to very big struggle with electronics, due to lack of knowledge! next to it is the schematics of the modification
DC motor speed control with IR sensor
code:
int transistorPin = 9; // motor connected to digital pin 9 int sensor = 3; // sensor connected to analog pin 3 int sensorValue = 0; // variable to store the read value void setup() { Serial.begin (115200); pinMode(transistorPin, OUTPUT); // sets the pin as output } void loop() { sensorValue = analogRead(sensor); // read the input pin sensorValue = map(sensorValue, 0, 1023, 100, 255); //map values analogWrite(transistorPin, sensorValue); Serial.println(sensorValue); }
8 LDRs making some noise 1st code
code:
int myInts[8]; const int myPins[] = {A0,A1,A2,A3,A4,A5,A6,A7}; int mySensVals[8]; int myMaxValues[8]; int myMinValues[8];
const int speakerPin = 9;
void setup() { Serial.begin(115200); // using those pins as output pinMode(speakerPin, OUTPUT); for (int n = 0; n < 8; n++) { pinMode(myPins[n], INPUT); digitalWrite(myPins[n], HIGH); myMinValues[n] = 1023; myMaxValues[n] = 0; } }
void readSensor(int n) { int val = analogRead(myPins[n]); if (val > myMaxValues[n]) { myMaxValues[n] = val; } if (val < myMinValues[n]) { myMinValues[n] = val; } mySensVals[n] = map(val, myMinValues[n], myMaxValues[n], 0, 255); }
void readSensors(void) { for (int n = 0; n < 8; n++) { readSensor(n); } }
void printSensors(void) { for (int n = 0; n < 8; n++) { Serial.print(n, DEC); Serial.print(": "); Serial.print(mySensVals[n], DEC); Serial.print(", "); } Serial.println(); }
void loop() { readSensors(); printSensors(); }
void makeTone(int freq){ digitalWrite(speakerPin, HIGH); delayMicroseconds(freq); digitalWrite(speakerPin, LOW); delayMicroseconds(freq); }
8 LDRs making some noise with tones
code:
include <stdint.h> include <avr/io.h> include <avr/interrupt.h>
static uint8_t pin_bv[2]; volatile uint8_t * port[2]; volatile uint8_t * ddr[2];
static volatile uint16_t interval[2];
void setup_tone_pins(uint8_t pin_a, uint8_t pin_b) { // setup output pins if (pin_a < 8) { pin_bv[0] = _BV(pin_a); port[0] = &PORTD; ddr[0] = &DDRD; } else if (pin_a < 14) { pin_bv[0] = _BV(pin_a - 8); port[0] = &PORTB; ddr[0] = &DDRB; }
if (pin_b < 8) { pin_bv[1] = _BV(pin_b); port[1] = &PORTD; ddr[1] = &DDRD; } else if (pin_b < 14) { pin_bv[1] = _BV(pin_b - 8); port[1] = &PORTB; ddr[1] = &DDRB; }
* port[0] &= (uint8_t)~pin_bv[0]; * port[1] &= (uint8_t)~pin_bv[1]; * ddr[0] |= pin_bv[0]; * ddr[1] |= pin_bv[1]; interval[0] = 0; interval[1] = 0;
// setup Timer 1 TCCR1A = 0; TCCR1B = _BV(CS11); // prescaler at F_CPU / 8 = 2MHz TIMSK1 = _BV(OCIE1B) | _BV(OCIE1A); // enable both OCR interrupts } // Interrupt Service Routines ISR(TIMER1_COMPA_vect) { if (interval[0]) { * port[0] ^= pin_bv[0]; // toggle output pin polarity OCR1A = TCNT1 + interval[0]; // schedule next interrupt } else { * port[0] &= (uint8_t)~pin_bv[0]; // set output pin low } }
ISR(TIMER1_COMPB_vect) { if (interval[1]) { * port[1] ^= pin_bv[1]; // toggle output pin polarity OCR1B = TCNT1 + interval[1]; // schedule next interrupt } else { * port[1] &= (uint8_t)~pin_bv[1]; // set output pin low } }
void tone_a(uint16_t freq) { // calculate interval from given freq if ((freq > 15) && (freq < 62500)) { interval[0] = 1000000L / freq; OCR1A = TCNT1 + interval[0]; } else { interval[0] = 0; } }
void tone_b(uint16_t freq) { // calculate interval from given freq if ((freq > 15) && (freq < 62500)) { interval[1] = 1000000L / freq; OCR1B = TCNT1 + interval[0]; } else { interval[1] = 0; } }
int myInts[8]; const int myPins[] = {A0,A1,A2,A3,A4,A5,A6,A7}; int mySensVals[8]; int myMaxValues[8]; int myMinValues[8];
const int speakerPin = 9;
const int tones[] = {33, 37, 41, 46, 49, 55, 61};
void setup() {
setup_tone_pins(9, 10); // configure Timer2 for HIGH FREQUENCY PWM (62.5kHz) on pins 3 and 11 pinMode(3, OUTPUT); pinMode(11, OUTPUT); TCCR2B &= (uint8_t)~(_BV(CS20) | _BV(CS21) | _BV(CS22)); TCCR2B |= _BV(CS20); // run Timer2 at 16MHz Serial.begin(115200); // using those pins as output // pinMode(speakerPin, OUTPUT);
for (int n = 0; n < 8; n++) { pinMode(myPins[n], INPUT); digitalWrite(myPins[n], HIGH); myMinValues[n] = 1023; myMaxValues[n] = 0; } }
void readSensor(int n) {
int val = analogRead(myPins[n]); if (val > myMaxValues[n]) { myMaxValues[n] = val; } if (val < myMinValues[n]) { myMinValues[n] = val; } mySensVals[n] = map(val, myMinValues[n], myMaxValues[n], 0, 255); }
void readSensors(void) {
for (int n = 0; n < 8; n++) { readSensor(n); } }
void printSensors(void) {
for (int n = 0; n < 8; n++) { Serial.print(n, DEC); Serial.print(": "); Serial.print(mySensVals[n], DEC); Serial.print(", "); } Serial.println(); }
int calc_freq(int first_sens, int num_sens) {
int n, val, tone_nr, freq; //int oct = 0; int avg = 0; int count = 0; for (int i = 0; i < num_sens; i++) { n = first_sens + i; val = mySensVals[n]; if (val > 16) { // oct += 1 << i;
avg += val; count++; } } if (count > 0) { avg = avg / count; tone_nr = map(avg, 0, 255, 0, 5); // freq = tones[tone_nr] * (1 << oct);
freq = tones[tone_nr] * (1 << count); return freq; } else { return 0; } }
void loop() {
readSensors(); printSensors(); // set frequency on pin 9ß uint16_t freq = calc_freq(0, 3); tone_a(freq); // set volume on pin 3 if (freq > 15) { analogWrite(3,255-mySensVals[3]); } else { analogWrite(3, 255); } // set frequency on pin 10 freq = calc_freq(4, 3); tone_b(freq); // set volume on pin 11 if (freq > 15) { analogWrite(11,255-mySensVals[7]); } else { analogWrite(11, 255); } delay(133);
}
//void makeTone(int freq){
// digitalWrite(speakerPin, HIGH); //delayMicroseconds(freq); // digitalWrite(speakerPin, LOW); // delayMicroseconds(freq); //}