PiezoToLED

Wiring

Schematics for PiezoToLED
Notes

  • LED resistances are 220 ohms
  • Piezos resistances are 1M ohm

Code

Download

				
	// Setting up LED pins
	const int redLED = 9;
	const int orangeLED = 6;
	const int greenLED = 3;

	// Setting up piezos pins
	const int redPiezo = A0;
	const int orangePiezo = A2;
	const int greenPiezo = A4;

	// Setting up piezos sensitivity
	const int threshold = 50;

	// Variables for debugging
	int redReading = 0;
	int orangeReading = 0;
	int greenReading = 0;

	// the setup function runs once when you press reset or power the board
	void setup() {
	  // initialize serial port for debugging
	  Serial.begin(9600);
	  Serial.print("Initializing program.");

	  // initialize digital pin LED_BUILTIN as an output.
	  pinMode(redLED, OUTPUT);
	  pinMode(orangeLED, OUTPUT);
	  pinMode(greenLED, OUTPUT);
	  Serial.print("LED pins initialized.");
	}

	// the loop function runs over and over again forever
	void loop() {
	  redReading = analogRead(redPiezo);
	  orangeReading = analogRead(orangePiezo);
	  greenReading = analogRead(greenPiezo);

	  Serial.print("Red sensor: ");
	  Serial.println(redReading);
	  Serial.print("Orange sensor: ");
	  Serial.println(orangeReading);
	  Serial.print("Green sensor: ");
	  Serial.println(greenReading);

	  if ( redReading >= threshold) {
	    Serial.print("Triggering red LED.");
	    digitalWrite(redLED, HIGH);
	  } else {
	    digitalWrite(redLED, LOW);
	  }

	  if ( orangeReading >= threshold) {
	    Serial.print("Triggering orange LED.");
	    digitalWrite(orangeLED, HIGH);
	  } else {
	    digitalWrite(orangeLED, LOW);
	  }

	  if ( greenReading >= threshold) {
	    Serial.print("Triggering green LED.");
	    digitalWrite(greenLED, HIGH);
	  } else {
	    digitalWrite(greenLED, LOW);
	  }
	}