Skip to content

Instantly share code, notes, and snippets.

@Aleksey-Danchin
Created February 9, 2025 13:29
Show Gist options
  • Save Aleksey-Danchin/e66b490b5b8952e3eb3f41f2b5a2f83b to your computer and use it in GitHub Desktop.
Save Aleksey-Danchin/e66b490b5b8952e3eb3f41f2b5a2f83b to your computer and use it in GitHub Desktop.
Arduino Netgun
#include <Servo.h>
#define PIN_PHOTO_SENSOR A0
#define PIN_SERVO 11
#define LIGHT_LIMIT_K 80
#define MAX_POSITIVE_SPEED 180
#define MAX_NEGATIVE_SPEED 0
#define REST_SPEED 90
Servo servo;
bool isRotated = false;
void setup() {
Serial.begin(9600);
servo.attach(PIN_SERVO);
servo.write(REST_SPEED);
}
void loop() {
int val = analogRead(PIN_PHOTO_SENSOR);
bool nextIsRotated = val < LIGHT_LIMIT_K;
Serial.println(val);
if (nextIsRotated != isRotated) {
isRotated = nextIsRotated;
servo.write(
isRotated
? MAX_POSITIVE_SPEED
: MAX_NEGATIVE_SPEED
);
delay(250);
servo.write(REST_SPEED);
delay(1000);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment