Code issues

dumDumPerson
Posts: 1
Joined: Fri Jul 14, 2023 6:07 am

Code issues

Postby dumDumPerson » Fri Jul 14, 2023 6:46 am

Hi, I am fairly new to creating projects with esp-boards, though I have some experience with Arduinos. I wanted to gift my girlfriend a nice birthday present, a little pet robot. It's supposed to look at faces and show a blinking eye on the screen. I followed an instruction on instructables ( https://www.instructables.com/WatchEye/ ) but I used a different screen. Instead of the Adafruit LED Backpack I used an LED Matrix and a shift register. I tried around for a bit but it doesn't seem to be working. :?

Does this line "LedControl lc = LedControl(14, 15, 16, 1); // Pins: DIN,CLK,CS, # of Display connected" even work like that? :?:

Any help would be greatly appreciated, I included the code below!

Best regards

dumDumPerson
  1. /*
  2.    WatchEye - One-eyed Pet With Face Tracking
  3.    ==========================================
  4.    Markus opitz 2022
  5.    www.instructables.com
  6. */
  7.  
  8.  
  9. // Camera
  10. #include "camera_index.h"
  11. #include "fb_gfx.h"
  12. #include "esp_camera.h"
  13. #include "fd_forward.h"
  14. #include "soc/rtc_cntl_reg.h"  //disable brownout problems
  15. #include "soc/soc.h"           //disable brownout problems
  16.  
  17. // LED matrix
  18. #include "LedControl.h"
  19.  
  20. LedControl lc = LedControl(14, 15, 16, 1); // Pins: DIN,CLK,CS, # of Display connected
  21. unsigned long delayTime = 400; // Delay between Frames
  22. // Put values in arrays
  23.  
  24. byte one[] =
  25. {
  26.   B00011000,
  27.   B00111100,
  28.   B01111110,
  29.   B11000011,
  30.   B11000011,
  31.   B01111110,
  32.   B00111100,
  33.   B00011000
  34. };
  35. byte two[] =
  36. {
  37.   B00000000,
  38.   B00000000,
  39.   B01111110,
  40.   B11100111,
  41.   B11100111,
  42.   B01111110,
  43.   B00000000,
  44.   B00000000
  45. };
  46. byte three[] =
  47. {
  48.   B00000000,
  49.   B00000000,
  50.   B00011000,
  51.   B11111111,
  52.   B11111111,
  53.   B00011000,
  54.   B00000000,
  55.   B00000000
  56. };
  57. byte four[] =
  58. {
  59.   B00000000,
  60.   B00000000,
  61.   B01111110,
  62.   B11100111,
  63.   B11100111,
  64.   B01111110,
  65.   B00000000,
  66.   B00000000
  67. };
  68.  
  69.  
  70. // Arduino like analogWrite  === SERVOS =================
  71. // value has to be between 0 and valueMax
  72. void ledcAnalogWrite(uint8_t channel, uint32_t value, uint32_t valueMax = 180)
  73. {
  74.     // calculate duty, 8191 from 2 ^ 13 - 1
  75.     uint32_t duty = (8191 / valueMax) * min(value, valueMax);
  76.     ledcWrite(channel, duty);
  77. }
  78.  
  79. int pan_center = 90; // center the pan servo
  80. int tilt_center = 90; // center the tilt servo
  81. RTC_DATA_ATTR int pan_center_old;  //remember old value in RTC
  82. int i;
  83. unsigned long runTime, newTime;
  84.  
  85. // ===== CAMERA_MODEL_AI_THINKER =========
  86. #define PWDN_GPIO_NUM     32
  87. #define RESET_GPIO_NUM    -1
  88. #define XCLK_GPIO_NUM      0
  89. #define SIOD_GPIO_NUM     26
  90. #define SIOC_GPIO_NUM     27
  91. #define Y9_GPIO_NUM       35
  92. #define Y8_GPIO_NUM       34
  93. #define Y7_GPIO_NUM       39
  94. #define Y6_GPIO_NUM       36
  95. #define Y5_GPIO_NUM       21
  96. #define Y4_GPIO_NUM       19
  97. #define Y3_GPIO_NUM       18
  98. #define Y2_GPIO_NUM        5
  99. #define VSYNC_GPIO_NUM    25
  100. #define HREF_GPIO_NUM     23
  101. #define PCLK_GPIO_NUM     22
  102.  
  103.  
  104. bool initCamera() {
  105.     camera_config_t config;
  106.     config.ledc_channel = LEDC_CHANNEL_0;
  107.     config.ledc_timer = LEDC_TIMER_0;
  108.     config.pin_d0 = Y2_GPIO_NUM;
  109.     config.pin_d1 = Y3_GPIO_NUM;
  110.     config.pin_d2 = Y4_GPIO_NUM;
  111.     config.pin_d3 = Y5_GPIO_NUM;
  112.     config.pin_d4 = Y6_GPIO_NUM;
  113.     config.pin_d5 = Y7_GPIO_NUM;
  114.     config.pin_d6 = Y8_GPIO_NUM;
  115.     config.pin_d7 = Y9_GPIO_NUM;
  116.     config.pin_xclk = XCLK_GPIO_NUM;
  117.     config.pin_pclk = PCLK_GPIO_NUM;
  118.     config.pin_vsync = VSYNC_GPIO_NUM;
  119.     config.pin_href = HREF_GPIO_NUM;
  120.     config.pin_sscb_sda = SIOD_GPIO_NUM;
  121.     config.pin_sscb_scl = SIOC_GPIO_NUM;
  122.     config.pin_pwdn = PWDN_GPIO_NUM;
  123.     config.pin_reset = RESET_GPIO_NUM;
  124.     config.xclk_freq_hz = 20000000;
  125.     config.pixel_format = PIXFORMAT_JPEG;
  126.     /*
  127.       XGA(1024x768)
  128.       SVGA(800x600)
  129.       VGA(640x480)
  130.       CIF(400x296)
  131.       QVGA(320x240)
  132.       HQVGA(240x176)
  133.       QQVGA(160x120)
  134.     */
  135.     if (psramFound()) {
  136.         config.frame_size = FRAMESIZE_VGA;
  137.         config.jpeg_quality = 10;
  138.         config.fb_count = 2;
  139.     }
  140.     else {
  141.         config.frame_size = FRAMESIZE_CIF;
  142.         config.jpeg_quality = 12;
  143.         config.fb_count = 1;
  144.     }
  145.  
  146.     esp_err_t result = esp_camera_init(&config);
  147.  
  148.     if (result != ESP_OK) {
  149.         return false;
  150.     }
  151.  
  152.     return true;
  153. }
  154.  
  155. mtmn_config_t mtmn_config = { 0 };
  156. int detections = 0;
  157. int counter = 0;
  158.  
  159.  
  160. void setup() {   // ================ setup =====================
  161.  
  162.     WRITE_PERI_REG(RTC_CNTL_BROWN_OUT_REG, 0); //disable brownout detector
  163.     Serial.begin(115200);
  164.  
  165.     esp_sleep_enable_ext0_wakeup(GPIO_NUM_2, 1);
  166.  
  167.     // LED matrix
  168.     lc.shutdown(0, false); // Wake up display
  169.     lc.setIntensity(0, 5); // Set intensity level
  170.     lc.clearDisplay(0); // Clear Displays
  171.  
  172.     // Ai-Thinker: pins 13 and 12 ===== Servos =====
  173.     ledcSetup(2, 50, 16); //channel, freq, resolution
  174.     ledcAttachPin(13, 2); // pin, channel
  175.  
  176.     ledcSetup(4, 50, 16);
  177.     ledcAttachPin(12, 4);
  178.     delay(5);
  179.     ledcAnalogWrite(2, 110); // pan   start position
  180.     ledcAnalogWrite(4, 90);  // tilt
  181.     delay(500);
  182.  
  183.  
  184.     if (!initCamera()) {
  185.         Serial.printf("Failed to initialize camera...");
  186.         return;
  187.     }
  188.  
  189.     mtmn_config = mtmn_init_config();
  190.     Serial.println("Setup complete");
  191.  
  192. } // ================ setup end =====================
  193.  
  194.  
  195.  
  196.  
  197.  
  198. static void draw_face_boxes(dl_matrix3du_t* image_matrix, box_array_t* boxes)
  199. {
  200.     int x, y, w, h, i, half_width, half_height;
  201.     fb_data_t fb;
  202.     fb.width = image_matrix->w;
  203.     fb.height = image_matrix->h;
  204.     fb.data = image_matrix->item;
  205.     fb.bytes_per_pixel = 3;
  206.     fb.format = FB_BGR888;
  207.  
  208.     for (i = 0; i < boxes->len; i++) {
  209.  
  210.         // Convoluted way of finding face centre...
  211.         x = ((int)boxes->box[i].box_p[0]);
  212.         w = (int)boxes->box[i].box_p[2] - x + 1;
  213.         half_width = w / 2;
  214.         int face_center_pan = x + half_width; // image frame face centre x co-ordinate
  215.  
  216.         y = (int)boxes->box[i].box_p[1];
  217.         h = (int)boxes->box[i].box_p[3] - y + 1;
  218.         half_height = h / 2;
  219.         int face_center_tilt = y + half_height;  // image frame face centre y co-ordinate
  220.  
  221.         //    assume QVGA 320x240         CIF(400x296)              VGA(640x480)        SVGA(800x600)
  222.         //    int sensor_width = 320;         400                     640                   800
  223.         //    int sensor_height = 240;        296                     480                   600
  224.         //    int lens_fov = 45               45                      45                    45
  225.         //    float diagonal = sqrt(sq(sensor_width) + sq(sensor_height)); // pixels along the diagonal
  226.         //    float pixels_per_degree = diagonal / lens_fov;
  227.         //    400/45 = 8.89                   498/45 = 11.05       800/45 = 17.78     1000/45 = 22.22
  228.  
  229.         // QVGA 320x240
  230.         //  float move_to_x = pan_center - ((-160 + face_center_pan) / 8.89 -5) ;     // -5    correct that the lens is not centered
  231.         //  float move_to_y = tilt_center + ((-120 + face_center_tilt) / 8.89) ;
  232.  
  233.         // CIF(400x296)
  234.         //  float move_to_x = pan_center - ((-200 + face_center_pan) / 11.05 -5) ;
  235.         //  float move_to_y = tilt_center + ((-148 + face_center_tilt) / 11.05) ;
  236.  
  237.         // VGA(640x480)
  238.         float move_to_x = pan_center - ((-320 + face_center_pan) / 17.78 - 5);
  239.         float move_to_y = tilt_center + ((-240 + face_center_tilt) / 17.78);
  240.  
  241.         // SVGA(800x600)
  242.         // float move_to_x = pan_center - ((-400 + face_center_pan) / 22.22 -5) ;
  243.         // float move_to_y = tilt_center + ((-300 + face_center_tilt) / 22.22) ;
  244.  
  245.  
  246.         Serial.print("box is drawn      ");
  247.         pan_center = (pan_center + move_to_x) / 2;
  248.         //pan_center = 180 - pan_center;
  249.         if ((pan_center_old - pan_center < 10) && (pan_center_old - pan_center > -10)) {
  250.             sone();
  251.             delay(300);
  252.             stwo();
  253.             delay(300);
  254.             sthree();
  255.             delay(300);
  256.             sfour();
  257.             delay(300);
  258.         }
  259.  
  260.  
  261.         pan_center_old = pan_center;  // save servo position
  262.  
  263.         Serial.print(pan_center); Serial.print("    x "); Serial.print(move_to_x); Serial.print("   ");
  264.         ledcAnalogWrite(2, pan_center); // channel, 0-180
  265.         delay(2);
  266.  
  267.         tilt_center = (tilt_center + move_to_y) / 2;
  268.         int reversed_tilt_center = map(tilt_center, 60, 130, 130, 60);
  269.         reversed_tilt_center = 180 - reversed_tilt_center;
  270.         Serial.print(reversed_tilt_center); Serial.print("    y "); Serial.print(move_to_y);
  271.         Serial.println("");
  272.         ledcAnalogWrite(4, reversed_tilt_center); // channel, 50-140
  273.         delay(2);
  274.     }
  275. }
  276.  
  277. void sone()
  278. {
  279.     for (int i = 0; i < 8; i++)
  280.     {
  281.         lc.setRow(0, i, one[i]);
  282.     }
  283. }
  284. void stwo()
  285. {
  286.     for (int i = 0; i < 8; i++)
  287.     {
  288.         lc.setRow(0, i, two[i]);
  289.     }
  290. }
  291. void sthree()
  292. {
  293.     for (int i = 0; i < 8; i++)
  294.     {
  295.         lc.setRow(1, i, three[i]);
  296.     }
  297. }
  298. void sfour()
  299. {
  300.     for (int i = 0; i < 8; i++)
  301.     {
  302.         lc.setRow(1, i, four[i]);
  303.     }
  304. }
  305. void loop() {
  306.  
  307.     // camera and servos ==========================
  308.     camera_fb_t* frame;
  309.     frame = esp_camera_fb_get();
  310.  
  311.     dl_matrix3du_t* image_matrix = dl_matrix3du_alloc(1, frame->width, frame->height, 3);
  312.     fmt2rgb888(frame->buf, frame->len, frame->format, image_matrix->item);
  313.  
  314.     esp_camera_fb_return(frame);
  315.  
  316.     box_array_t* boxes = face_detect(image_matrix, &mtmn_config);
  317.  
  318.     // if there is no face, look for it
  319.  
  320.     if (boxes != NULL) {
  321.         Serial.printf("Faces detected");
  322.         draw_face_boxes(image_matrix, boxes);  // draw boxes and move servos
  323.         runTime = millis();
  324.     }
  325.     newTime = millis();
  326.     if (newTime - runTime > 15000) {   // no action for more than 15 sec --> sleep;    15000 = 15sec
  327.         ledcAnalogWrite(2, 110); // pan,  channel, 0-180
  328.         ledcAnalogWrite(4, 110); // tilt
  329.         delay(200);
  330.         stwo();
  331.         delay(200);
  332.         sthree();
  333.  
  334.  
  335.         esp_deep_sleep_start();
  336.     }
  337.  
  338.  
  339.     dl_matrix3du_free(image_matrix);
  340.  
  341.     delay(1);
  342. }
  1. /*
  2.  * WatchEye - One-eyed Pet With Face Tracking
  3.  * ==========================================
  4.  * Markus opitz 2022
  5.  * www.instructables.com
  6.  */
  7.  
  8.  
  9. // Camera
  10. #include "camera_index.h"
  11. #include "fb_gfx.h"
  12. #include "esp_camera.h"
  13. #include "fd_forward.h"
  14. #include "soc/rtc_cntl_reg.h"  //disable brownout problems
  15. #include "soc/soc.h"           //disable brownout problems
  16.  
  17. // LED matrix
  18. #include <Wire.h>
  19. #include "Adafruit_LEDBackpack.h"
  20. #include "Adafruit_GFX.h"
  21. #include "blink.h"
  22. Adafruit_8x8matrix matrix = Adafruit_8x8matrix();
  23.     uint8_t
  24.     blinkIndex[] = {
  25.       1, 2, 3, 4, 3, 2, 1 },
  26.     // Blink bitmap sequence
  27.     blinkCountdown = 100, // 100    Countdown to next blink (in frames)
  28.     gazeCountdown  =  75, // 75    Countdown to next eye movement
  29.     gazeFrames     =  50; // 50    Duration of eye movement (smaller = faster)
  30.     int8_t
  31.     eyeX = 3, eyeY = 3,   // Current eye position
  32.     newX = 3, newY = 3,   // Next eye position
  33.     dX   = 0, dY   = 0;   // Distance from prior to new position
  34.  
  35.  
  36.  
  37.  
  38. // Arduino like analogWrite  === SERVOS =================
  39. // value has to be between 0 and valueMax
  40. void ledcAnalogWrite(uint8_t channel, uint32_t value, uint32_t valueMax = 180)
  41. {
  42.   // calculate duty, 8191 from 2 ^ 13 - 1
  43.   uint32_t duty = (8191 / valueMax) * min(value, valueMax);
  44.   ledcWrite(channel, duty);
  45. }
  46.  
  47. int pan_center = 90; // center the pan servo
  48. int tilt_center = 90; // center the tilt servo
  49. RTC_DATA_ATTR int pan_center_old;  //remember old value in RTC
  50. int i;
  51. unsigned long runTime, newTime;
  52.  
  53. // ===== CAMERA_MODEL_AI_THINKER =========
  54. #define PWDN_GPIO_NUM     32
  55. #define RESET_GPIO_NUM    -1
  56. #define XCLK_GPIO_NUM      0
  57. #define SIOD_GPIO_NUM     26
  58. #define SIOC_GPIO_NUM     27
  59. #define Y9_GPIO_NUM       35
  60. #define Y8_GPIO_NUM       34
  61. #define Y7_GPIO_NUM       39
  62. #define Y6_GPIO_NUM       36
  63. #define Y5_GPIO_NUM       21
  64. #define Y4_GPIO_NUM       19
  65. #define Y3_GPIO_NUM       18
  66. #define Y2_GPIO_NUM        5
  67. #define VSYNC_GPIO_NUM    25
  68. #define HREF_GPIO_NUM     23
  69. #define PCLK_GPIO_NUM     22
  70.  
  71.  
  72. bool initCamera() {
  73.   camera_config_t config;
  74.   config.ledc_channel = LEDC_CHANNEL_0;
  75.   config.ledc_timer = LEDC_TIMER_0;
  76.   config.pin_d0 = Y2_GPIO_NUM;
  77.   config.pin_d1 = Y3_GPIO_NUM;
  78.   config.pin_d2 = Y4_GPIO_NUM;
  79.   config.pin_d3 = Y5_GPIO_NUM;
  80.   config.pin_d4 = Y6_GPIO_NUM;
  81.   config.pin_d5 = Y7_GPIO_NUM;
  82.   config.pin_d6 = Y8_GPIO_NUM;
  83.   config.pin_d7 = Y9_GPIO_NUM;
  84.   config.pin_xclk = XCLK_GPIO_NUM;
  85.   config.pin_pclk = PCLK_GPIO_NUM;
  86.   config.pin_vsync = VSYNC_GPIO_NUM;
  87.   config.pin_href = HREF_GPIO_NUM;
  88.   config.pin_sscb_sda = SIOD_GPIO_NUM;
  89.   config.pin_sscb_scl = SIOC_GPIO_NUM;
  90.   config.pin_pwdn = PWDN_GPIO_NUM;
  91.   config.pin_reset = RESET_GPIO_NUM;
  92.   config.xclk_freq_hz = 20000000;
  93.   config.pixel_format = PIXFORMAT_JPEG;
  94.               /*
  95.       XGA(1024x768)
  96.       SVGA(800x600)
  97.       VGA(640x480)
  98.       CIF(400x296)
  99.       QVGA(320x240)
  100.       HQVGA(240x176)
  101.       QQVGA(160x120)
  102.      */
  103.   if (psramFound()) {
  104.     config.frame_size = FRAMESIZE_VGA;
  105.     config.jpeg_quality = 10;
  106.     config.fb_count = 2;
  107.   } else {
  108.     config.frame_size = FRAMESIZE_CIF;
  109.     config.jpeg_quality = 12;
  110.     config.fb_count = 1;
  111.   }
  112.  
  113.   esp_err_t result = esp_camera_init(&config);
  114.  
  115.   if (result != ESP_OK) {
  116.     return false;
  117.   }
  118.  
  119.   return true;
  120. }
  121.  
  122. mtmn_config_t mtmn_config = {0};
  123. int detections = 0;
  124. int counter = 0;
  125.  
  126.  
  127. void setup() {   // ================ setup =====================
  128.  
  129.   WRITE_PERI_REG(RTC_CNTL_BROWN_OUT_REG, 0); //disable brownout detector
  130.   Serial.begin(115200);
  131.  
  132.   esp_sleep_enable_ext0_wakeup(GPIO_NUM_2, 1);
  133.    
  134. // LED matrix
  135.   Wire.begin(14, 15);//SDA, SCL = GPIO14, GPIO15
  136.       matrix.begin(0x70);
  137.       matrix.setRotation(3);
  138.       matrix.setBrightness(4);
  139.       matrix.setTextColor(LED_ON);
  140.       matrix.clear();
  141.       matrix.writeDisplay();
  142.   blinkblink();      
  143.  
  144. // Ai-Thinker: pins 13 and 12 ===== Servos =====
  145.   ledcSetup(2, 50, 16); //channel, freq, resolution
  146.   ledcAttachPin(13, 2); // pin, channel
  147.  
  148.   ledcSetup(4, 50, 16);
  149.   ledcAttachPin(12, 4);
  150.   delay(5);
  151.   ledcAnalogWrite(2, 110); // pan   start position
  152.   ledcAnalogWrite(4, 90);  // tilt
  153.   delay(500);
  154.  
  155.  
  156.   if (!initCamera()) {
  157.     Serial.printf("Failed to initialize camera...");
  158.     return;
  159.   }
  160.  
  161.   mtmn_config = mtmn_init_config();
  162.   Serial.println("Setup complete");
  163.  
  164. } // ================ setup end =====================
  165.  
  166.  
  167.  
  168.  
  169. static void draw_face_boxes(dl_matrix3du_t *image_matrix, box_array_t *boxes)
  170. {
  171.   int x, y, w, h, i, half_width, half_height;
  172.   fb_data_t fb;
  173.   fb.width = image_matrix->w;
  174.   fb.height = image_matrix->h;
  175.   fb.data = image_matrix->item;
  176.   fb.bytes_per_pixel = 3;
  177.   fb.format = FB_BGR888;
  178.  
  179.   for (i = 0; i < boxes->len; i++) {
  180.  
  181.     // Convoluted way of finding face centre...
  182.     x = ((int)boxes->box[i].box_p[0]);
  183.     w = (int)boxes->box[i].box_p[2] - x + 1;
  184.     half_width = w / 2;
  185.     int face_center_pan = x + half_width; // image frame face centre x co-ordinate
  186.  
  187.     y = (int)boxes->box[i].box_p[1];
  188.     h = (int)boxes->box[i].box_p[3] - y + 1;
  189.     half_height = h / 2;
  190.     int face_center_tilt = y + half_height;  // image frame face centre y co-ordinate
  191.  
  192.     //    assume QVGA 320x240         CIF(400x296)              VGA(640x480)        SVGA(800x600)
  193.     //    int sensor_width = 320;         400                     640                   800
  194.     //    int sensor_height = 240;        296                     480                   600
  195.     //    int lens_fov = 45               45                      45                    45
  196.     //    float diagonal = sqrt(sq(sensor_width) + sq(sensor_height)); // pixels along the diagonal
  197.     //    float pixels_per_degree = diagonal / lens_fov;
  198.     //    400/45 = 8.89                   498/45 = 11.05       800/45 = 17.78     1000/45 = 22.22
  199.  
  200.    // QVGA 320x240
  201.   //  float move_to_x = pan_center - ((-160 + face_center_pan) / 8.89 -5) ;     // -5    correct that the lens is not centered
  202.   //  float move_to_y = tilt_center + ((-120 + face_center_tilt) / 8.89) ;
  203.  
  204.    // CIF(400x296)
  205.   //  float move_to_x = pan_center - ((-200 + face_center_pan) / 11.05 -5) ;
  206.   //  float move_to_y = tilt_center + ((-148 + face_center_tilt) / 11.05) ;
  207.  
  208.    // VGA(640x480)
  209.     float move_to_x = pan_center - ((-320 + face_center_pan) / 17.78 -5) ;
  210.     float move_to_y = tilt_center + ((-240 + face_center_tilt) / 17.78) ;
  211.    
  212.    // SVGA(800x600)
  213.   // float move_to_x = pan_center - ((-400 + face_center_pan) / 22.22 -5) ;
  214.   // float move_to_y = tilt_center + ((-300 + face_center_tilt) / 22.22) ;
  215.    
  216.    
  217.     Serial.print("box is drawn      ");
  218.     pan_center = (pan_center + move_to_x) / 2 ;
  219.     //pan_center = 180 - pan_center;
  220.     if ((pan_center_old - pan_center < 10) && (pan_center_old - pan_center > -10)){
  221.       blinkblink();
  222.     }
  223.  
  224.    
  225.     pan_center_old = pan_center;  // save servo position
  226.    
  227.     Serial.print(pan_center); Serial.print("    x "); Serial.print(move_to_x);Serial.print("   ");
  228.     ledcAnalogWrite(2, pan_center); // channel, 0-180
  229.     delay(2);
  230.    
  231.     tilt_center = (tilt_center + move_to_y) / 2;
  232.     int reversed_tilt_center = map(tilt_center, 60, 130, 130, 60);
  233.     reversed_tilt_center = 180 - reversed_tilt_center;
  234.     Serial.print(reversed_tilt_center); Serial.print("    y "); Serial.print(move_to_y);
  235.     Serial.println("");
  236.     ledcAnalogWrite(4, reversed_tilt_center); // channel, 50-140
  237.     delay(2);
  238.   }
  239. }
  240.  
  241.  
  242. void loop() {
  243.        
  244. // camera and servos ==========================  
  245.   camera_fb_t * frame;
  246.   frame = esp_camera_fb_get();
  247.  
  248.   dl_matrix3du_t *image_matrix = dl_matrix3du_alloc(1, frame->width, frame->height, 3);
  249.   fmt2rgb888(frame->buf, frame->len, frame->format, image_matrix->item);
  250.  
  251.   esp_camera_fb_return(frame);
  252.  
  253.   box_array_t *boxes = face_detect(image_matrix, &mtmn_config);
  254.  
  255.   // if there is no face, look for it
  256.   while (boxes == NULL) {
  257.     Serial.printf("look for faces");    
  258.     // add a searching modus here
  259.     // searching();
  260.   }
  261.  
  262.   if (boxes != NULL) {
  263.     Serial.printf("Faces detected");
  264.     draw_face_boxes(image_matrix, boxes);  // draw boxes and move servos
  265.     runTime = millis();
  266.   }
  267.   newTime = millis();
  268.   if (newTime-runTime > 15000) {   // no action for more than 15 sec --> sleep;    15000 = 15sec
  269.     ledcAnalogWrite(2, 110); // pan,  channel, 0-180
  270.     ledcAnalogWrite(4, 110); // tilt
  271.     matrix.clear();
  272.     matrix.writeDisplay();
  273.     delay(100);
  274.    
  275.     esp_deep_sleep_start();
  276.   }
  277.  
  278.  
  279.   dl_matrix3du_free(image_matrix);
  280.  
  281. // camera and servos === END ========================  
  282.  
  283.    delay(1);
  284. }
  285.  
  286.  
  287. void blinkblink()
  288. {
  289. // eye + blink =======================
  290.        for (int i = 0; i < 28; i++) {
  291.        
  292.        matrix.clear();
  293.         matrix.drawBitmap(0, 0,
  294.         blinkImg[
  295.           (blinkCountdown < sizeof(blinkIndex)) ? // Currently blinking?
  296.         blinkIndex[blinkCountdown] :            // Yes, look up bitmap #
  297.         0                                       // No, show bitmap 0
  298.         ], 8, 8, LED_ON);
  299.  
  300.         // Decrement blink counter.  At end, set random time for next blink.
  301.         if(--blinkCountdown == 0) blinkCountdown = random(5, 180);
  302.      
  303.         // Add a pupil (2x2 black square) atop the blinky eyeball bitmap.
  304.         // Periodically, the pupil moves to a new position...
  305.         if(--gazeCountdown <= gazeFrames) {
  306.           // Eyes are in motion - draw pupil at interim position
  307.           matrix.fillRect(
  308.              newX - (dX * gazeCountdown / gazeFrames),
  309.              newY - (dY * gazeCountdown / gazeFrames),
  310.              2, 2, LED_OFF);
  311.           if(gazeCountdown == 0) {    // Last frame?
  312.  
  313.             eyeX = newX;
  314.             eyeY = newY; // Yes.  What's new is old, then...
  315.             do { // Pick random positions until one is within the eye circle
  316.               newX = random(7);
  317.               newY = random(7);
  318.               dX   = newX - 3;  
  319.               dY   = newY - 3;
  320.             }
  321.             while((dX * dX + dY * dY) >= 10);      // Thank you Pythagoras
  322.             dX            = newX - eyeX;             // Horizontal distance to move
  323.             dY            = newY - eyeY;             // Vertical distance to move
  324.             gazeFrames    = random(5, 20);           // Duration of eye movement
  325.             gazeCountdown = random(gazeFrames, 120); // Count to end of next movement
  326.           }
  327.         }
  328.         else {
  329.           // Not in motion yet -- draw pupil at current static position
  330.           matrix.fillRect(eyeX, eyeY, 2, 2, LED_OFF);
  331.         }
  332.         delay(5);
  333.         matrix.writeDisplay();
  334.      }
  335. }
  336. void searching(void) {
  337. }

ESP_Sprite
Posts: 9727
Joined: Thu Nov 26, 2015 4:08 am

Re: Code issues

Postby ESP_Sprite » Mon Jul 17, 2023 2:18 am

Moving Showcase -> Arduino subforum.

Who is online

Users browsing this forum: No registered users and 51 guests