User:Yoana Buzova/mrSTOCK2

From XPUB & Lens-Based wiki

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

Slide proj.jpg Slide projector proj schematics.jpg




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);
  
 }


Ir controlled DCmotor.jpg DCmotor+ard.jpg


8 LDRs making some noise 1st code

Nano ldr sound.jpg

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

Nano ldr sound3.png

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);  
 //}

Amplifying speaker with transistors

Amplifiy with 2transistors.JPG Amplify with two transistors.jpg

Mr. Stock explaining how to connect ldr without a resistor, using the internal chip resistors

Chip internal resistors.JPG