Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ArduinoShrink not working with Ultrasonic sensor #11

Open
coder4432 opened this issue Oct 20, 2021 · 3 comments
Open

ArduinoShrink not working with Ultrasonic sensor #11

coder4432 opened this issue Oct 20, 2021 · 3 comments

Comments

@coder4432
Copy link

coder4432 commented Oct 20, 2021

Hi!
I am currently working on a project for the Arduino Mini, with an OLED display, RGB LEDs, and a HC-SR04 Ultrasonic sensor. I am currently troubleshooting the code on an Arduino Uno, before uploading to the Mini. Due to the size of the code, I have added the ArduinoShrink Library to reduce the code size for the tiny Arduino Mini.

However, in testing I have found that when trying to use the Ultrasonic sensor with the ArduinoShrink library, the Ultrasonic sensor does not trigger at all, and stays completely stationary. I have tried without the ArduinoShrink library and works completely fine.

The code below won't work, as I have had to redact the connecting files as they contain personal images. Is there any reason that the ArduinoShrink library would prevent the Ultrasonic sensor from working?

Thanks,

(Using latest Libraries in Arduino 1.8.14)

#include <ArduinoShrink.h>
#include <FastLED.h>
#include <EEPROM.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

int SCREEN_WIDTH = 128; // OLED display width, in pixels
int SCREEN_HEIGHT = 64; // OLED display height, in pixels

int OLED_RESET = - 1; // Reset pin # (or -1 if sharing Arduino reset pin)
int SCREEN_ADDRESS = 0x3C; ///< See datasheet for Address; 0x3D for 128x64, 0x3C for 128x32
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);

int enddelay = 300;            // The amount of delay at the end of the colour loop (before reset) *IT IS NECCESARY FOR PROPER FUNCTION (MINIMUM OF 200)*
int waittime = 1000;              // The amount of time between changing states (IE. 0 means that it will constantly check for updates, 2000 means that it will pick up and update every two seconds)
int pingfreq = 100;


int pingPin = 7;
int echoPin = 8;
int cm;
long duration;
int FirstValue;
int sumDistance;
int averageDistance;
int delayonoff;
unsigned long timestamp1;
unsigned long timestamp2;
long hovertime;
int seconds1;

int onoff = 0;              // Telling the arduino whether the light should be on or not
int changeState = 0;       // Variable for reading the pushchange status
int count_value = 0;      // The stage/colour the arduino is on
int prestate = 0;        // This means that the change cannot be pressed too quickly



unsigned long colourscreentime1;
unsigned long colourscreentime2;
unsigned long colourscreentime0;


void setup() {
  Serial.begin(9600);
  // SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V byteernally
  if (!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) {
    Serial.print(F("SSD1306 allocation failed"));
    for (;;); // Don't proceed, loop forever
  }

  pinMode(pingPin, OUTPUT);
  pinMode(echoPin, INPUT);
  getAVDistance();
  echocalc();
  FirstValue = cm - (cm * 0.8);
  cm = 0;


}




void loop() {

  getDistance();
  if (cm < FirstValue) {
    delayonoff = 0;
    timestamp1 = millis();

    while (cm < FirstValue) {
      getDistance();
    }
    timestamp2 = millis();
    hovertime = timestamp2 - timestamp1;
    seconds1 = hovertime / 1000;
    delay(100);


    if (hovertime <= 750) {
      changeState = 1;
      display.setTextSize(2); // Draw 1X-scale text
      display.setTextColor(SSD1306_WHITE);
      display.setCursor(64, 32);
      display.setTextSize(2); // Draw 1X-scale text
      display.println(F("MOTION DETECTED"));
      display.display();
      // check if the pushchange is pressed. If it is, then the changeState is HIGH:
      if (changeState == 1 && prestate == 0) {
        count_value++;

        //If count_value is 0 then turn off the LEDs
        if (count_value == 0) {
          Serial.print("state0");
          Serial.print(" ");
          colourscreen();
          delay(waittime);
        }
        //If count_value is 1 then tell the Arduino to turn the LEDs on
        if ((count_value == 1) && (onoff = 0)) {
          Serial.print("state1");
          Serial.print(" ");
          colourscreen();
          onoff = 1;
        }
        //If count_value is 2 then set the LEDs to white
        if ((count_value == 2) && (onoff = 1)) {
          Serial.print("state2");
          Serial.print(" ");
          colourscreen();
          delay(waittime);
        }
        //If count_value is 3 then set the LEDs to blue
        if ((count_value == 3) && (onoff = 1)) {
          Serial.print("state3");
          Serial.print(" ");
          colourscreen();
          delay(waittime);
        }
        //If count_value is 4 then set the LEDs to green
        if ((count_value == 4) && (onoff = 1)) {
          Serial.print("state4");
          Serial.print(" ");
          colourscreen();
          delay(waittime);
        }
        //If count_value is 5 then set the LEDs to red
        if ((count_value == 5) && (onoff = 1)) {
          Serial.print("state5");
          Serial.print(" ");
          colourscreen();
          delay(waittime);
        }
        //If count_value is 6 then set the LEDs to purple
        if ((count_value == 6) && (onoff = 1)) {
          Serial.print("state6");
          Serial.print(" ");
          colourscreen();
          delay(waittime);
        }
        //If count_value is 7 then set the LEDs to turquoise
        if ((count_value == 7) && (onoff = 1)) {
          Serial.print("state7");
          Serial.print(" ");
          colourscreen();
          delay(waittime);
        }
        //If count_value is 8 then turn the LEDs off, set count_value to 0 (go back to beginning of loop) and pause for 300ms
        if ((count_value == 8) && (onoff = 1)) {
          Serial.print("state8");
          Serial.print(" ");
          colourscreen();
          onoff = 0;
          count_value = 0;
          delay(enddelay);
        }
      }
      Serial.print(count_value);
    }


    if (hovertime > 750 && hovertime <= 1500) {
      display.setTextSize(2); // Draw 1X-scale text
      display.setTextColor(SSD1306_WHITE);
      display.setCursor(64, 32);
      display.setTextSize(2); // Draw 1X-scale text
      display.println(F("MOTION DETECTED"));
      display.display();
    }



  }
  else {
    delayonoff = 1;
  }
  if (delayonoff == 1) {
    delay(pingfreq);
  }


}

long getDistance() {
  digitalWrite(pingPin, LOW);
  delayMicroseconds(2);
  digitalWrite(pingPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(pingPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  // Calculating the distance
  echocalc();
  return cm;
}

long getAVDistance() {
  digitalWrite(pingPin, LOW);
  delayMicroseconds(2);
  digitalWrite(pingPin, HIGH);
  delayMicroseconds(5);
  digitalWrite(pingPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  // Calculating the distance
  echocalc();
  return cm;
}

void echocalc() {
  cm = duration * 0.034 / 2;
}
@nerdralph
Copy link
Owner

It may be related to issue #10 , since micros is probably used. It'll likely be a week or two before I have time to investigate. In the mean time, could you try 1.8.13, since that's the version I used for testing ArduinoShrink?

@coder4432
Copy link
Author

Sorry for the late reply, I have tested IDE version 1.8.13, and the problem persists, I have also tried the latest version (1.8.16), and the error still occurs.
Thanks for looking into it.

@MCUdude
Copy link

MCUdude commented Jan 9, 2023

The latest version of the ArduinoShrink library will probably resolve the issue

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants