Created
March 15, 2017 14:16
-
-
Save maxpromer/d0dc34b6ef2885343a9491192a373ff7 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Codeing By IOXhop : www.ioxhop.com | |
*/ | |
#include <SoftwareSerial.h>; | |
#define US100_TX_PIN 2 | |
#define US100_RX_PIN 3 | |
SoftwareSerial mySerial(US100_RX_PIN, US100_TX_PIN); // Rx, Tx | |
void setup() { | |
Serial.begin(9600); | |
mySerial.begin(9600); | |
Serial.println("Distance\tTemperature"); | |
} | |
void loop() { | |
mySerial_flush(); | |
mySerial.write(0x55); | |
delay(100); | |
if(mySerial.available() < 2) { | |
Serial.println("Error read for US-100"); | |
delay(500); | |
return; | |
} | |
unsigned int distance = mySerial.read() * 256 + mySerial.read(); | |
Serial.print(distance); | |
Serial.print(" mm.\t"); | |
mySerial_flush(); | |
mySerial.write(0x50); | |
delay(100); | |
if(mySerial.available() < 1) { | |
delay(500); | |
return; | |
} | |
int temp = mySerial.read() - 45; | |
Serial.print(temp); | |
Serial.print(" *C."); | |
Serial.println(); | |
delay(500); | |
} | |
void mySerial_flush() { | |
while(mySerial.available()) | |
mySerial.read(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment