Author Topic: AnalogRead piezo freezing board?  (Read 1675 times)

Helyx

  • Newbie
  • *
  • Posts: 2
AnalogRead piezo freezing board?
« on: May 12, 2016, 11:34:31 pm »
Hi all, noob to the digispark world!
Trying to use a piezo to turn on some leds, and having issues. Got digiKeyboard to output values at certain points in the loop to see what's up. I noticed that when I flick the piezo somewhat hard to try to get a reading, the digikeyboard output just stops for 2 or 3 seconds, then starts again. I can get the program to work sometimes, but it seems unreliable. Any help? I have a 1Mohm resistor in parallel with the piezo to control the voltage from it, should I get a bigger resistor?

Thanks!

Helyx

  • Newbie
  • *
  • Posts: 2
Re: AnalogRead piezo freezing board?
« Reply #1 on: June 25, 2016, 04:23:43 pm »
Still having this problem, but it seems to only occur when a hall effect sensor I have also rrturns LOW.
 Here's my code if anyone can help. Basically using a hall effect sensor and reading a double-tap on the piezo to turn lights on and off. I think the program works but it freezes so often that it's hard to tell. >_<



#include "DigiKeyboard.h"
// these constants won't change:
const int ledPin = 1;      // led connected to pin 1
const int hallPin = 0;     // Output pin 3 of Hall sensor to pin 0
const int knockSensor =0; // the piezo is connected to pin 5(not a typo)
const int threshold = 66;  // threshold value to decide when the detected sound is a knock or not

int ledState = false;         // variable used to store the last LED status, to toggle the light

void setup()  {
   // initialize the LED pin as an output:
  pinMode(ledPin, OUTPUT);     
  // initialize the hall effect sensor pin as an input:
  pinMode(hallPin, INPUT);
  pinMode(2, INPUT);   
 Serial.begin(9600);
}

void loop(){
 // these variables will change:
int piezoReading = 0;      // variable to store the value read from the sensor pin
int hallState = HIGH;
int num1 = 0;
int num2 = 0;
int PiezoSwitch = false;
int switch1 = false;
int switch2 = false;
int inLoop = false;
unsigned long ms = millis();

DigiKeyboard.print("LED State=");
DigiKeyboard.println(ledState);

hallState = digitalRead(hallPin);
DigiKeyboard.print("hall state=");
DigiKeyboard.println(hallState);
 
num1 = analogRead(knockSensor);
DigiKeyboard.print("num1= ");
DigiKeyboard.println(num1);
 delay(66);
 if(num1 > threshold && hallState == LOW && ledState == false) inLoop = true;
 while(millis()-ms < 777 && inLoop == true)     //Has 777ms passed?
  {
    num2 = analogRead(knockSensor);
    if(num2 > threshold)
    {
      DigiKeyboard.print("num2= ");
      DigiKeyboard.println(num2);
      inLoop = false;
      PiezoSwitch = true;
      DigiKeyboard.println("PiezoSwitch=true");
    }
   }
  //delay(111);
    ms = millis();           //and reset time.
 inLoop = false;
 
 if(PiezoSwitch == true) {
  switch1=true;
   }
   if(switch1 == true)  {
     if(ledState == false)  {
       digitalWrite(ledPin, HIGH);
       DigiKeyboard.println("PHOTONS");
       ledState = true;
     }
   }
num1 = 0;
num2 = 0;
delay(1000);
if(ledState == true) {
 num1 = analogRead(knockSensor);
 DigiKeyboard.print("OFF num1= ");
 DigiKeyboard.println(num1);
 delay(66);
 if(num1 > threshold) inLoop = true;
  while(millis()-ms < 777 && inLoop == true)     //Has 777ms passed?
    {
      num2 = analogRead(knockSensor);
      DigiKeyboard.print("OFF num2= ");
      DigiKeyboard.println(num2);
      if(num2 > threshold)
       {
        inLoop = false;
         digitalWrite(ledPin, LOW);
         DigiKeyboard.println("no photons for you");
         ledState = false;
     }
    }
  //delay(111);
    ms = millis();           //and reset time.
 }
 delay(666);
}

Ralf

  • Guest
Re: AnalogRead piezo freezing board?
« Reply #2 on: June 26, 2016, 04:06:23 am »
There are two obvious mistakes in the code (everybody who had read the wiki should have avoided them ;) ):

- You shall not use SerialBegin() when your board has no hardware Serial (I wonder why the code is compiled at all)

- You shall use Digispark.Keyboard.delay() instead of delay(). The Software USB routines are time-critical and your computer is annoyed if he polls the USB gadget and gets no answer.  Digispark.Keyboard.delay() serves polls in the backgroud.