Skip to content

Instantly share code, notes, and snippets.

@suadanwar
Last active October 11, 2019 00:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save suadanwar/a09389f8d63fac189c13d5485fce37ff to your computer and use it in GitHub Desktop.
Save suadanwar/a09389f8d63fac189c13d5485fce37ff to your computer and use it in GitHub Desktop.
This sample code is for height measurement using ultrasonic sensor and dot matrix on Maker UNO's tutorial.
#include <MD_Parola.h>
#include <MD_MAX72xx.h>
#include <SPI.h>
#define HARDWARE_TYPE MD_MAX72XX::FC16_HW
#define MAX_DEVICES 4
#define CLK_PIN 13
#define DATA_PIN 11
#define CS_PIN 10
#define scrollSpeed 70 // scroll speed
#define scrollPause 0 // ms of pause after finished displaying message
// Hardware SPI connection
MD_Parola P = MD_Parola(HARDWARE_TYPE, CS_PIN, MAX_DEVICES);
// Arbitrary output pins
// MD_Parola P = MD_Parola(HARDWARE_TYPE, DATA_PIN, CLK_PIN, CS_PIN, MAX_DEVICES);
// sets scrolling direction if slider in middle at start
textEffect_t scrollEffect = PA_SCROLL_LEFT;
textPosition_t scrollAlign = PA_LEFT; // how to align the text
#define BUF_SIZE 75 // Maximum of 75 characters
#define trigPin A0
#define echoPin A1
char testStr[10] = { "" };
String heightStr = "";
long duration;
int distance;
int height;
void setup() {
pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin, INPUT); // Sets the echoPin as an Input
Serial.begin(9600); // Starts the serial communication
P.begin();
}
void loop() {
measure();
if (distance > 60) {
P.displayText("TINGGI", PA_LEFT, 0, 0, PA_PRINT, PA_NO_EFFECT);
} else {
height = 180 - distance;
heightStr = String(height, DEC) + "cm";
heightStr.toCharArray(testStr, 75);
P.displayText(testStr, PA_CENTER, 0, 0, PA_PRINT, PA_NO_EFFECT);
}
P.displayAnimate();
delay(100);
}
void measure() {
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = duration * 0.017;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment