skizziks_53

Dweeno Slave 2017 v1.0

Aug 16th, 2017
123
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2. Dweeno Slave 2017 v1.0
  3. serial messaging sketch for Uno / Nano / Mega
  4. */
  5.  
  6. int button_pin_2 = 2; // Button #1 is connected to pin 2.
  7. int button_pin_4 = 4; // Button #2 is connected to pin 4.
  8. int buttonValue = 0; // used to store the button input in.
  9.  
  10. int inputPinValue = 0; // This is a temporary variable to store the input pin values in.
  11.  
  12. int LEDpin = 13; // This is the usual #13 LED pin
  13.  
  14. // These four variables are used for the debounce timer for the buttons.
  15. boolean buttonPushed = false;
  16. unsigned long buttonPress_startTime = 0;
  17. unsigned long buttonPress_currentTime = 0;
  18. int buttonPress_delayTime = 1000; // This is the debounce time used for both of the buttons.
  19.  
  20. char charRead;
  21. int mDigits[4]; // This is the character array for the incoming message word.
  22. int mPosition = -1; // This is a index into the mText[] array above.
  23. int asciiCharValue;
  24.  
  25. int commandWord = 0; // This is a counter for which command word is being received (12 words maximum).
  26. int commandArray[12]; // This is the array that will hold the 12 command words when done.
  27.  
  28. int c_ones; // These are used for converting the serial/char data into a 4-digit number.
  29. int c_tens;
  30. int c_hundreds;
  31. int c_thousands;
  32.  
  33. boolean messageIncoming = false;
  34. boolean messageOutgoing = false;
  35.  
  36. // Below is variables used for the arduinoTimeDelay(int, int) function.
  37. int waitMinutes = 0;
  38. int waitSeconds = 0;
  39. boolean timeDelayTimer_Enabled = false;
  40. unsigned long timeDelay_startTime = 0;
  41. unsigned long timeDelay_currentTime = 0;
  42. int timeDelay_intervalTime = 1000;
  43. int timeDelay_timeCounter = 0;
  44.  
  45.  
  46. // Function declarations.
  47. void performCommands();
  48. void button1_pressed();
  49. void button2_pressed();
  50. void blink_slow(int);
  51. void blink_quick(int);
  52. void digitalRead_pin_D3();
  53. void analogRead_pin_A3();
  54. void digitalWrite_pin_D4(int);
  55. void analogWrite_pin_D5(int);
  56. void arduinoTimeDelay(int, int);
  57. void arduinoTimeDelayFinished();
  58. void resetCommandWords();
  59.  
  60.  
  61. // ##################################################################
  62. void setup() {
  63. Serial.begin(115200);
  64. // Note: the serial speed in the Arduino sketch and the PC program both must match!
  65.  
  66. for (int x = 0; x < 8; x++) {
  67. commandArray[x] = 0;
  68. }
  69. mPosition = -1;
  70. commandWord = 0;
  71.  
  72. pinMode(button_pin_2, INPUT_PULLUP);
  73. pinMode(button_pin_4, INPUT_PULLUP);
  74. pinMode(3, INPUT_PULLUP);
  75. //pinMode(A3, INPUT); -- analogRead doesn't require a pinmode declaration, and may not function properly with it done!
  76. pinMode(4, OUTPUT);
  77. //pinMode(5, OUTPUT); -- analogWrite doesn't require a pinmode declaration, and may not function properly with it done!
  78. pinMode(LEDpin, OUTPUT);
  79. digitalWrite(LEDpin, LOW);
  80. }
  81.  
  82. // ##################################################################
  83.  
  84. void loop() { // Beginning of main loop
  85. if (buttonPushed == false) {
  86. buttonValue = digitalRead(button_pin_2);
  87. if (buttonValue == 0) {
  88. button1_pressed();
  89. }
  90. buttonValue = digitalRead(button_pin_4);
  91. if (buttonValue == 0) {
  92. button2_pressed();
  93. }
  94. }
  95.  
  96. if (Serial.available()) {
  97. while (Serial.available() > 0) {
  98. charRead = Serial.read();
  99. delay(1);
  100. asciiCharValue = (int) charRead;
  101. if (asciiCharValue == 42) { // 42 = the asterisk ***
  102. messageIncoming = false;
  103. messageOutgoing = true;
  104. }
  105. else if (asciiCharValue == 58) { // 58 = the colon :::
  106. convertToCommandWord();
  107. mPosition = -1;
  108. commandWord ++;
  109. }
  110. else {
  111. if (asciiCharValue > 47 && asciiCharValue < 58) {
  112. mPosition ++;
  113. mDigits[mPosition] = asciiCharValue - 48;
  114. messageIncoming = true;
  115. }
  116. }
  117. }
  118. }
  119.  
  120. if (messageIncoming == false) {
  121. if (messageOutgoing == true) {
  122. performCommands();
  123. resetCommandWords();
  124. messageOutgoing = false;
  125. }
  126. }
  127.  
  128. // Button debounce timing code------
  129. if (buttonPushed == true) {
  130. buttonPress_currentTime = millis();
  131. if (buttonPress_currentTime >= buttonPress_startTime) {
  132. if (buttonPress_currentTime >= (buttonPress_startTime + buttonPress_delayTime)) {
  133. buttonPushed = false;
  134. }
  135. }
  136. else {
  137. buttonPress_startTime = millis();
  138. }
  139. }
  140.  
  141.  
  142. // Below is variables used for the arduinoTimeDelay(int, int) function.
  143. if (timeDelayTimer_Enabled == true) {
  144. timeDelay_currentTime = millis();
  145. if (timeDelay_currentTime >= timeDelay_startTime) {
  146. if (timeDelay_currentTime >= (timeDelay_startTime + timeDelay_intervalTime)) {
  147. timeDelay_timeCounter++;
  148. if (timeDelay_timeCounter >= waitSeconds) {
  149. timeDelayTimer_Enabled = false;
  150. arduinoTimeDelayFinished();
  151. }
  152. else {
  153. timeDelay_startTime = millis();
  154. }
  155. }
  156. }
  157. else {
  158. timeDelay_startTime = millis();
  159. }
  160.  
  161. }
  162.  
  163.  
  164. } // end of main loop
  165.  
  166. // ##################################################################
  167.  
  168. void convertToCommandWord() {
  169. c_ones = 0;
  170. c_tens = 0;
  171. c_hundreds = 0;
  172. c_thousands = 0;
  173.  
  174. if (mPosition == 0) {
  175. c_ones = mDigits[0];
  176. }
  177. if (mPosition == 1) {
  178. c_ones = mDigits[1];
  179. c_tens = mDigits[0] * 10;
  180. }
  181. if (mPosition == 2) {
  182. c_ones = mDigits[2];
  183. c_tens = mDigits[1] * 10;
  184. c_hundreds = mDigits[0] * 100;
  185. }
  186. if (mPosition == 3) {
  187. c_ones = mDigits[3];
  188. c_tens = mDigits[2] * 10;
  189. c_hundreds = mDigits[1] * 100;
  190. c_thousands = mDigits[0] * 1000;
  191. }
  192. commandArray[commandWord] = c_ones + c_tens + c_hundreds + c_thousands;
  193. for (int y = 0; y < 4; y++) {
  194. mDigits[y] = 0;
  195. }
  196. }
  197.  
  198. // ##############################################################
  199.  
  200. void resetCommandWords() {
  201. for (int x = 0; x < 12; x++) {
  202. commandArray[x] = 0;
  203. }
  204. commandWord = 0;
  205. }
  206.  
  207. // ##############################################################
  208.  
  209. void commandNotFound() {
  210. // If the sent command is not found, then this message gets returned.
  211. Serial.print("???:*");
  212. /*
  213. Below is a set of lines to troubleshoot communications problems.
  214. If you comment out the single Serial.print() line above,
  215. and then un-comment-out the set of Serial.print() lines below,
  216. the error message returns all the numbers currently stored in commandArray[] .
  217. */
  218. /*
  219. Serial.print("err:");
  220. Serial.print(commandArray[0]);
  221. Serial.print(":");
  222. Serial.print(commandArray[1]);
  223. Serial.print(":");
  224. Serial.print(commandArray[2]);
  225. Serial.print(":");
  226. Serial.print(commandArray[3]);
  227. Serial.print(":");
  228. Serial.print(commandArray[4]);
  229. Serial.print(":");
  230. Serial.print(commandArray[5]);
  231. Serial.print(":");
  232. Serial.print(commandArray[6]);
  233. Serial.print(":");
  234. Serial.print(commandArray[7]);
  235. Serial.print(":");
  236. Serial.print(commandArray[8]);
  237. Serial.print(":");
  238. Serial.print(commandArray[9]);
  239. Serial.print(":");
  240. Serial.print(commandArray[10]);
  241. Serial.print(":");
  242. Serial.print(commandArray[11]);
  243. Serial.print(":?");
  244. */
  245. }
  246.  
  247. // ############################################################
  248. void performCommands() {
  249. switch (commandArray[0]) {
  250.  
  251. case 100:
  252. // blink the pin 13 LED slowly, as many times as requested
  253. blink_slow(commandArray[1]);
  254. break;
  255.  
  256. case 101:
  257. // blink the pin13 LED quickly, as many times as requested
  258. blink_quick(commandArray[1]);
  259. break;
  260.  
  261. case 103:
  262. // perform a digitalRead on pin D3.
  263. digitalRead_pin_D3();
  264. break;
  265.  
  266. case 104:
  267. // perform an analogRead on pin A3.
  268. analogRead_pin_A3();
  269. break;
  270.  
  271. case 105:
  272. // perform a digitalWrite on pin D4.
  273. // commandArray[1] = the value to write (zero or 1).
  274. digitalWrite_pin_D4(commandArray[1]);
  275. break;
  276.  
  277. case 106:
  278. // perform an analog write on pin D5.
  279. // commandArray[1] = the value to write (zero to 255).
  280. analogWrite_pin_D5(commandArray[1]);
  281. break;
  282.  
  283. case 107:
  284. // Use the Arduino to cause a time delay.
  285. // commandArray[1] = the time to wait in minutes.
  286. // commandArray[2] = the time to wait in seconds.
  287. arduinoTimeDelay(commandArray[1], commandArray[2]);
  288. break;
  289.  
  290. default:
  291. // if nothing else matches, then signal that the command sent was not found.
  292. commandNotFound();
  293. break;
  294. }
  295. }
  296.  
  297. // ##############################################################
  298.  
  299. void button1_pressed() {
  300. Serial.print("btn:1:*");
  301. buttonPushed = true;
  302. buttonPress_startTime = millis();
  303. }
  304.  
  305. void button2_pressed() {
  306. Serial.print("btn:2:*");
  307. buttonPushed = true;
  308. buttonPress_startTime = millis();
  309. }
  310.  
  311. void blink_slow(int blinkTimes) {
  312. // command# = 100
  313. for (int x = 0; x < blinkTimes; x++) {
  314. digitalWrite(LEDpin, HIGH);
  315. delay(1000);
  316. digitalWrite(LEDpin, LOW);
  317. delay(1000);
  318. }
  319. Serial.print("100:*");
  320. }
  321.  
  322. void blink_quick(int blinkTimes) {
  323. // command# = 101
  324. for (int x = 0; x < blinkTimes; x++) {
  325. digitalWrite(LEDpin, HIGH);
  326. delay(125);
  327. digitalWrite(LEDpin, LOW);
  328. delay(125);
  329. }
  330. Serial.print("101:*");
  331. }
  332.  
  333. void digitalRead_pin_D3() {
  334. // command# = 103
  335. inputPinValue = digitalRead(3);
  336. Serial.print("103:");
  337. Serial.print(inputPinValue);
  338. Serial.print(":*");
  339. }
  340.  
  341. void analogRead_pin_A3() {
  342. // command# = 104
  343. inputPinValue = analogRead(A2);
  344. Serial.print("104:");
  345. Serial.print(inputPinValue);
  346. Serial.print(":*");
  347. }
  348.  
  349. void digitalWrite_pin_D4(int pinValue) {
  350. // command# = 105
  351. digitalWrite(4, pinValue);
  352. Serial.print("105:*");
  353. }
  354.  
  355. void analogWrite_pin_D5(int pinValue) {
  356. // command# = 106
  357. analogWrite(5, pinValue);
  358. Serial.print("106:*");
  359. }
  360.  
  361. void arduinoTimeDelay(int t_Minutes, int t_Seconds) {
  362. // command# = 107
  363. waitMinutes = t_Minutes * 60;
  364. waitSeconds = (waitMinutes + t_Seconds);
  365. timeDelay_timeCounter = 0;
  366. timeDelayTimer_Enabled = true;
  367. timeDelay_startTime = millis();
  368. }
  369.  
  370. void arduinoTimeDelayFinished() {
  371. // This gets called when the arduinoTimeDelay() function has completed.
  372. Serial.print("107:*");
  373. }
RAW Paste Data