Skip to content

Commit c369670

Browse files
committed
Trying to make a Linear Illumination LED pwm.
1 parent db4ba4e commit c369670

File tree

4 files changed

+40
-40
lines changed

4 files changed

+40
-40
lines changed

app_httpd.cpp

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,11 @@
2020
#include "camera_index_ov2640.h"
2121
#include "camera_index_ov3660.h"
2222

23-
// Globals (from main .ino) needed to set Led and Lamp levels
24-
extern long int lampVal;
25-
extern int lampChannel;
23+
// Function+Globals needed for led and Lamp levels
2624
void flashLED(int flashtime);
25+
extern int lampVal; // The current Lamp value
26+
extern int lampChannel; // PWM channel Lamp is attached to
27+
extern float lampR; // The R value in the graph equation
2728

2829
#include "fb_gfx.h"
2930
#include "fd_forward.h"
@@ -531,14 +532,17 @@ static esp_err_t cmd_handler(httpd_req_t *req){
531532
detection_enabled = val;
532533
}
533534
}
534-
else if(!strcmp(variable, "lamp")) {
535+
else if(!strcmp(variable, "lamp") && (lampVal != -1)) {
535536
Serial.print("Lamp: ");
536537
lampVal = val;
537538
if (lampVal > 100) lampVal = 100; // normalise 0-255 (pwm range) just in case..
538539
if (lampVal < 0 ) lampVal = 0;
539-
ledcWrite(lampChannel, lampVal * 2.55); // Add s exponential map for 0-100 vs pwm value here.
540+
// https://diarmuid.ie/blog/pwm-exponential-led-fading-on-arduino-or-other-platforms
541+
int brightness = pow (2, (lampVal / lampR)) - 1;
542+
ledcWrite(lampChannel, brightness);
540543
Serial.print(lampVal);
541-
Serial.println("%");
544+
Serial.print("%, pwm = ");
545+
Serial.println(brightness);
542546
}
543547
else {
544548
res = -1;
@@ -562,7 +566,6 @@ static esp_err_t status_handler(httpd_req_t *req){
562566
char * p = json_response;
563567
*p++ = '{';
564568

565-
p+=sprintf(p, "\"Lamp\":%u", lampVal);
566569
p+=sprintf(p, "\"framesize\":%u,", s->status.framesize);
567570
p+=sprintf(p, "\"quality\":%u,", s->status.quality);
568571
p+=sprintf(p, "\"brightness\":%d,", s->status.brightness);
@@ -591,6 +594,7 @@ static esp_err_t status_handler(httpd_req_t *req){
591594
p+=sprintf(p, "\"face_detect\":%u,", detection_enabled);
592595
p+=sprintf(p, "\"face_enroll\":%u,", is_enrolling);
593596
p+=sprintf(p, "\"face_recognize\":%u", recognition_enabled);
597+
if (lampVal != -1) p+=sprintf(p, "\"Lamp\":%u", lampVal);
594598
*p++ = '}';
595599
*p++ = 0;
596600
httpd_resp_set_type(req, "application/json");

camera_pins.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,8 @@
9595
#define PCLK_GPIO_NUM 22
9696

9797
#define LED_PIN 33 // Status led
98-
#define LED_ON LOW // Inverted
99-
#define LED_OFF HIGH
98+
#define LED_ON LOW // - Pin is inverted.
99+
#define LED_OFF HIGH //
100100
#define LAMP_PIN 4 // LED FloodLamp.
101101

102102
#else

esp32-cam-webserver.ino

Lines changed: 26 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -18,49 +18,48 @@
1818
//#define CAMERA_MODULE_OV3660
1919

2020
#if __has_include("mywifi.h")
21-
// I keep my settings in a seperate header file that is in my .gitignore file
21+
// I keep my settings in a seperate header file
2222
#include "mywifi.h"
2323
#else
24-
// OWEN: todo
25-
// Leave as is to create the default accesspoint, or comment out ACCESSPOINT
26-
// line and supply your own network SSID and PASSWORD.
27-
//bool accessPoint = true;
28-
//const char ap_ssid[] = "ESP-CAM-SERVER";
29-
//const char ap_password[] = "ESP-CAM-DEMO";
3024
const char* ssid = "my-access-point-ssid";
3125
const char* password = "my-access-point-password";
3226
#endif
3327

3428
#include "camera_pins.h"
3529

3630
// Status and illumination LED's
37-
#ifdef LAMP_PIN // Do we have a LED Illumination Lamp?
38-
const int lampPin = LAMP_PIN;
31+
#ifdef LAMP_PIN
32+
int lampVal = 0; // Current Lamp value, range 0-100, Start off
3933
#else
40-
const int lampPin = -1;
41-
#endif
42-
int lampVal = 0; // (range 0-100) Start off
43-
int lampChannel = 7; // a free PWM channel (some channels used by camera)
44-
const int pwmfreq = 50000; // 50K pwm frequency
45-
const int pwmresolution = 8; // duty cycle has 8 bit range
46-
34+
int lampVal = -1; // disable Lamp
35+
#endif
36+
int lampChannel = 7; // a free PWM channel (some channels used by camera)
37+
const int pwmfreq = 50000; // 50K pwm frequency
38+
const int pwmresolution = 8; // duty cycle has 8 bit range
39+
// https://diarmuid.ie/blog/pwm-exponential-led-fading-on-arduino-or-other-platforms
40+
const int pwmIntervals = 100; // The number of Steps between the output being on and off
41+
float lampR; // The R value in the PWM graph equation (calculated in setup)
42+
4743
void startCameraServer();
4844

4945
void setup() {
5046
Serial.begin(115200);
51-
Serial.setDebugOutput(true); // OWEN: Change?
47+
Serial.setDebugOutput(true);
5248
Serial.println();
5349

54-
#ifdef LED_PIN
50+
#ifdef LED_PIN // If we have notification LED, set it to output
5551
pinMode(LED_PIN, OUTPUT);
5652
digitalWrite(LED_PIN, LED_OFF);
5753
#endif
58-
59-
if (lampPin != -1) {
60-
ledcSetup(lampChannel, pwmfreq, pwmresolution); // configure LED PWM channel
61-
ledcWrite(lampChannel, lampVal); // set initial value
62-
ledcAttachPin(LAMP_PIN, lampChannel); // attach the GPIO pin to the channel
63-
}
54+
55+
#ifdef LAMP_PIN
56+
ledcSetup(lampChannel, pwmfreq, pwmresolution); // configure LED PWM channel
57+
ledcWrite(lampChannel, lampVal); // set initial value
58+
ledcAttachPin(LAMP_PIN, lampChannel); // attach the GPIO pin to the channel
59+
// Calculate the PWM scaling R factor:
60+
// https://diarmuid.ie/blog/pwm-exponential-led-fading-on-arduino-or-other-platforms
61+
lampR = (pwmIntervals * log10(2))/(log10(255));
62+
#endif
6463

6564
camera_config_t config;
6665
config.ledc_channel = LEDC_CHANNEL_0;
@@ -124,8 +123,7 @@ void setup() {
124123
WiFi.begin(ssid, password);
125124

126125
while (WiFi.status() != WL_CONNECTED) { // Owen, pulse LED for this.
127-
delay(500);
128-
Serial.print(".");
126+
delay(250);
129127
}
130128

131129
// feedback that we are connected
@@ -147,12 +145,12 @@ void setup() {
147145

148146
void flashLED(int flashtime)
149147
{
150-
#ifdef LED_PIN // If we have it, flash it.
148+
#ifdef LED_PIN // Notification LED; If we have it; flash it.
151149
digitalWrite(LED_PIN, LED_ON); // On at full power.
152150
delay(flashtime); // delay
153151
digitalWrite(LED_PIN, LED_OFF); // turn Off
154152
#else
155-
return; // or nothing
153+
return; // No notifcation LED, do nothing
156154
#endif
157155
}
158156

mywifi.sample.h

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
// Rename this example to 'mywifi.h' and fill in your details.
2+
// This is in the '.gitignore' file, which helps to keep details secret.
23

3-
//bool accessPoint = true;
4-
//const char ap_ssid[] = "ESP-CAM-SERVER";
5-
//const char ap_password[] = "ESP-CAM-DEMO";
64
const char* ssid = "my-access-point-ssid";
75
const char* password = "my-access-point-password";

0 commit comments

Comments
 (0)