Advertisement
Guest User

Untitled

a guest
Nov 4th, 2021
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.46 KB | None | 0 0
  1. #define BLYNK_PRINT SwSerial
  2. #define BLYNK_PRINT Serial
  3. #define BLYNK_MSG_LIMIT 0
  4. #include <SoftwareSerial.h>
  5. SoftwareSerial SwSerial(10, 11); // RX, TX
  6. #include <BlynkSimpleStream.h>
  7. #include <ESP8266WiFi.h>
  8. #include <BlynkSimpleEsp8266.h>
  9.  
  10. char auth[] = "blynk key";
  11. char ssid[] = "wifi ssid";
  12. char pass[] = "wifi pass";
  13.  
  14. const int stepPin = 13;
  15. const int dirPin = 12;
  16. const int shutterPin =14;
  17.  
  18. bool moveForward = false;
  19. bool moveBackward = false;
  20. const double pitch = 2; // pitch in mm
  21. const double stepAngle = 1.8; // angle per step
  22. double distance = 0;
  23. double numImages = 0;
  24. bool runIt = false;
  25. bool autoMode = false;
  26. double startStep = 0;
  27. double endStep = 0;
  28. double currentStep = 0;
  29.  
  30. void terminalout(String out)
  31. {
  32. Blynk.virtualWrite(V2, out + "\n");
  33. }
  34.  
  35. void setup()
  36. {
  37. SwSerial.begin(9600);
  38. /*
  39. Sets the two pins as Outputs
  40. */
  41. pinMode(stepPin, OUTPUT);
  42. pinMode(dirPin, OUTPUT);
  43. pinMode(shutterPin,OUTPUT);
  44. Serial.begin(9600);
  45. Blynk.begin(Serial, auth, ssid, pass);
  46. }
  47.  
  48. void takeImage()
  49. {
  50. // stabilise period
  51. delay(1000);
  52. digitalWrite(shutterPin, HIGH);
  53. delay(500);
  54. digitalWrite(shutterPin, LOW);
  55. // TODO : do shutter things
  56. }
  57.  
  58. void loop()
  59. {
  60.  
  61. Blynk.run();
  62.  
  63. if (moveForward)
  64. {
  65. move(true);
  66. }
  67.  
  68. if (moveBackward)
  69. {
  70.  
  71. move(false);
  72. }
  73.  
  74. if (runIt)
  75. {
  76. runIt = false; // stop it running more than once
  77. runProcess();
  78. }
  79. }
  80.  
  81.  
  82.  
  83. void move(bool forwards)
  84. {
  85. if (forwards)
  86. digitalWrite(dirPin, HIGH);
  87. else
  88. digitalWrite(dirPin, LOW);
  89.  
  90. digitalWrite(stepPin, HIGH);
  91. delay(2);
  92. digitalWrite(stepPin, LOW);
  93. delay(2);
  94.  
  95. if (forwards)
  96. currentStep++;
  97. else
  98. currentStep--;
  99. }
  100.  
  101. void runProcess()
  102. {
  103. terminalout("Running process");
  104. if (autoMode)
  105. {
  106. terminalout("Auto mode");
  107. // check that start is before end
  108. if (startStep < endStep)
  109. {
  110. // we are ok to go
  111. double numberOfStepsTotal = endStep - startStep;
  112. double numberOfStepsPerShot = numberOfStepsTotal / numImages;
  113. // move the carriage to the start if it's not already there
  114. terminalout("Start Step = " + String(startStep,0));
  115. terminalout("End Step = " + String(endStep,0));
  116. terminalout("Number of images = " + String(numImages,0));
  117.  
  118. while (currentStep > startStep)
  119. {
  120. move(false);
  121. }
  122. for (int i = 0; i < numImages; i++)
  123. {
  124. terminalout("Taking image " + String(i) + " at location " + String(currentStep,0));
  125. takeImage();
  126. terminalout("Image taken");
  127. for (int j = 0; j < numberOfStepsPerShot; j++)
  128. {
  129. move(true);
  130. }
  131. terminalout("Moved to next image");
  132. }
  133. terminalout("finished");
  134. }
  135. }
  136.  
  137. else
  138. {
  139. terminalout("Manual Mode");
  140. // move forward
  141. digitalWrite(dirPin, HIGH);
  142.  
  143. // calculate amount to move per step
  144. double distanceToMove = distance / numImages;
  145. // so need to move "distance to move" numImages times
  146. // convert distance to move to steps
  147. double eachstep = pitch / (360.0 / stepAngle);
  148. double stepsPerDistance = distanceToMove / eachstep;
  149.  
  150. for (int i = 0; i < numImages; i++)
  151. {
  152. takeImage();
  153. for (int j = 0; j < stepsPerDistance; j++)
  154. {
  155. move(true);
  156. }
  157. }
  158. }
  159.  
  160.  
  161. }
  162.  
  163. BLYNK_CONNECTED()
  164. {
  165. Blynk.syncAll();
  166. }
  167.  
  168. /*
  169. move forward
  170. */
  171. BLYNK_WRITE(V0)
  172. {
  173. int pinValue = param.asInt();
  174. if (pinValue == 1)
  175. {
  176. moveForward = true;
  177. }
  178. else
  179. {
  180. moveForward = false;
  181. }
  182. }
  183.  
  184. // move backwards
  185. BLYNK_WRITE(V1)
  186. {
  187. int pinValue = param.asInt();
  188. if (pinValue == 1)
  189. {
  190. moveBackward = true;
  191. }
  192. else
  193. {
  194. moveBackward = false;
  195. }
  196. }
  197.  
  198. // distance
  199. BLYNK_WRITE(V3)
  200. {
  201. distance = param.asInt();
  202. }
  203.  
  204. // num images
  205. BLYNK_WRITE(V4)
  206. {
  207. numImages = param.asInt();
  208. }
  209.  
  210. // go button on image and distance
  211. BLYNK_WRITE(V5)
  212. {
  213. int pinValue = param.asInt();
  214. if (pinValue == 1)
  215. {
  216. runIt = true;
  217. terminalout("Runit\n");
  218. }
  219. }
  220.  
  221. // manual or auto
  222. BLYNK_WRITE(V6)
  223. {
  224. int pinValue = param.asInt();
  225. if (pinValue == 1)
  226. {
  227. autoMode = true;
  228. }
  229. else
  230. autoMode = false;
  231. }
  232.  
  233. // set start
  234. BLYNK_WRITE(V7)
  235. {
  236. int pinValue = param.asInt();
  237. if (pinValue == 1)
  238. {
  239. startStep = currentStep;
  240. }
  241. }
  242.  
  243. // set end
  244. BLYNK_WRITE(V8)
  245. {
  246. int pinValue = param.asInt();
  247. if (pinValue == 1)
  248. {
  249. endStep = currentStep;
  250. }
  251. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement