Advertisement
hms11

ShopScale

Nov 30th, 2022
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.27 KB | None | 0 0
  1.  
  2. // Fill-in information from your Blynk Template here
  3. #define BLYNK_TEMPLATE_ID "TMPLgBRoLHL2"
  4. #define BLYNK_DEVICE_NAME "Scale Switch"
  5.  
  6. #define BLYNK_FIRMWARE_VERSION "0.1.0"
  7.  
  8. #define BLYNK_PRINT Serial
  9. //#define BLYNK_DEBUG
  10.  
  11. #define APP_DEBUG
  12.  
  13. // Uncomment your board, or configure a custom board in Settings.h
  14. //#define USE_WROVER_BOARD
  15. //#define USE_TTGO_T7
  16.  
  17. #include "BlynkEdgent.h"
  18.  
  19. WidgetLED led1(V1);
  20. WidgetLED led2(V2);
  21.  
  22. BlynkTimer timer;
  23.  
  24. int realSwitch = 0;
  25. const int realSwitchPin = 26;
  26. bool scaleSwitchOn = false;
  27. bool blynkScaleSwitchOn = false;
  28. bool blynkScaleSwitchFirst = false;
  29. bool scaleOn = false;
  30.  
  31. const int output1 = 4;
  32. const int scaleDisplayMOSFET = 16;
  33. const int scaleMOSFET = 17;
  34. const int output4 = 18;
  35.  
  36.  
  37.  
  38. void setup()
  39. {
  40. Serial.begin(115200);
  41. delay(100);
  42. BlynkEdgent.begin();
  43.  
  44. pinMode(realSwitchPin, INPUT);
  45. pinMode(scaleMOSFET, OUTPUT);
  46. pinMode(scaleDisplayMOSFET, OUTPUT);
  47.  
  48. digitalWrite(output1, LOW);
  49. digitalWrite(scaleDisplayMOSFET, LOW);
  50. digitalWrite(scaleMOSFET, LOW);
  51. digitalWrite(output4, LOW);
  52. Blynk.syncAll();
  53.  
  54. }
  55.  
  56. void realScaleSwitch() {
  57. realSwitch = digitalRead(realSwitchPin);
  58. if (realSwitch == 1) {
  59. scaleSwitchOn = true;
  60. }
  61. else if (realSwitch == 0) {
  62. scaleSwitchOn = false;
  63. }
  64. }
  65.  
  66.  
  67. void switchState() {
  68. if ((blynkScaleSwitchOn) || (scaleSwitchOn)) {
  69. scaleOn = true;
  70. }
  71. if ((!scaleSwitchOn) && (!blynkScaleSwitchOn)) {
  72. scaleOn = false;
  73. }
  74. }
  75.  
  76.  
  77. void scaleOperation() {
  78. realScaleSwitch();
  79. switchState();
  80. if (scaleOn) {
  81. digitalWrite(scaleMOSFET, HIGH);
  82. digitalWrite(output4, HIGH);
  83. Blynk.virtualWrite(V1, HIGH);
  84. timer.setTimeout(45000, []()
  85. {
  86. digitalWrite(scaleDisplayMOSFET, HIGH);
  87. Blynk.virtualWrite(V1, LOW);
  88. Blynk.virtualWrite(V2, HIGH);
  89. });
  90. }
  91. else if (!scaleOn) {
  92. digitalWrite(scaleDisplayMOSFET, LOW);
  93. digitalWrite(scaleMOSFET, LOW);
  94. digitalWrite(output4, LOW);
  95. Blynk.virtualWrite(V1, LOW);
  96. Blynk.virtualWrite(V2, LOW);
  97. }
  98. }
  99.  
  100.  
  101. BLYNK_WRITE (V0)
  102. {
  103. if (param.asInt() == 0) {
  104. blynkScaleSwitchOn = false;
  105. }
  106. else if (param.asInt() == 1) {
  107. blynkScaleSwitchOn = true;
  108. }
  109. }
  110.  
  111.  
  112.  
  113. void loop() {
  114. BlynkEdgent.run();
  115. scaleOperation();
  116. }
  117.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement