Created
February 26, 2020 04:43
-
-
Save peow2373/689484a36379127b625d015f1e2e8a93 to your computer and use it in GitHub Desktop.
Uses analog inputs from a potentiometer and a photoreceptor to open a door via servo motors and change Neopixels' colors
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
#include <Adafruit_NeoPixel.h> | |
#include <Servo.h> | |
#ifdef __AVR__ | |
#include <avr/power.h> // Required for 16 MHz Adafruit Trinket | |
#endif | |
int analogInPin1 = A0; // Analog input pin that the potentiometer is attached to | |
int analogInPin2 = A1; // Analog input pin that the photoreceptor is attached to | |
int servoPin1 = 5; // Analog output pin | |
int servoPin2 = 6; // Analog output pin | |
Servo servo1; | |
Servo servo2; | |
#define pixelPin 9 // Digital Output | |
#define pixelCount 2 | |
#define pixelPin2 10 | |
int sensorValue1 = 0; // value read from the potentometer | |
int sensorValue2 = 0; // calue read from the photoreceptor | |
int outputValue1 = 0; // value output to ledPin1 | |
int outputValue2 = 0; // calue output to ledPin2 | |
int angle = 0; | |
int angle1 = 0; | |
boolean photo = false; | |
Adafruit_NeoPixel pixels = Adafruit_NeoPixel(pixelCount, pixelPin, NEO_GRB + NEO_KHZ800); | |
Adafruit_NeoPixel pixels2 = Adafruit_NeoPixel(1, pixelPin2, NEO_GRB + NEO_KHZ800); | |
void setup() { | |
// initialize serial communications at 9600 bps: | |
Serial.begin(9600); | |
pinMode(5, OUTPUT); | |
pinMode(6, OUTPUT); | |
pinMode(9, OUTPUT); | |
pinMode(10, OUTPUT); | |
pixels.begin(); // Initialize NeoPixel object | |
pixels.show(); // Initialize all pixels to 'off' | |
pixels.setBrightness(30); | |
pixels2.begin(); | |
pixels.setBrightness(100); | |
pixels2.setPixelColor(0, pixels.Color(255, 255, 255)); // White | |
pixels2.show(); | |
servo1.attach(servoPin1); | |
servo2.attach(servoPin2); | |
} | |
void loop() { | |
// read the analog input values: | |
sensorValue1 = analogRead(analogInPin1); | |
sensorValue2 = analogRead(analogInPin2); | |
Serial.println("potentiometer = "); | |
Serial.println(sensorValue1); | |
Serial.print("\n"); | |
Serial.println("photoreceptor = "); | |
Serial.println(sensorValue2); | |
//delay(1000); | |
if (sensorValue2 <= 900) { | |
photo = true; | |
} | |
if ((sensorValue1 >= 800 && sensorValue1 <= 1023 && sensorValue1 != 0) || photo == true) { | |
pixels.setPixelColor(1, pixels.Color(0, 255, 0)); // Green | |
pixels.setPixelColor(0, pixels.Color(0, 0, 0)); // Off | |
pixels.show(); | |
delay(1500); | |
for (angle = 90; angle < 180; angle++) { | |
servo1.write(angle); | |
delay(15); | |
} | |
for (angle1 = -90; angle1 < 0; angle1++) { | |
servo2.write(angle1); | |
delay(15); | |
} | |
delay(5000); | |
for (angle = 180; angle > 90; angle--) { | |
servo1.write(angle); | |
delay(15); | |
} | |
for (angle1 = 180; angle1 > 90; angle1--) { | |
servo2.write(angle1); | |
delay(15); | |
} | |
delay(1000); | |
photo = false; | |
} else { | |
pixels.setPixelColor(0, pixels.Color(255, 0, 0)); // Red | |
pixels.setPixelColor(1, pixels.Color(0, 0, 0)); // Off | |
pixels.show(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment