use servo library for both motors

This commit is contained in:
Roald Batts 2023-01-26 20:55:37 -05:00
parent 965fbc7b81
commit 66ab550699

View file

@ -2,13 +2,15 @@
#include <LiquidCrystal_I2C.h>
#include <DHT.h>
#include <BH1750.h>
#include <Servo.h>
#define DHT11_PIN 7
#define photoL1 A0
#define LED 3
#define button 2
#define buzzer 6
#define servoPin 10
#define circleServoPin 9
#define tiltServoPin 10
BH1750 lightMeter;
DHT dht(DHT11_PIN, DHT11);
@ -16,6 +18,8 @@ volatile int buttonState;
volatile int lightReadL1 = 0;
int count;
LiquidCrystal_I2C lcd(0x27, 16, 2);
Servo circleServo;
Servo tiltServo;
void setup()
{
@ -24,7 +28,9 @@ void setup()
pinMode(LED, OUTPUT); //initialize digital pin LED as an output.
pinMode(button, INPUT);
pinMode(buzzer, OUTPUT);
pinMode(servoPin,OUTPUT);
circleServo.attach(circleServoPin);
tiltServo.attach(tiltServoPin);
Serial.print("init lcd\n");
lcd.init();
lcd.backlight();
@ -50,17 +56,17 @@ void loop(){
//Serial.print(count);
//Serial.print("] hey\n");
Serial.println("0");
servopulse(servoPin,0);
delay(500);
Serial.println("90");
servopulse(servoPin,90);
delay(500);
Serial.println("180");
servopulse(servoPin,180);
delay(500);
delay(300);
for (int i = 180; i > 0; i--) {
circleServo.write(i);
tiltServo.write(i);
delay(10);
}
for (int i = 0; i < 180; i++) {
circleServo.write(i);
tiltServo.write(i);
delay(10);
}
lightReadL1 = analogRead(photoL1);
if (lightReadL1 > 300){
@ -156,12 +162,3 @@ void blink()
digitalWrite(LED,LOW); //turn the LED off by making the voltage LOW
delay(1000); //wait for a second
}
void servopulse(int pin, int myangle){
int pulsewidth = map(myangle, 0, 180, 500, 2500);
for (int i = 0; i < 10; i++){
digitalWrite(pin,HIGH);
delayMicroseconds(pulsewidth);
digitalWrite(pin,LOW);
delay(20-pulsewidth/1000);
}
}