Unit 6: Tracking Sound Changes

 

 

Code for Sketch One: 


const int RED_PIN = 11;     // Red pin for RGB LED
const int GREEN_PIN = 10;   // Green pin for RGB LED
const int BLUE_PIN = 9;     // Blue pin for RGB LED
const int BUZZER_PIN = 8;   // Pin for the active buzzer
const int POT_PIN = A0;     // Analog pin for potentiometer

void setup() {
  // Initialize the RGB LED pins as outputs
  pinMode(RED_PIN, OUTPUT);
  pinMode(GREEN_PIN, OUTPUT);
  pinMode(BLUE_PIN, OUTPUT);

  // Initialize the buzzer pin as an output
  pinMode(BUZZER_PIN, OUTPUT);
}

void loop() {
  // Read the potentiometer value
  int potValue = analogRead(POT_PIN);

  // Map the potentiometer value to RGB and pause duration parameters
  int redValue = map(potValue, 0, 1023, 0, 255);
  int greenValue = map(potValue, 0, 1023, 255, 0);
  int pauseDuration = map(potValue, 0, 1023, 2000, 100); // Decrease pause duration as potValue increases

  // Set the RGB LED color
  analogWrite(RED_PIN, redValue);
  analogWrite(GREEN_PIN, greenValue);
  analogWrite(BLUE_PIN, 0); // Keeping blue off for simplicity

  // If the potentiometer is in the ‘red’ state, beep continuously
  if (redValue >= 255) {
    tone(BUZZER_PIN, 494); // Continuous beep at frequency 494 Hz
  } else {
    // Play the buzzer tone (fixed pitch B note, frequency 494 Hz)
    tone(BUZZER_PIN, 494, 500); // Beep for 500ms
    delay(500);                 // Beep duration
    delay(pauseDuration);      // Pause duration based on potValue
  }
}

  

Code for Sketch Two:


const int RED_PIN = 11, GREEN_PIN = 10, BLUE_PIN = 9, BUZZER_PIN = 8, POT_PIN = A0;

int randomThreshold; // Random threshold for red state

void setup() {
  // Initialize the RGB LED pins as outputs
  pinMode(RED_PIN, OUTPUT);
  pinMode(GREEN_PIN, OUTPUT);
  pinMode(BLUE_PIN, OUTPUT);

  // Initialize the buzzer pin as an output
  pinMode(BUZZER_PIN, OUTPUT);

  // Seed the random number generator
  randomSeed(analogRead(A1));

  // Set an initial random threshold for the red state
  randomThreshold = random(512, 1023);
}

void loop() {
  // Read the potentiometer value
  int potValue = analogRead(POT_PIN);

  // Map the potentiometer value to RGB and pause duration parameters
  int redValue = map(potValue, 0, 1023, 0, 255);
  int greenValue = map(potValue, 0, 1023, 255, 0);
  int pauseDuration = map(potValue, 0, 1023, 2000, 100); // Decrease pause duration as potValue increases

  // Set the RGB LED color
  analogWrite(RED_PIN, redValue);
  analogWrite(GREEN_PIN, greenValue);
  analogWrite(BLUE_PIN, 0); // Keeping blue off for simplicity

  // Check if the potentiometer value exceeds the random threshold
  if (potValue >= randomThreshold) {
    // If the threshold is exceeded, set the red state and beep continuously
    analogWrite(RED_PIN, 255);
    analogWrite(GREEN_PIN, 0);
    analogWrite(BLUE_PIN, 0);
    tone(BUZZER_PIN, 494); // Continuous beep at frequency 494 Hz

    // Reset the random threshold to a new value
    randomThreshold = random(512, 1023);
  } else {
    // Play the buzzer tone (fixed pitch B note, frequency 494 Hz)
    tone(BUZZER_PIN, 494, 500); // Beep for 500ms
    delay(500);                 // Beep duration
    delay(pauseDuration);      // Pause duration based on potValue
  }
}

    

Notes: