Created
December 15, 2016 20:01
-
-
Save WlascicielBMW/0ff408a5f0326cbdf729da6d11280ff9 to your computer and use it in GitHub Desktop.
Arduino UNO + HC-SR04 + LED RBG + Port Szeregowy
This file contains 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
int Trig = 2; //pin 2 Arduino połączony z pinem Trigger czujnika | |
int Echo = 3; //pin 3 Arduino połączony z pinem Echo czujnika | |
int red = 8; // podłaczenie diody R=8, G=10, B=9, V=GND | |
// podłączenie termometru OUT=3, +=5V, -=GND | |
int green = 10; | |
int blue = 9; | |
int CM; //odległość w cm | |
long CZAS; //długość powrotnego impulsu w uS | |
void setup() | |
{ | |
Serial.begin(9600); //inicjalizaja monitora szeregowego | |
pinMode(Trig, OUTPUT); //ustawienie pinu 2 w Arduino jako wyjście | |
pinMode(Echo, INPUT); //ustawienie pinu 3 w Arduino jako wejście | |
Serial.println("Test czujnika odleglosci"); | |
} | |
void pomiar_odleglosci() | |
{ | |
digitalWrite(Trig, HIGH); //ustawienie stanu wysokiego na 10 uS - impuls inicjalizujacy - patrz dokumentacja | |
delayMicroseconds(10); | |
digitalWrite(Trig, LOW); | |
CZAS = pulseIn(Echo, HIGH); | |
CM = CZAS / 58; //szerokość odbitego impulsu w uS podzielone przez 58 to odleglosc w cm - patrz dokumentacja | |
} | |
void dioda () | |
{ | |
if ( float(CM) >= 10, float(CM)<= 30) { //odczytanie wartości "t" z biblioteki i ustawienie warunku niskiego | |
digitalWrite(red, LOW); | |
digitalWrite(green, LOW); | |
digitalWrite(blue, HIGH); | |
} | |
if ( float(CM) > 30) { //odczytanie wartości "t" z biblioteki i ustawienie warunku rownego | |
digitalWrite(blue, LOW); | |
digitalWrite(red, LOW); | |
digitalWrite(green, HIGH); | |
} | |
if ( float(CM) < 10) { //odczytanie wartości "t" z biblioteki i ustawienie warunku wyższego | |
digitalWrite(blue, LOW); | |
digitalWrite(green, LOW); | |
digitalWrite(red, HIGH); | |
} | |
} | |
void loop() | |
{ | |
pomiar_odleglosci (); //pomiar odległości | |
dioda (); | |
Serial.print("Odleglosc: "); //wyświetlanie wyników na ekranie w pętli co 200 ms | |
Serial.print(CM); | |
Serial.println(" cm"); | |
delay(100); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment