Merhaba! Bir arkadaşımla bir animatronik yapmaya çalışıyoruz. Arkadaşım dizaynı aldı bende kod kablo fln. Hemen kablolru alıp döşemeden önce Tinkercad circuits de bir deneyeyim dedim. Yapmaya çalıştığım şey, ultrasonik mesafe sensörünün değerini CM şeklinde okuyup, distance değişkenini ona göre değiştirmek, eğer distance 250 den aşşağıya düşersede, Jumpscare değişkenine 100 verip Jumpscare 95 den büyük olur ise 3 pinini Yüksek ayarlamak. Size devreyide kosuda veriyorum. (NOT kodda hiç iyi değilim baya yardım fln aldım ve devre çalışmıyo) Kod:
int distance = 0;
int jumpscare = 0;
long readUltrasonicDistance(int triggerPin, int echoPin)
{
pinMode(triggerPin, OUTPUT); // Clear the trigger
digitalWrite(triggerPin, LOW);
delayMicroseconds(2);
// Sets the trigger pin to HIGH state for 10 microseconds
digitalWrite(triggerPin, HIGH);
delayMicroseconds(10);
digitalWrite(triggerPin, LOW);
pinMode(echoPin, INPUT);
// Reads the echo pin, and returns the sound wave travel time in microseconds
return pulseIn(echoPin, HIGH);
}
void setup()
{
pinMode(2, OUTPUT);
pinMode(8, OUTPUT);
}
void loop()
{
while (1 < 2) {
distance = 0.01723 * readUltrasonicDistance(1, 3);
}
while (1 < 2) {
if (distance < 250) {
jumpscare = 100;
} else {
jumpscare = 10;
}
}
if (jumpscare > 95) {
digitalWrite(2, HIGH);
}
while (1 < 2) {
digitalWrite(8, HIGH);
}
delay(10); // Delay a little bit to improve simulation performance
}
int distance = 0;
int jumpscare = 0;
long readUltrasonicDistance(int triggerPin, int echoPin)
{
pinMode(triggerPin, OUTPUT); // Clear the trigger
digitalWrite(triggerPin, LOW);
delayMicroseconds(2);
// Sets the trigger pin to HIGH state for 10 microseconds
digitalWrite(triggerPin, HIGH);
delayMicroseconds(10);
digitalWrite(triggerPin, LOW);
pinMode(echoPin, INPUT);
// Reads the echo pin, and returns the sound wave travel time in microseconds
return pulseIn(echoPin, HIGH);
}
void setup()
{
pinMode(2, OUTPUT);
pinMode(8, OUTPUT);
}
void loop()
{
while (1 < 2) {
distance = 0.01723 * readUltrasonicDistance(1, 3);
}
while (1 < 2) {
if (distance < 250) {
jumpscare = 100;
} else {
jumpscare = 10;
}
}
if (jumpscare > 95) {
digitalWrite(2, HIGH);
}
while (1 < 2) {
digitalWrite(8, HIGH);
}
delay(10); // Delay a little bit to improve simulation performance
}