Skip to content
Snippets Groups Projects
Commit 28766eb3 authored by Yuliia Denysiuk's avatar Yuliia Denysiuk
Browse files

Adding the website and light draft. Also changed the min max pulse for servo...

Adding the website and light draft. Also changed the min max pulse for servo since the lamp phycaly cannot move to this amplitude.
parent baca3bea
No related branches found
No related tags found
No related merge requests found
#include <Wire.h>
#include <BH1750.h>
#include <Adafruit_PWMServoDriver.h>
#include <WiFi.h>
#include <WebServer.h>
#include <Adafruit_NeoPixel.h>
// Which pin on is connected to the NeoPixels
#define LED_PIN 6
// How many NeoPixels are attached
#define LED_COUNT 60
#define LISTEN_PORT 80
Adafruit_NeoPixel strip(LED_COUNT, LED_PIN, NEO_GRB + NEO_KHZ800);
const char* ssid = "NDL_24G"; //name of local WiFi network in the NDL
const char* password = "RT-AC66U";
WebServer server(80);
int settingValue = 0;
void handleRoot() {
String html = R"rawliteral(
<!DOCTYPE html>
<html>
<head><title>FireBeetle Settings</title></head>
<body>
<h1>FireBeetle ESP32 Settings</h1>
<p>Current Value: )rawliteral" + String(settingValue) + R"rawliteral(</p>
<form action="/set" method="POST">
New Value: <input type="number" name="value">
<input type="submit" value="Update">
</form>
</body>
</html>
)rawliteral";
server.send(200, "text/html", html);
}
void handleSet() {
if (server.hasArg("value")) {
settingValue = server.arg("value").toInt();
}
server.send(200, "text/html", "<h1>Value Updated!</h1><a href='/'>Go Back</a>");
}
BH1750 lightsens1;
BH1750 lightsens2;
......@@ -10,8 +52,8 @@ TwoWire I2C_1 = TwoWire(0); // First I2C bus
TwoWire I2C_2 = TwoWire(1); // Second I2C bus
Adafruit_PWMServoDriver pwm = Adafruit_PWMServoDriver(0x40, I2C_1);
int angle = 150; // Initial angle at the middle 0
int pulse = 292; // Initial pulse of rest
int angle = 210; // Initial angle at the middle 0
int pulse = 294; // Initial pulse of rest
int stepSize = 5; // Degree of movement per loop iteration
int botser = 15; // Big servo pin number
int topser = 14; // Small servo pin number
......@@ -20,14 +62,40 @@ int startDiffX = 0; // Diff in luxes at the start
int startDiffY = 0; // Diff in luxes at the start
#define SERVOMIN 150 // Minimum pulse length (0 degrees)
#define SERVOMAX 600 // Maximum pulse length (180 degrees)
#define SERVOMIN 200 // Minimum pulse length (0 degrees)
#define SERVOMAX 500 // Maximum pulse length (180 degrees)
#define SERVO_FREQ 50 // Analog servos run at ~50 Hz
void setup() {
Serial.begin(115200);
//Wire.begin();
//WEBSITE
WiFi.begin(ssid, password); // Start Wi-Fi connection to specified access point
Serial.println("Start connecting.");
while (WiFi.status() != WL_CONNECTED) { // Wait until we are connected
delay(500);
Serial.print(".");
}
Serial.print("Connected to ");
Serial.print(ssid);
Serial.print(", IP address: ");
Serial.println(WiFi.localIP()); // Print local IP to Serial Monitor
server.on("/", handleRoot);
server.on("/set", HTTP_POST, handleSet);
server.begin();
Serial.println("HTTP server started");
//END WEBSITE
//LIGHTS
strip.begin(); // INITIALIZE NeoPixel strip object (REQUIRED)
strip.show(); // Turn OFF all pixels ASAP
strip.setBrightness(50); // Set BRIGHTNESS to about 1/5 (max = 255)
//END LIGHTS
//LIGHT SENSORS
// Initialize the first I2C bus (I2C_1) on pins GPIO 21 (SDA) and GPIO 22 (SCL)
I2C_1.begin(21, 22); // SDA, SCL
......@@ -57,14 +125,17 @@ void setup() {
uint16_t strtlux3 = lightsens3.readLightLevel();
startDiffX = strtlux1 - strtlux2;
startDiffY = strtlux3 - (strtlux1+strtlux2)/2;
//END LIGHT SENSORS
//SERVOS
pwm.begin();
pwm.setOscillatorFrequency(27000000);
pwm.setPWMFreq(SERVO_FREQ); // Set frequency to 50 Hz for servo control
pwm.setPWM(botser, 0, pulse);
pwm.setPWM(topser, 0, angle);
//END SERVOS
Serial.println("Waiting...");
delay(1000);
Serial.println("Finished setup");
......@@ -73,20 +144,19 @@ void setup() {
// Calculate the difference in light levels
int difToMove(int lux1, int lux2, bool i) {
int difToMove(int lux1, int lux2, int startDiff) {
int diff = 0;
if (i){
diff = (lux1 - lux2 - startDiffX)/secitivity;
}
else{
diff = (lux1 - lux2 - startDiffY)/secitivity;
}
//diff = (lux1 - lux2 - startDiff)/secitivity;
diff = (lux1 - lux2)/secitivity;
return diff;
}
void loop() {
server.handleClient(); //Handle all website logic, this should be run every loop!
for(int i=0; i<strip.numPixels(); i++){
strip.setPixelColor(i, strip.Color(127, 127, 127));
}
int p = pulse; // update pulse to the initial state every loop so it would not move forever
uint16_t lux1 = lightsens1.readLightLevel();
......@@ -95,8 +165,8 @@ void loop() {
uint16_t lux4 = (lux1 + lux2)/2; // adding this for easier control of top servo
Serial.printf("light1= %d \nlight2= %d\nlight3= %d\ncombined top light = %d\n", lux1, lux2, lux3, lux4); //Serial.printf("light1= %d \nlight2= %d", lux1, lux2);
int xdiff = difToMove(lux1, lux2, 1); // Determine the step based on light difference
int ydiff = difToMove(lux3, lux4, 0);
int xdiff = difToMove(lux2, lux1, startDiffX); // Determine the step based on light difference
int ydiff = difToMove(lux3, lux4, startDiffY);
Serial.printf("xdiff: %d \n ydiff: %d \n", xdiff, ydiff);
//Serial.printf("xdiff: %d \n", xdiff);
......@@ -106,7 +176,7 @@ void loop() {
angle = min(SERVOMAX, angle);
p = p - xdiff; //We can add some restrictions, but so far there are no way we can go over max or under min
p = p + xdiff; //We can add some restrictions, but so far there are no way we can go over max or under min
Serial.printf("New pulse: %d \nNew angle: %d \n", p, angle);
pwm.setPWM(botser, 0, p);
pwm.setPWM(topser, 0, angle);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment