Bad day downloading lib

Just found the simple code from a member using hc-sr04, and also found an SR04 in a junk bag. In order to test the code, downloading the lib is needed.
Did find several sr04 libs and clicked on "installing".
Then.... using Win10, I don't find the bastards for including in the Arduino library folder.

Any clever guess what do I miss?

I'm not sure I understand if your need is real or a joke.
but see if this link helps.
The text is in Portuguese but the link is in English.

Or here:
https://downloads.arduino.cc/libraries/github.com/ErickSimoes/Ultrasonic-3.0.0.zip
RV mineirin

Here's what I use for my SR04. No libraries.
There are some other functions but I'm sure you know how to extract what you want.
There is a "more accurate" method using timer interrupts but with as SR04 super precision timing is not warranted.

I use sort for a what I've learned is a median filter. Simply put I take some measurements, sort them and use the middle measurement.

This version outputs the distance data to a cc2530 Zigbee module.

//GarageMeasurement Application
// Target:  8Mhz Pro Mini with optiboot (req'd for WDT)
// Approximate measurements
//    Car ~ 120 cm
//  noCar ~ 250 cm
// Define Trig and Echo pin:
// v02a seems to work with 5V Arduino and HC-SR04
// For hardware information, see end of file

// 2020-12-21 v02c - added toggle LED
// 2020-12-22 v03a - changed serial to read stream looking for \n char to indicate end of last command.
// 2020-12-24 v03b - changed "distance" to 16 bit as the echo would overflow if it were uint8_t
// 2020-12-28 v04a - updated notes.
// 2020-12-30 v04c - added WDT, updated hardware bootloader to optiboot

// TODO
//		Change #OK.  timing.

#include "ArduinoSort.h"  // downloaded library for sorting arrays.  Currently in program folder
#include <avr/wdt.h>

#define trigPin 9
#define echoPin 8
#define NumReadings 9       // index - to 6,  Number must be odd for sort to work properly
#define MedianReading 4

#define measCMD @garage.   // we really only care about the @ as we decide to take a reading
                           //  based on inputBuff[0] = '@'  ~line 57

// --- Define variables ------------------------

uint16_t distance;
char inputBuff[30];
uint16_t inputRdgs[NumReadings + 2];
bool inputError;                        // not yet implemented
bool inputValid;                        // not yet implemented
uint8_t tempByte;
uint8_t i;
uint8_t k = 0;

unsigned long checkinTime =  600000;     //86400000;  // s/b 24 hours
unsigned long checkinZero;
const byte MessageLength = 16;


// --- Function Prototypes ---------------------

void measureDistance(void);
bool readSerial(void);
void writeSerial(void);
uint16_t getEchoDistance (void);
void sendDistance(void);
void initializeZigbee(void);
void watchdogSetup(void);

// --- Setup -----------------------------------
void setup() {

  // Define inputs and outputs:
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  pinMode( LED_BUILTIN, OUTPUT);

  Serial.begin(38400);
  checkinZero = millis();
  delay(500);
  void watchdogSetup();
  initializeZigbee();
}

// --- Loop -----------------------------------
void loop() {


if (Serial.available() ){
    tempByte = (uint8_t) Serial.read();
    if (tempByte == 0x40){
        i = 0;
        tempByte = (uint8_t) Serial.read();
        while (tempByte != 0x2E){
          inputBuff[i++] = tempByte;
          tempByte = (uint8_t) Serial.read();
        }
        measureDistance();
        sendDistance();
    }
  } // --- if ---  (read !. initiates measurement)

  else {
    if((millis()-checkinZero) > checkinTime){
      Serial.write("#OK.");
      checkinZero = millis();
    }
  }
  delay(10); //
  // just to slow down the LED, count loops then toggle.
  if (k > 200){
    asm ("sbi %0, %1 \n": : "I" (_SFR_IO_ADDR(PINB)), "I" (PINB5));    // Toggle LED
    k = 0;
  }
  ++k;
  //wdt_reset();
}  // --- loop ----


//// --------------------------------------------------------------------------------////
/// --- Functions -------------------------------------------------------------------///
// ----------------------------------------------------------------------------------//

void measureDistance(){

  for (int j = 0; j < (NumReadings + 1); j++) {
    inputRdgs[j] = getEchoDistance();
    delay(100);
  }
  sortArray(inputRdgs, NumReadings);
  // after sorting we will ignore the highest 3 and lowest 3 readings.
  distance = inputRdgs[MedianReading];  // this reading is the middle of NumReadings readings.
}

 // ---------------------------------------------------------------------------------
// ----------------------------------------------------------------------------------

uint16_t getEchoDistance (void){
  unsigned long duration;
  digitalWrite(trigPin, LOW);
  delay(5);

  // trigger the sensor
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(8);
  digitalWrite(trigPin, LOW);

  // Read the echoPin, pulseIn() returns the duration (length of the pulse) in microseconds:
  duration = pulseIn(echoPin, HIGH);
  distance = (duration * 0.034 / 2);
  return distance;
}


// --- read & write functions -------------------------------------------------------
// ----------------------------------------------------------------------------------
// Assume ML sends 16 bytes and Hubitat doesn't send two consecutive messages
// we should then read 16 bytes and if there are more we
bool readSerial(void) {
  inputValid = true;
  uint8_t i = 0;

  while (Serial.available()) {
      inputBuff[i] = (byte)Serial.read();  // the (byte) casts the received int16 to int8, hence the parens.
      i++;
  }
  if (i > MessageLength)  inputValid = false;
  return inputValid;
}
// --- serialRead ---

 // ---------------------------------------------------------------------------------
// ----------------------------------------------------------------------------------

void sendDistance(void) {
// send "#"+"distance"+"." to hub
  char cstr[16];
  itoa(distance, cstr, 10);
  char buf[20];
  strcpy(buf,"!");
  strcat(buf,cstr);
  strcat(buf,".");
  Serial.write (buf);   // send off the Zigbee board
} // --- sendDistance ---

/*
char *  itoa ( int value, char * str, int base );

value  =  Value to be converted to a string.
str    =  Array in memory where to store the resulting null-terminated string.
base   =  Numerical base used to represent the value as a string, between 2 and 36, where 10 means decimal base, 16 hexadecimal, 8 octal, and 2 binary.

*/


 // ---------------------------------------------------------------------------------
// ----------------------------------------------------------------------------------

void initializeZigbee()  {
  Serial.write("ping_0.");
  delay(500);
  Serial.write("poll_0.");   // poll_0 not in documentation must test
  delay(500);
  Serial.write("nopollbutt1.");
  delay(500);
  Serial.write("nopollbutt2.");

  delay(500);
  Serial.write("nopolladc0.");
  delay(500);
  Serial.write("nopolladc1.");
  delay(500);
  Serial.write("nopolladc4.");
  delay(500);
  Serial.write("nopolladc5.");

  delay(500);
  Serial.write("off.");
  delay(500);
  Serial.write("pulldown.");
}

void watchdogSetup(void) {
  cli();   // disable all interrupts
  wdt_reset();
  // reset the WDT timer
  /*
  WDTCSR configuration:
  WDIE = 1: Interrupt Enable
  WDE = 1 :Reset Enable
  WDP3 = 0 :For 2000ms Time-out
  WDP2 = 1 :For 2000ms Time-out
  WDP1 = 1 :For 2000ms Time-out
  WDP0 = 1 :For 2000ms Time-out
  */

  // Enter Watchdog Configuration mode:
  WDTCSR |= B00011000;
  // Set Watchdog settings:
  WDTCSR = B01000111;
  sei();
  //Serial.println(WDTCSR);
}

// ------------------------------------------------------------------------------
/*
Hardware Information:

1) HC-SR04 @ 5V
2) Arduino ProMini @ 3.3v / 8Mhz
3) cc2530 with Purple board
      input voltage to purple board = 5V, cc2530 is running at 3.3V.
      I/O = 3.3 V

We tried a level shifter board between the ProMini(3.3v) and the HC-SR04
but it didn't work.  We didn't bother to troubleshoot as there as a
concern the level shifter might cause an inconsistent delay in the echo pulse.

An on-line schematic of the HC-SR04 identified the ┬ÁP as an STC11.  There were
several schematics but the one showing the STC11 processor had the correct
ultrasonic driver (MAX232).
The STC11 processor spec page 216 states, @5V
    Maximum input low = 0.8V
    Minimum input high = 2V
Meaning we can run the Trigger from the ProMini directly into the HC-SR04 input.
Testing proves this out and the spec indicates there is adequate design margin.

ProMini can be directly connected to the cc2530 board as both are 3.3v
*/

// --- eof ---

No, no, I'm not joking. Just never feeling comfortable with the misdirected "help" in Win 10.

Got Ultrasonic lib installed, compiled simple example and downloaded. The printing is 357 what ever I put in front of the sensor......

Thanks but this makes no difference. Fighting Win 10 with this or another download/library can't make much difference.

You don't really need the sort. it was just easier. You can put this in your project folder.

#ifndef ArduinoSort_h
#define ArduinoSort_h

/**** These are the functions you can use ****/

// Sort an array
template<typename AnyType> void sortArray(AnyType array[], size_t sizeOfArray);

// Sort in reverse
template<typename AnyType> void sortArrayReverse(AnyType array[], size_t sizeOfArray);

// Sort an array with custom comparison function
template<typename AnyType> void sortArray(AnyType array[], size_t sizeOfArray, bool (*largerThan)(AnyType, AnyType));

// Sort in reverse with custom comparison function
template<typename AnyType> void sortArrayReverse(AnyType array[], size_t sizeOfArray, bool (*largerThan)(AnyType, AnyType));





/**** Implementation below. Do not use below functions ****/

namespace ArduinoSort {
	template<typename AnyType> bool builtinLargerThan(AnyType first, AnyType second) {
		return first > second;
	}

	template<> bool builtinLargerThan(char* first, char* second) {
		return strcmp(first, second) > 0;
	}

	template<typename AnyType> void insertionSort(AnyType array[], size_t sizeOfArray, bool reverse, bool (*largerThan)(AnyType, AnyType)) {
		for (size_t i = 1; i < sizeOfArray; i++) {
			for (size_t j = i; j > 0 && (largerThan(array[j-1], array[j]) != reverse); j--) {
				AnyType tmp = array[j-1];
				array[j-1] = array[j];
				array[j] = tmp;
			}
		}
	}
}

template<typename AnyType> void sortArray(AnyType array[], size_t sizeOfArray) {
	ArduinoSort::insertionSort(array, sizeOfArray, false, ArduinoSort::builtinLargerThan);
}

template<typename AnyType> void sortArrayReverse(AnyType array[], size_t sizeOfArray) {
	ArduinoSort::insertionSort(array, sizeOfArray, true, ArduinoSort::builtinLargerThan);
}

template<typename AnyType> void sortArray(AnyType array[], size_t sizeOfArray, bool (*largerThan)(AnyType, AnyType)) {
	ArduinoSort::insertionSort(array, sizeOfArray, false, largerThan);
}

template<typename AnyType> void sortArrayReverse(AnyType array[], size_t sizeOfArray, bool (*largerThan)(AnyType, AnyType)) {
	ArduinoSort::insertionSort(array, sizeOfArray, true, largerThan);
}


#endif

Thanks guys! It looks like the one out of 50 boards in a China bag is not willing to work..... Maybe I bought it from Ebay, 20% being used, rejected, reworked.....

If you don't like fighting with library installations, then I recommend using Library Manager. It makes it super simple both to install and to keep your libraries updated. Not every library is in there, but there are 4000 so you have a pretty nice selection. And the fact that a library is not in the Library Manager is a good indication that it is poorly maintained, so you're probably better off with the ones that are in there except for the rare case where none is available for your intended application.

As JohnRob mentioned, a library is not really essential for the ultrasonic sensors. I usually just use pulseIn, but if you do want a library then definitely check out the most popular one. It is in Library Manager under "NewPing by Tim Eckel". Some supplemental documentation for it here:
https://bitbucket.org/teckel12/arduino-new-ping/wiki/Home

I have also had mixed results with the super cheap HC-SR04 I bought from the Chinese sellers on eBay and Aliexpress. I don't know that I ever got a dead one, but some of them were less reliable/sensitive when I tried to use them in an application where there was a short time window to detect an object, meaning they only had a few pings worth of echoes to hear. Some of them could handle it consistently, and others did not. I ended up just sorting through the collection until I had enough of the reliable ones, keeping the rest for less demanding applications.

Using the code given by @ JohnRob in #3 and stripping it down the result is good. Reliable numbers are are showing up in Serial Monitor.


#define trigPin 12
#define echoPin 11

uint16_t getEchoDistance (void);

// --- Setup -----------------------------------
void setup() {

  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);

  Serial.begin(115200);
}

// --- Loop -----------------------------------
void loop() {
  Serial.println(getEchoDistance());
  delay(100);
}

uint16_t getEchoDistance (void) {
  unsigned long duration;
  uint16_t distance;
  digitalWrite(trigPin, LOW);
  delay(5);

  // trigger the sensor
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(8);
  digitalWrite(trigPin, LOW);

  // Read the echoPin, pulseIn() returns the duration (length of the pulse) in microseconds:
  duration = pulseIn(echoPin, HIGH);
  distance = (duration * 0.034 / 2);
  return distance;
}


Thank You for Your help guys!

Still, adding libraries to the IDE world makes trouble most of the times. Sometimes the lib is zipped. Where it is landed on the hard disc is a mystery. Sometimes in the folder downloads but not today.....

Strange, I have Win10. All my downloads go to the folder "download".

I always download the ZIP code, then unzip it to c:...\Documents\Arduino\libaries

Never had an issue.

I use Chrome for a browser, I believe when downloading the download folder is shown.

My feeling is the opposite but I'll try to watch out and not be too trigger happy clicking "download/install"...

If it ain't broke, don't fix it :smiley: I even have switched the notifications off