{"id":151,"date":"2015-08-17T22:11:48","date_gmt":"2015-08-17T21:11:48","guid":{"rendered":"http:\/\/watchdiana.fail\/blog\/?p=151"},"modified":"2015-08-18T02:39:28","modified_gmt":"2015-08-18T01:39:28","slug":"sparky-v2-latest-code","status":"publish","type":"post","link":"https:\/\/watchdiana.fail\/blog\/2015\/08\/17\/sparky-v2-latest-code\/","title":{"rendered":"Sparky v2 &#8211; latest code"},"content":{"rendered":"<p>The code for Sparky Version 2 is just about finished.  I need to swap something I did back-to-front with something Mat did far more elegantly and in fewer lines, but the logic&#8217;s sound.  Here, with a bit of editing, is Oxygen v7.  V1-5 were debugging programs of various sorts.<\/p>\n<pre>\/* Version 7 of Oxygen is the software implementation.\r\n * \r\n * We do not use pins 0 & 1 (RX\/TX) for the main board.\r\n * We scan LVL after smoothing, compare to the set voltages of 'max_trigger', and 'min_trigger' trigger DIR to 1 or 0 if we want to move.\r\n * We then hit STEP.\r\n * Pins should be set to cross directly to DIR and STEP, on veroboard.\r\n * For convenience, we begin crossing at pin 3, with pin 2 being the zoom toggle\r\n * \r\n * For debugging, we output LVL, MAX, MIN, DIR to serial, and comment that out.\r\n * \r\n * Arduino takes in LVL and four cut-out or direction override pins, plus one 'zoom' pin to speed up or slow down stepping in software, and outputs 7 pins - M0, 1, 2, Step, Dir, \/Sleep, \/Reset\r\n\r\n *\/\r\n\r\n\/\/ hardware constants for board input\/output\r\n\r\nconst int ledPin = 13;\r\nconst int zoomPin = 2;  \/\/ earth to 0v on Arduino board to increase speed (decrease software delay)\r\n\r\n\/\/ digital (output) pins (Arduino Pro Mini matched to DRV8825 Stepper Driver)\r\nconst int dirPin = 3;\r\nconst int stepPin = 4;\r\nconst int sleepPin = 5;\r\nconst int resetPin = 6;\r\n\r\n\/\/ step size pin array\r\nint mPins[3] = {7, 8, 9};\r\nconst int mPinCount = sizeof(mPins)\/sizeof(int);\r\nint mPinStates[mPinCount] = {};\r\n\/\/ char *mPinNames[mPinCount] = {\"M0\", \"M1\", \"M2\"};  \/\/ Probably only ever in debugging\/humanised printing\r\n\r\n\/\/ Input for cut-outs and manual drive\r\nconst int upperCutoutPin = 10;\r\nconst int lowerCutoutPin = 11;\r\nconst int driveUpPin = 15;  \/\/ A1 Can be used as digital pin.\r\nconst int driveDownPin = 16;\/\/ A2 can be used as digital pin.\r\n\r\n\/\/ analogue (measurement) pins\r\nconst int levelPin = A0;\r\n\r\n\/\/ measurement constants\r\n\/* lvl = 0...1023 (corresponding to 0 to 5v ish) *\/\r\n\r\nconst int AnalogReadsPerVolt = 200;  \/\/1000 ish corresponds to 5v\r\n\r\n#define volts  \/\/ defines volts as a word to ignore by defining it as blank\r\n\r\nconst int max_trigger = 1 volts * AnalogReadsPerVolt;\r\nconst int min_trigger = .1 volts * AnalogReadsPerVolt;\r\n\r\n\/\/ measurement variables\r\n\r\nint lvl = 0;\r\n\r\n\/\/ move-related variables and calculations\r\n\r\nint move_rate = 100;  \/\/ Used for speeding up\/slowing down in software\r\nint step_ratio = 1;   \/\/ Valid options, 1, 2, 4, 8, 16, 32\r\nint step_index = 0;   \/\/ used to keep track of which result we want\r\nint stepsize_results[] = {32, 16, 8, 4, 2, 1};\r\nconst int stepsize_count = sizeof(stepsize_results) \/ sizeof(int);\r\nint modePinSettings[stepsize_count] = {5, 4, 3, 2, 1, 0}; \/\/ read from High\/Low table in docs, M2 first\r\n\r\n\/* Finds index within modePinSettings, to give us a later result *\/\r\nint find_index(int array[], int count, int value)\r\n{\r\n  int i;\r\n  for(i=0; i<count; i++)\r\n    {\r\n      if(array[i] == value){\r\n\t\/\/Serial.print(\"inside find_index, i = \");\r\n\t\/\/Serial.println(i);\r\n\t       return i;  \/\/ if found return index\r\n      }\r\n    }\r\n  \/\/Serial.println(\"inside find_index, -1\");\r\n  return -1;\r\n}  \/\/ not found\r\n\r\nvoid stepDown(int move_rate){\r\n  delay(move_rate);\r\n  if( digitalRead ( lowerCutoutPin ) == LOW) {\r\n    digitalWrite(dirPin, HIGH);\r\n    delay(move_rate);\r\n    digitalWrite(stepPin, HIGH);\r\n    \r\n    delay(move_rate);\r\n    digitalWrite(stepPin, LOW);\r\n    Serial.print(\"Going Down.\");\r\n  } else { Serial.println(\"Lower Cut-out Engaged: no move.\");\r\n  }\r\n}\r\nvoid stepUp(int move_rate){\r\n  delay(move_rate);\r\n  if( digitalRead ( upperCutoutPin) == LOW ) {\r\n    digitalWrite(dirPin, LOW);\r\n    delay(move_rate);\r\n    digitalWrite(stepPin, HIGH);\r\n    \r\n    delay(move_rate);\r\n    digitalWrite(stepPin, LOW);\r\n    \/\/Serial.println(\"Going Up.\");\r\n  } else{ Serial.println(\"Upper Cut-out Engaged: no move.\");\r\n     }\r\n}\r\n\r\n\r\nvoid setup(){\r\n  \/\/Serial.begin(9600);        \/\/ Optional output, for debugging lines\r\n  \/\/char buffer[50] = {};\r\n  \/\/sprintf(buffer, \"Compiled: %s, %s, %s\", __DATE__, __TIME__, __FILE__);\r\n  \/\/Serial.println(buffer);\r\n  \/\/ pin modes for clean reading\/writing\r\n  pinMode(dirPin, OUTPUT);\r\n  pinMode(stepPin, OUTPUT);\r\n  pinMode(sleepPin, OUTPUT);\r\n  pinMode(resetPin, OUTPUT);\r\n  pinMode(zoomPin, INPUT_PULLUP);  \/\/ earth this pin to speed up software loop\r\n  pinMode(upperCutoutPin, INPUT_PULLUP);  \/\/ takes 1k pull-up on line, as well as internal\r\n  pinMode(lowerCutoutPin, INPUT_PULLUP);  \/\/ 1k pull-up massively reduces inductance noise\r\n  pinMode(driveUpPin, INPUT_PULLUP);\r\n  pinMode(driveDownPin, INPUT_PULLUP);\r\n  \r\n  \/\/ Initialise and set mPins, wake stepper driver\r\n  digitalWrite(sleepPin, LOW);\r\n  step_index = find_index(stepsize_results, stepsize_count, step_ratio);\r\n  if(step_index == -1) step_index = 5;   \/\/ No letting non-valid numbers through, although our 'else' should catch them.  Should.\r\n\r\n  int j = modePinSettings[step_index];\r\n  for(int i = 0; i < mPinCount; i ++){  \/\/ initialise pinMode for identical pins as loop\r\n    pinMode(mPins[i], OUTPUT);      \r\n    digitalWrite(mPins[i], j % 2);\r\n    j >>= 1;\r\n  }\r\n  \r\n  \r\n  digitalWrite(sleepPin, HIGH); \/\/ \/SLEEP - high to suppress behaviour\r\n  \r\n  \/\/ Reset Stepper Board \r\n  digitalWrite(resetPin, LOW);\r\n  delay(5);\r\n  digitalWrite(resetPin, HIGH); \/\/ \/RESET\r\n  digitalWrite(ledPin, HIGH);\r\n\r\n  \/\/Serial.print(\"Initialised with min \");\r\n  \/\/Serial.println(min_trigger);\r\n  \r\n}\r\n\r\nvoid loop()  {\r\n  \r\n  \r\n  if(digitalRead(zoomPin) == LOW){  \/\/ move rate will appear twice in each loop that moves, once in each loop that does not move\r\n    move_rate = 50;\r\n  } else {\r\n    move_rate = 500;}\r\n  \/\/Serial.print(\"zoomPin: \");\r\n  \/\/Serial.print(digitalRead(zoomPin));\r\n  \/\/Serial.print(\"move_rate: \");\r\n  \/\/Serial.println(move_rate);\r\n  \r\n  \r\n  if(digitalRead(driveUpPin) == LOW) {\r\n    \/\/Serial.println(\"Drive pin: Up.\");\r\n    stepUp(move_rate);\r\n  } else if(digitalRead(driveDownPin) == LOW) {\r\n    \/\/Serial.println(\"Drive pin: Down.\");\r\n    stepDown(move_rate); \/\/ Behaviour under thick fingers: shout at clumsy person\r\n  } else {  \r\n\r\n    lvl = analogRead(levelPin);\r\n    \r\n    if(lvl < min_trigger) {\r\n      \/\/Serial.println(\"Calling stepUp()\");\r\n      stepUp(move_rate);\r\n      \/\/ \/\/Serial.println(\"Moving Up.\");  \/\/ debugging line\r\n    }\r\n    else if(lvl > max_trigger) {\r\n      \/\/Serial.println(\"Calling stepDown()\");\r\n      stepDown(move_rate);\r\n      \/\/ \/\/Serial.println(\"Moving Down.\");  \/\/ debugging line\r\n    } else {\/\/Serial.println(\"Fallthrough.\");\r\n    } \/\/ lvl not triggering either threshold.  Try earthing it to be sure it goes up.\r\n  }\r\n}\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>The code for Sparky Version 2 is just about finished. I need to swap something I did back-to-front with something Mat did far more elegantly and in fewer lines, but the logic&#8217;s sound. Here, with a bit of editing, is Oxygen v7. V1-5 were debugging programs of various sorts. \/* Version 7 of Oxygen is [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8,30,29],"tags":[7,9,31,32,12,13],"class_list":["post-151","post","type-post","status-publish","format-standard","hentry","category-coding","category-electronics","category-sparky","tag-c","tag-coding","tag-fun-with-arduinos","tag-fun-with-electronics","tag-sparky","tag-things-i-do-because-im-stubborn","missing-thumbnail"],"_links":{"self":[{"href":"https:\/\/watchdiana.fail\/blog\/wp-json\/wp\/v2\/posts\/151","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/watchdiana.fail\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/watchdiana.fail\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/watchdiana.fail\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/watchdiana.fail\/blog\/wp-json\/wp\/v2\/comments?post=151"}],"version-history":[{"count":1,"href":"https:\/\/watchdiana.fail\/blog\/wp-json\/wp\/v2\/posts\/151\/revisions"}],"predecessor-version":[{"id":152,"href":"https:\/\/watchdiana.fail\/blog\/wp-json\/wp\/v2\/posts\/151\/revisions\/152"}],"wp:attachment":[{"href":"https:\/\/watchdiana.fail\/blog\/wp-json\/wp\/v2\/media?parent=151"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/watchdiana.fail\/blog\/wp-json\/wp\/v2\/categories?post=151"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/watchdiana.fail\/blog\/wp-json\/wp\/v2\/tags?post=151"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}