Skip to content

Commit

Permalink
back on track
Browse files Browse the repository at this point in the history
  • Loading branch information
loan-mgt committed May 21, 2023
1 parent 82c71c6 commit a979ad1
Show file tree
Hide file tree
Showing 4 changed files with 1,095 additions and 12 deletions.
16 changes: 14 additions & 2 deletions L9110/HG7881_L9110/HG7881_L9110.ino
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ void setup()
void loop()
{
noInterrupts();
Serial.println("hello");
Serial.println("step1");
interrupts();


Expand All @@ -37,17 +37,29 @@ void loop()
}else{
motor1.set_target(target);
}



noInterrupts();
Serial.println("step2");
interrupts();


motor1.start();

while (!motor1.target_reached()) {
motor1.start();


}
noInterrupts();
Serial.println("step3");

interrupts();
delay(1000); // Wait for 1 second
//motor1.turn_off();

count++;



}
205 changes: 205 additions & 0 deletions lowsun/lowsun.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,205 @@
#include <ArduinoBLE.h>
#include <ESP32Time.h>
#include <Motor_PID.h>
#define ENCA 2 // YELLOW from polulu
#define ENCB 15 // WHITE from polulu

#define IN2 26 //B1-A
#define IN1 25 //A1-A

ESP32Time rtc;
//ESP32Time rtc(3600); // offset in seconds GMT+1

BLEService service("1ce76320-2d32-41af-b4c4-46836ea7a62a"); // Bluetooth® Low Energy LED Service
BLECharacteristic dateCharacteristic("ad804469-19ec-406a-b949-31ae17e43813", BLERead | BLENotify | BLEWrite, 8);
BLECharacteristic lightCharacteristic("947aad02-c25d-11ed-afa1-0242ac120002", BLERead | BLENotify | BLEWrite , 4);
BLECharacteristic doorCharacteristic("c3773399-b755-4e30-9160-bed203fae718", BLERead | BLENotify | BLEWrite , 2);
BLECharacteristic doorCloseCharacteristic("e011ba0e-84c5-4e83-8648-f3e2660c44b0", BLERead | BLENotify | BLEWrite , 4);
BLECharacteristic doorOpenCharacteristic("cc959fff-4f84-4d08-a720-9d9156a48ed5", BLERead | BLENotify | BLEWrite , 4);

//badge
uint8_t ble_value = 0x0;
int analogValue = 500;
int minValue = analogValue;
int maxValue = analogValue;

//settings door
int doorWantedStatus = 1;
float doorNbTurn = 1;
int target;

//settings close
int doorCloseMode = 0;
int doorCloseLightThreshold = -1;
int doorCloseTimeH = -1;
int doorCloseTImeM = -1;

//settings open
int doorOpenMode = 0;
int doorOpenLightThreshold = -1;
int doorOpenTimeH = -1;
int doorOpenTImeM = -1;

//door controle
int oneTurn = 372;//372
int doorStaus = 1;
float kp = 8;
float kd = 1;
float ki = 0.01;
motor motor1 = motor(ENCA, ENCB, IN1, IN2, 0, 50, 100); // Set upper limit to 100


void setup() {
Serial.begin(115200);
rtc.setTime(1680108200);
while (!Serial);

// begin initialization
if (!BLE.begin()) {

noInterrupts();
Serial.println("starting Bluetooth® Low Energy module failed!");
interrupts();
while (1);
}

// set advertised local name and service UUID:
BLE.setLocalName("COOP-DOOR");
BLE.setAdvertisedService(service);
service.addCharacteristic(dateCharacteristic);
service.addCharacteristic(lightCharacteristic);
service.addCharacteristic(doorCharacteristic);
service.addCharacteristic(doorCloseCharacteristic);
service.addCharacteristic(doorOpenCharacteristic);
BLE.addService(service);
dateCharacteristic.writeValue(0);
lightCharacteristic.writeValue(0);
uint8_t initialValue[] = {10, 1};
doorCharacteristic.writeValue(initialValue, sizeof(initialValue));


// start advertising
BLE.advertise();


noInterrupts();
Serial.println("BLE LED Peripheral");
interrupts();

//door controler
motor1.init(kp, ki, kd);
//motor1.set_position(0);
//motor1.set_target(10);
motor1.turn_off();

}

void loop() {
// listen for Bluetooth® Low Energy peripherals to connect:
BLEDevice central = BLE.central();

// if a central is connected to peripheral:
if (central) {
noInterrupts();
Serial.print("Connected to central: ");
// print the central's MAC address:
Serial.println(central.address());
interrupts();


// while the central is still connected to peripheral:
while (central.connected()) {
// if the remote device wrote to the characteristic,
// use the value to control the LED:
//delay(500);

manageSettingsDoor();




}

// when the central disconnects, print it out:

noInterrupts();
Serial.print(F("Disconnected from central: "));
Serial.println(central.address());
interrupts();
}





}


