Back to Parent

// Define a pin to place the Piezo Element
int piezoPin = A0;

// Create a variable to hold the light reading
int piezoReading = 0;

// Define a pin to place an LED
int ledPin = D6;

// Create a variable to store the LED brightness.
int ledBrightness = 0;

// Define a pin to place the switch
int switchPin = D4;

void setup() {

 // Set up the LED for output
  pinMode(ledPin, OUTPUT);
  
 // Set up the switch for input
  pinMode(switchPin , INPUT_PULLUP); 

  // Create two cloud variables for LED and piezo
  Particle.variable("Piezo", &piezoReading, INT);
  Particle.variable("LED", &ledBrightness, INT);
  
}

void loop() {
  // Use map() to adjust the brightness  
  piezoReading = analogRead(piezoPin);
  ledBrightness = map(piezoReading, 0, 20, 128, 255);
  int buttonState = digitalRead( switchPin );
  digitalWrite( ledPin, LOW );

  if ( buttonState == LOW ){
    
  analogWrite( ledPin, ledBrightness );
  }
  
  else{
    digitalWrite( ledPin, LOW );
  }
  
}
Click to Expand

Content Rating

Is this a good/useful/informative piece of content to include in the project? Have your say!

0