digital vibration sensors report HIGH - always?

Gents, software guy here messing with hardware. Wish to study vibrations, pretty happy with what I’ve learned via analogRead. Not so much on digitalRead.

Want to study where the ambiguity zone is - need to tap the desk or kick the desk to get sensor to read HIGH? Code & output below using UNO and
Pin 3 an 801 sensor
Pin 8 an SW18010P sensor
Pin 12 a something w/o a label???

Have some simple error of understanding. Know nothing about pullup resistors, would be happy if code generated output I could relate back to how hard I hit the desk. in Ddata2() tried with digitalRead == HIGH and digitalRead != HIGH. Seems to give same result, apparently “hit” equals 1 at the end of every pass thru the 3 sensors.

Cold beer next time you are in Dallas to keep my life from being ruined by something I know will turn out to be simple…

The .txt file was copied & pasted off the serial monitor.

Dbaby2.txt (1.64 KB)

Kinda need the code to make any sense out of that output.

Thought I’d posted the code…

char pgmname = “babyD2”;
int i, p, r, go, hit, prints;
int loopcount;
int LEDGo = 6;
int DPins[5];
int Dpincount;
int hitpin[5];
unsigned long hittime[5];
unsigned long loopstart;

void nameDpins() // this will be for Digital pins AND must enter at least 1 pin to read
{
errsetsen:
Serial.println(“Enter 1 to 5 Digital pins to read: pin#,pin#,…”);
while (!Serial.available()){ ;}
delay(50);
i = 0;
r = 0;
while(Serial.available())
{
DPins = Serial.parseInt();

  • r++;*
    if(DPins < 0 || DPins > 15) r = 6; // setting error condition to be caught below
    * if(Serial.read() != ‘\n’)*
    * {*
    _ Serial.println("Digital pin: " + String(DPins*) + " at position: " + String(i) + “.”);
    i++;
    }
    }
    if(r < 1 || r > 5)goto errsetsen;
    Dpincount = r; // pin numbers & count are OK*

    } // end nameDpins
    void setDlow() // for digital pins, set them to LOW and set them back to INPUT
    {
    * Serial.println(“setting pins”);
    for(p = 0; p < Dpincount; p++){
    pinMode(DPins[p], OUTPUT);
    digitalWrite(DPins[p], LOW);
    pinMode(DPins[p], INPUT);
    Serial.println("Set to LOW Pin: "+ String(DPins[p])+ "reads as: " + String(digitalRead(DPins[p])) + “.”);
    }
    } // end of setDlow()
    void Ddata2(){ // actual work done here - want it to just cycle thru up to 5 digital sensors and report when vib felt
    for(i = 0; i < Dpincount; i++){ // initializes data arrays and refreshes after a data hit*

    hittime = 0;
    hitpin = 0;
    * hit = 0;
    }
    while(go == 1){ // loop til a vib felt*

    * for(i = 0; i < Dpincount; i++){ // put data in the arry from each sensor that feels a vib*
    if(digitalRead(DPins == HIGH)){
    hittime = micros();
    hitpin = DPins*;*
    * hit = 1;
    go = 0; // won’t test this until rest of pins read on pass with hit*

    * }
    }
    }
    Serial.println("Hit in loop = " + String(loopcount)+ “.”); // display what happened*

    * for(i = 0; i < Dpincount; i++){
    Serial.println("Pin= "+String(DPins) + " hittime= " + String(hittime));
    }
    }*

    void setup() {_

* pinMode(LEDGo, OUTPUT);*
* Serial.begin(9600);*
* Serial.println("-----" + String(pgmname) + “-----”);*
* Serial.println(“Enter 1 line Session comment or a dot, then press ENTER:”);*
* while (!Serial.available()){ ;}*
* delay(50);*
* while(Serial.available())*
* {*
* Serial.println(String(Serial.readString()));*
* }*
* nameDpins();*
* go = 1;*
* loopcount = 0;*
}
void loop() { // just loop forever, kill it w/ reset button
* loopcount++;*
* setDlow();*
* loopstart = micros();*
* Serial.println(" Going to Ddata1 or Ddata2.");*
* digitalWrite(LEDGo, HIGH);*
* Ddata2();*
* digitalWrite(LEDGo, LOW);*
* }*

Don't see how to paste code! Yikes. Used simple "select all" from IDE, the copy & paste. DID NOT WORK!

In the code above the first time it came to an open bracket in an instruction involving an array to started printing "displaying?) in italics and never displayed another bracket, either open or close!

Sorry about confusion here, gonna try and paste code properly, however that is. And yes, I read every word of instructions before I ever posted to this site! Jus' sayin'

Now understand to not use “quick reply” when posting code! The .txt file attached is output of this sketch.

I do not understand why I get time data from micros() in only the first loop!

char pgmname[] = "babyD2";
int i, p, r, go, hit, prints;
int loopcount;
int LEDGo = 6;
int DPins[5];
int Dpincount;
int hitpin[5];
unsigned long hittime[5];
unsigned long loopstart;

void nameDpins() // this will be for Digital pins AND must enter at least 1 pin to read
{
  errsetsen:
  Serial.println("Enter 1 to 5 Digital pins to read: pin#,pin#,...");
  while (!Serial.available()){ ;}
  delay(50);
  i = 0;
  r = 0;
  while(Serial.available())
  {
    DPins[i] = Serial.parseInt();
    r++;
    if(DPins[i] < 0 || DPins[i] > 15) r = 6;  // setting error condition to be caught below
    if(Serial.read() != '\n')
    {
      Serial.println("Digital pin: " + String(DPins[i]) + " at position: " + String(i) + ".");
      i++;
    }
  }
   if(r < 1 || r > 5)goto errsetsen;
   Dpincount = r;  // pin numbers & count are OK
}  // end nameDpins


void setDlow() // for digital pins, set them to LOW and set them back to INPUT
{
  Serial.println("setting pins");
  for(p = 0; p < Dpincount; p++){
    pinMode(DPins[p], OUTPUT);
    digitalWrite(DPins[p], LOW);
    pinMode(DPins[p], INPUT);
    Serial.println("Set to LOW Pin: "+ String(DPins[p])+ "reads as: " + String(digitalRead(DPins[p])) + ".");   
  } 
}  // end of setDlow()

void Ddata2(){  // actual work done here - want it to just cycle thru up to 5 digital sensors and report when vib felt
  for(i = 0; i < Dpincount; i++){  // initializes data arrays and refreshes after a data hit
    hittime[i] = 0;
    hitpin[i] = 0;
    hit = 0;
  }
  while(go == 1){ // loop til a vib felt
  for(i = 0; i < Dpincount; i++){ // put data in the arry from each sensor that feels a vib
    if(digitalRead(DPins[i] == HIGH)){
      hittime[i] = micros();
      hitpin[i] = DPins[i];
      hit = 1;
      go = 0; // won't test this until rest of pins read on pass with hit
      }
  }
  }
  Serial.println("Hit in loop = " + String(loopcount)+ ".");  // display what happened
  for(i = 0; i < Dpincount; i++){
    Serial.println("Pin= "+String(DPins[i]) + " hittime= " + String(hittime[i]));
    }
}

void setup() {
  
  pinMode(LEDGo, OUTPUT);
  Serial.begin(9600);
  Serial.println("-----" + String(pgmname) + "-----");
  Serial.println("Enter 1 line Session comment or a dot, then press ENTER:");
   while (!Serial.available()){ ;}
   delay(50);
   while(Serial.available())
    {
      Serial.println(String(Serial.readString()));
    }
    nameDpins();
    go = 1;
    loopcount = 0;
}

void loop() { // just loop forever, kill it w/ reset button
  loopcount++;
  setDlow();
  loopstart = micros();
  Serial.println(" Going to Ddata1 or Ddata2.");
  digitalWrite(LEDGo, HIGH);
  Ddata2();
  digitalWrite(LEDGo, LOW);
  }

Dbaby2.txt (1.64 KB)

Hi,
If you attach a code file.
Attach it as the .ino file that it is stored as.

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

Thanks… Tom… :slight_smile:

Go read the "How to Use This Forum" post at the top of any of the boards and you'll learn how to post code right. Wading in by the seat of your pants is a recipe for frustration in this hobby. Never be afraid to read the instructions.

yep. re-read that and think the code is now posted inside the tag pairs.