void manageSettingsDoor() {
if (doorCharacteristic.written() ) {

noInterrupts();
Serial.println("update Door");
Serial.print(doorCharacteristic.value()[0]);
Serial.print(";");
Serial.println(doorCharacteristic.value()[1]);
Serial.println("doorNbTurn");
interrupts();
doorNbTurn = doorCharacteristic.value()[0] / 10;
doorWantedStatus = doorCharacteristic.value()[1];

if (doorWantedStatus == 0 && doorStaus != doorWantedStatus ) {
target = (int) oneTurn * doorNbTurn;
noInterrupts();

Serial.println(target); // * doorNbTurn);
interrupts();

motor1.set_target(target); // * doorNbTurn);
} else if (doorWantedStatus == 1 && doorStaus != doorWantedStatus) {
target = (int) oneTurn * 0;
noInterrupts();

Serial.println(target); // * doorNbTurn*-1 );
interrupts();
motor1.set_target(target); // * doorNbTurn * -1);

}
motor1.start();

while (!motor1.target_reached()) {

motor1.start();

}

delay(1000); // Wait for 1 second
motor1.turn_off();

doorStaus = doorWantedStatus;


} else {
//motor1.start();

}
}


byte* getBytesFromLong(long x ) {
byte* bytes = new byte[8];
for (int i = 0; i < 8; i++) {
bytes[i] = x & 0xff;
x = (x - bytes[i]) / 256;
}
return bytes;

}

long getLongFromBytes(const byte* bytes) {
long result = 0;
for (int i = 7; i >= 0; i--) {
result = (result * 256) + bytes[i];
}
return result;
}
50 changes: 40 additions & 10 deletions risingSun/risingSun.ino
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,10 @@ void setup() {

// begin initialization
if (!BLE.begin()) {
Serial.println("starting Bluetooth® Low Energy module failed!");

noInterrupts();
Serial.println("starting Bluetooth® Low Energy module failed!");
interrupts();
while (1);
}

Expand All @@ -77,7 +80,10 @@ void setup() {
// start advertising
BLE.advertise();

Serial.println("BLE LED Peripheral");

noInterrupts();
Serial.println("BLE LED Peripheral");
interrupts();

//door controler
motor1.init(kp, ki, kd);
Expand All @@ -93,9 +99,12 @@ void loop() {

// if a central is connected to peripheral:
if (central) {
Serial.print("Connected to central: ");
noInterrupts();
Serial.print("Connected to central: ");
// print the central's MAC address:
Serial.println(central.address());
interrupts();


// while the central is still connected to peripheral:
while (central.connected()) {
Expand All @@ -114,8 +123,11 @@ void loop() {
}

// when the central disconnects, print it out:
Serial.print(F("Disconnected from central: "));

noInterrupts();
Serial.print(F("Disconnected from central: "));
Serial.println(central.address());
interrupts();
}


Expand All @@ -126,14 +138,17 @@ void loop() {

void manageSettingsOpen() {
if (doorOpenCharacteristic.written() ) {
Serial.println("update Door Open settings");

noInterrupts();
Serial.println("update Door Open settings");
Serial.print(doorOpenCharacteristic.value()[0]);
Serial.print(";");
Serial.print(doorOpenCharacteristic.value()[1]);
Serial.print(";");
Serial.print(doorOpenCharacteristic.value()[2]);
Serial.print(";");
Serial.println(doorOpenCharacteristic.value()[3]);
interrupts();
doorOpenMode = doorOpenCharacteristic.value()[0];
doorOpenLightThreshold = doorOpenCharacteristic.value()[1];
doorOpenTimeH = doorOpenCharacteristic.value()[2];
Expand All @@ -146,14 +161,17 @@ void manageSettingsOpen() {

void manageSettingsClose() {
if (doorCloseCharacteristic.written() ) {
Serial.println("update Door Close settings");

noInterrupts();
Serial.println("update Door Close settings");
Serial.print(doorCloseCharacteristic.value()[0]);
Serial.print(";");
Serial.print(doorCloseCharacteristic.value()[1]);
Serial.print(";");
Serial.print(doorCloseCharacteristic.value()[2]);
Serial.print(";");
Serial.println(doorCloseCharacteristic.value()[3]);
interrupts();
doorCloseMode = doorCloseCharacteristic.value()[0];
doorCloseLightThreshold = doorCloseCharacteristic.value()[1];
doorCloseTimeH = doorCloseCharacteristic.value()[2];
Expand All @@ -167,18 +185,27 @@ void manageSettingsClose() {

void manageSettingsDoor() {
if (doorCharacteristic.written() ) {
Serial.println("update Door");

noInterrupts();
Serial.println("update Door");
Serial.print(doorCharacteristic.value()[0]);
Serial.print(";");
Serial.println(doorCharacteristic.value()[1]);
interrupts();
doorNbTurn = doorCharacteristic.value()[0] / 10;
doorWantedStatus = doorCharacteristic.value()[1];

if (doorWantedStatus == 0 && doorStaus != doorWantedStatus ) {
noInterrupts();
Serial.println(oneTurn * doorNbTurn); // * doorNbTurn);
interrupts();

motor1.set_target(oneTurn * doorNbTurn); // * doorNbTurn);
} else if (doorWantedStatus == 1 && doorStaus != doorWantedStatus) {

noInterrupts();
Serial.println(oneTurn * 0); // * doorNbTurn*-1 );
interrupts();
motor1.set_target(oneTurn * 0); // * doorNbTurn * -1);

}
Expand Down Expand Up @@ -243,12 +270,15 @@ void manageLight() {

void manageDate() {
if (dateCharacteristic.written()) {
Serial.println("Date update");

long xx = getLongFromBytes(dateCharacteristic.value());
Serial.println(xx);

rtc.setTime(xx);
noInterrupts();
Serial.println("Date update");
Serial.println(xx);
Serial.println(rtc.getEpoch());

interrupts();

} else {
//Serial.println(rtc.getEpoch());
Expand Down
Loading

0 comments on commit a979ad1

Please sign in to comment.