Вот пример кода Arduino, реализующего переключение при столкновении или сбое:
const int collisionPin = 2; // Pin connected to the collision switch
const int ledPin = 13; // Pin connected to an LED (for demonstration)
void setup() {
pinMode(collisionPin, INPUT_PULLUP); // Set the collision pin as input with internal pull-up resistor
pinMode(ledPin, OUTPUT); // Set the LED pin as output
Serial.begin(9600); // Initialize the serial communication
}
void loop() {
int collisionState = digitalRead(collisionPin); // Read the state of the collision switch
if (collisionState == LOW) { // If collision switch is pressed
digitalWrite(ledPin, HIGH); // Turn on the LED
Serial.println("Collision detected!"); // Print a message to the serial monitor
} else {
digitalWrite(ledPin, LOW); // Turn off the LED
}
delay(100); // Add a small delay for stability
}
В этом коде предполагается, что переключатель столкновений подключен к контакту 2 Arduino, а светодиод подключен к контакту 13 в демонстрационных целях. При нажатии переключателя столкновений Arduino включает светодиод и печатает сообщение на последовательный монитор, указывающее на обнаружение столкновения.
Обратите внимание, что теги могут различаться в зависимости от контекста или платформы, на которую вы ориентируетесь. Не стесняйтесь изменять их по мере необходимости.