Sunday 15 October 2017

Ukur Jarak SR04 P10 DMD Arduinio Nano (SKETCH Arduino)


Ukur Jarak SR04 P10 DMD Arduinio Nano

Sensor Ultrasonic SR04, Arduino Nano,P10 DMD,
2 Modul P10 (64 x 32), 5 VDC 2A




Videonya



SKetch Arduino

#include <SPI.h> 

#include <DMD.h> 
#include <TimerOne.h> 
#include <Arial14.h>
//#include "fixednums8x16.h"
#include "SystemFont5x7.h"
#include "Arial_black_16.h"

const int triggerPin = 2; // was pin 12 defore the DMD
const int echoPin = 3; // was pin13 before the DMD

#define DISPLAYS_ACROSS 2 // i whant to display it on 16x64 pixels
#define DISPLAYS_DOWN 1
DMD dmd(DISPLAYS_ACROSS, DISPLAYS_DOWN);

void ScanDMD() { 
  dmd.scanDisplayBySPI(); 
}

//long microsecondsToInches(long microseconds) {
 // return microseconds / 74 / 2;
//}

long microsecondsToCentimeters(long microseconds) {
  // *** THIS NEEDS TO BE CHECKED FOR THE HC-SR04 ***
  return microseconds / 29 / 2;


void setup() {
  // initialize serial communication for the terminal windows.
  Serial.begin(9600);

  // setup DMD refreshing
  Timer1.initialize( 500 ); 
  Timer1.attachInterrupt( ScanDMD ); 
  // initialise DMD as blank, all LEDs off
  dmd.clearScreen( true ); 
  // choose a font to use
  dmd.selectFont (Arial_black_16);

  // set up pins for ultrasonic sensor
  pinMode(triggerPin, OUTPUT);
  pinMode(echoPin, INPUT);
}

void loop() {
  // establish variables for duration of the ping,
  // and the distance result in inches and centimeters:
  long duration, inches, cm;

  // The device is triggered by a HIGH pulse of 2 or more microseconds.
  // Give a short LOW pulse beforehand to ensure a clean HIGH pulse:

  digitalWrite(triggerPin, LOW);
  delayMicroseconds(2);
  digitalWrite(triggerPin, HIGH);
  delayMicroseconds(5);
  digitalWrite(triggerPin, LOW);

  // The echo pin is used to read the signal from the device: a HIGH
  // pulse whose duration is the time (in microseconds) from the sending
  // of the ping to the reception of its echo off of an object.
  duration = pulseIn(echoPin, HIGH);

  // convert the time into a distance
//  inches = microsecondsToInches(duration);
  cm = microsecondsToCentimeters(duration);

//  Serial.print(inches);
//  Serial.print(" in, ");
//  Serial.print(cm);
//  Serial.print(" cm");
//  Serial.println();

  dmd.clearScreen( true );

  String output(inches);
  //output += " in, ";
  output += cm;
  output += " cm"; 
  char buf[64];//[32];
  output.toCharArray(buf, 64);
  dmd.drawString (0,0, buf, strlen(buf), GRAPHICS_NORMAL );
  Serial.println(output);

  delay(700); // 1000 if on will cause an 1 hz blinking value dispalyed
}


No comments:

Post a Comment