cute_useless_robot.ino 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /*
  2. Cute Useless Robot (´・ω・`)
  3. Developed by tobychui
  4. Idea come from Kairoshi
  5. Board Settings:
  6. ESP32 v2.014
  7. -> ESP32 Dev Module
  8. -> CPU Freq: 240Mhz
  9. -> Arduino & Events run on: Core 1
  10. -> Flash Mode: DIO
  11. */
  12. /* Libraries */
  13. #include <MD_MAX72xx.h>
  14. #include <SPI.h>
  15. #include <SD.h>
  16. #include <FS.h>
  17. #include <ESP32Servo.h> //Require ESP32Servo
  18. #include <WiFi.h>
  19. #include <ESPAsyncWebServer.h>
  20. #include <DNSServer.h>
  21. /* Pins Definations */
  22. #define STP_DATA_PIN 4 // Stepper Shift Register DS
  23. #define STP_CLOCK_PIN 16 // Stepper Shift Register SH_CP
  24. #define STP_LATCH_PIN 17 // Stepper Shift Register ST_CP
  25. #define SERVO_SWITCH 27 //Servo to push the switch
  26. #define SERVO_COVER 14 //Servo to push the cover
  27. #define DP_CLK_PIN 32 //Display CLK
  28. #define DP_DATA_PIN 33 //Display DIN
  29. #define DP_CS_PIN 25 //Display CS
  30. #define SD_CS_PIN 5 //SD Card CS pin
  31. #define TOGGLE_SWITCH 13 //Switch on top of the matrix display
  32. /* Display settings generated by trial and error. Don't touch these */
  33. #define HARDWARE_TYPE MD_MAX72XX::DR1CR0RR0_HW
  34. #define MAX_DEVICES 8
  35. /* WiFI AP Settings */
  36. #define AP_SSID "(´・ω・`)"
  37. #define ENABLE_WIFI_DEBUG false //Set to true to use WiFi Client mode for remote debugging
  38. #define DEBUG_SSID "" //Debug SSID, usually your home WiFi SSID
  39. #define DEBUG_PWD "" //Debug Password, your home WiFi Password
  40. AsyncWebServer server(80);
  41. DNSServer dnsServer;
  42. /* Calibrated offset for switch pusher servo, in degrees */
  43. #define SERVO_ALIGNMENT_OFFSET 4
  44. /* Hardware Type Definations */
  45. Servo servoSwitchPusher;
  46. Servo servoCoverPusher;
  47. MD_MAX72XX mx = MD_MAX72XX(HARDWARE_TYPE, DP_DATA_PIN, DP_CLK_PIN, DP_CS_PIN, MAX_DEVICES);
  48. /* Global Variables */
  49. char animation = 'a'; //Animation ID to render
  50. int movingDirection = 0; //Debug mode -> Moving direction
  51. bool movingActive = false; //Debug mode -> Is Moving direction active
  52. void setup() {
  53. delay(1000);
  54. Serial.begin(115200);
  55. // Allow allocation of all timers
  56. ESP32PWM::allocateTimer(0);
  57. ESP32PWM::allocateTimer(1);
  58. ESP32PWM::allocateTimer(2);
  59. ESP32PWM::allocateTimer(3);
  60. /* Stepper IO */
  61. pinMode(STP_DATA_PIN, OUTPUT);
  62. pinMode(STP_CLOCK_PIN, OUTPUT);
  63. pinMode(STP_LATCH_PIN, OUTPUT);
  64. standbySteppers();
  65. /* Servo IO */
  66. servoSwitchPusher.setPeriodHertz(50);
  67. servoCoverPusher.setPeriodHertz(50);
  68. servoSwitchPusher.attach(SERVO_SWITCH);
  69. servoCoverPusher.attach(SERVO_COVER);
  70. servoCoverPusher.write(0);
  71. servoSwitchPusher.write(0);
  72. /* Display Module */
  73. mx.begin();
  74. setDisplayBrightness(0x4);
  75. renderFrame(); //Render the default frame to matrix
  76. /* SD Card */
  77. // Initialize SD card
  78. if (!SD.begin(SD_CS_PIN, SPI, 4000000U, "/sd", 10U, false)) {
  79. Serial.println("[Error] Unable to mount SD card. Retrying in 10 seconds");
  80. loadSDErrorToFrameBuffer(); //Render SD ERROR to display
  81. renderFrame();
  82. //Retry in 10 seconds
  83. delay(5000);
  84. ESP.restart();
  85. }
  86. /* Start Dual-core processes */
  87. createSemaphore();
  88. startCoreTasks();
  89. }
  90. void loop() {
  91. //Process has been handled in tasks.ino
  92. //Do not use this loop
  93. }