Arduino project 6 Light Theremin Help

Hello, I was doing Project 6 of the Arduino Uno starter kit and the Phototransistor did not seem to pick up any signals and the Piezo would simply buzz without any change.
Is this a known issue? could it have something to do with my code? or is my wiring wrong?

int sensorValue;
int sensorLow = 1023;
int sensorHigh = 0;
const int ledPin = 13;

void setup() {
  // put your setup code here, to run once
  pinMode(ledPin, OUTPUT);
  digitalWrite(ledPin, HIGH);
  while (millis() > 5000) {
    sensorValue = analogRead(A0);

   }
  if (sensorValue > sensorValue) {
    sensorHigh = sensorValue;
  }
    {
      if (sensorValue < sensorLow) 
        sensorLow = sensorValue;
    }

      digitalWrite(ledPin, LOW);
    } 

  

   void loop() {
      sensorValue = analogRead(A0);
      int pitch =

        map(sensorValue, sensorLow, sensorHigh, 50, 4000);

      tone(8, pitch, 20);

      delay (10);
    }

Welcome to the forum, and good job with the code tags on your first post. :grinning:

The brackets in your code are in the wrong place, and the sensor is not being calibrated. You are reading the sensor for 5 seconds, but with the misplaced brackets, you don't actually assign the values to max and min.

See this correct code from the tutorial
https://create.arduino.cc/projecthub/SBR/light-theremin-c28a1d

/*
  Arduino Starter Kit example
  Project 6 - Light Theremin

  This sketch is written to accompany Project 6 in the Arduino Starter Kit

  Parts required:
  - photoresistor
  - 10 kilohm resistor
  - piezo

  created 13 Sep 2012
  by Scott Fitzgerald

  https://store.arduino.cc/genuino-starter-kit

  This example code is part of the public domain.
*/

// variable to hold sensor value
int sensorValue;
// variable to calibrate low value
int sensorLow = 1023;
// variable to calibrate high value
int sensorHigh = 0;
// LED pin
const int ledPin = 13;

void setup() {
  // Make the LED pin an output and turn it on
  pinMode(ledPin, OUTPUT);
  digitalWrite(ledPin, HIGH);

  // calibrate for the first five seconds after program runs
  while (millis() < 5000) {
    // record the maximum sensor value
    sensorValue = analogRead(A0);
    if (sensorValue > sensorHigh) {
      sensorHigh = sensorValue;
    }
    // record the minimum sensor value
    if (sensorValue < sensorLow) {
      sensorLow = sensorValue;
    }
  }
  // turn the LED off, signaling the end of the calibration period
  digitalWrite(ledPin, LOW);
}

void loop() {
  //read the input from A0 and store it in a variable
  sensorValue = analogRead(A0);

  // map the sensor values to a wide range of pitches
  int pitch = map(sensorValue, sensorLow, sensorHigh, 50, 4000);

  // play the tone for 20 ms on pin 8
  tone(8, pitch, 20);

  // wait for a moment
  delay(10);
}

Thanks for the Assistance!

This worked! Thank you very much!

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.