Author Topic: Another NOOB LED Flashy thingy question  (Read 20 times)

kopskoot

  • Newbie
  • *
  • Posts: 1
Another NOOB LED Flashy thingy question
« on: October 11, 2019, 01:31:12 pm »
Hi all. Noob here.

I am trying to create a little "Light up an LED when you receive a knock" project.

I am using a Digispark ATTiny85 with a LED and a Piezo sensor (with 1 Mega ohm resistor between the + and - of the piezo.)

negative cable of Piezo is connected to Pin 5 on AtTiny
LED is connected to Pin1

When I set my threshold too low, the LED keeps on flashing.

When I set it higher bit by bit, it gets to a point where the LED does not flash anymore, but when I then tap the Piezo it sounds like the ATTINY disconnects from USB (powering it from USB) and reconnects. Powering it from DC power supply simply does nothing if I tap the Piezo.

Herewith my "Code".

It has been adjusted from a similar project I did with Arduino Uno, with some minor changes for addressing the ports.

Please let me know where I am going wrong.

thanks.



// these constants won't change:
const int ledPin = 1;// LED connected to digital pin 1
const int knockSensor = 0; // the piezo is connected to analog pin 0 which is pin 5
const int threshold = 1095;  // threshold value to decide when the detected sound is a knock or not


// these variables will change:
int sensorReading = 0;      // variable to store the value read from the sensor pin
int ledState = LOW;         // variable used to store the last LED status, to toggle the light

void setup() {
  pinMode(ledPin, OUTPUT); // declare the ledPin as as OUTPUT
pinMode(5, INPUT);
}

void loop() {
  // read the sensor and store it in the variable sensorReading:
  sensorReading = analogRead(knockSensor);

  // if the sensor reading is greater than the threshold:
  if (sensorReading > threshold) {


  digitalWrite(ledPin, HIGH);   // sets the LED on

  delay(100);                  // waits for a second
  digitalWrite(ledPin, LOW);    // sets the LED off

  delay(500);                  // waits for a second
   
  }
 
  }