{"id":76,"date":"2015-07-24T16:17:23","date_gmt":"2015-07-24T15:17:23","guid":{"rendered":"http:\/\/watchdiana.fail\/blog\/?p=76"},"modified":"2015-07-24T16:17:23","modified_gmt":"2015-07-24T15:17:23","slug":"sparky-the-state-of-the-code","status":"publish","type":"post","link":"https:\/\/watchdiana.fail\/blog\/2015\/07\/24\/sparky-the-state-of-the-code\/","title":{"rendered":"Sparky &#8211; the state of the code 2015-07-24"},"content":{"rendered":"<p>Sparky is pretty much functional, and needs making into something good. I&#8217;ve re-written the Arduino code with what I&#8217;ve learned so far in Learn C the Hard Way, and I&#8217;ll use the same board as the Arduino&#8217;s on to trigger electronically isolated LEDs, meaning I don&#8217;t have to have any outputs, so I can use all the pins for measuring.<\/p>\n<p>The code is as generic as I can make it. Other than the LED pin, none of the pins come pre-named, so you can decide what to plug in where and how, and then output it, as long as you have this program and an Arduino programmer.<\/p>\n<pre>\n\/*\n * \n * Version 6 of Oxygen is an almost complete re-write.\n * V6 mounts the LEDs on the in pins, allowing for easy reading, without needing output.  Physical output can be provided by buffering LEDs to numbered pins.\n * V6 has a timing loop of one second, followed by a toggle of the LED on reserved Pin 13.\n * We do not use pins 0 & 1 (RX\/TX).\n * We scan as many lines as we can find pins for, and store them in an array so we can do good things with them.\n * The output to Serial Monitor has two possible outputs of human-readable including pin names, or binary string.\n * \n * TO DO: binary output function for Serial Monitor\n * TO DO: use millisecond timing from boot-up rather than a set delay to make the timing loop 1 second\n * TO DO: Import oscillator code from V5, as separate function applicable to any pin\n * TO DO: Function to map analogue pins to arbitrary voltage levels (default 5v 2 decimal places).\n * \n * May^wDoes contain sarcasm.\n *\/\n\n\/\/ unchanging constants and annoying variables\n\n\/\/ #include &lt;stdarg.h&gt; - use for more elegant printing later - Mat to show me how.  #includes are not generally needed in Arduino - implicit functions are brought in automatically\n\n\/\/ Blink stuff\nint ledState = LOW;\nint ledPin = 13;\n\n\n\/\/ Pin Arrays that I will later resent treating this way\n\n\/\/ digital (measurement) pins\n\nint digitalPinNumbers[] = {2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12};  \/\/ Leave out Pin13.\nconst int digitalPinCount = sizeof(digitalPinNumbers)\/sizeof(int);  \/\/ const so we have a non-variable for indexing\nchar *digitalPinNames[digitalPinCount] = {\"Pin 2\", \"Pin 3\", \"Pin 4\", \"Pin 5\", \"Pin 6\", \"Pin 7\", \"Pin 8\", \"Pin 9\", \"Pin 10\", \"Pin 11\", \"Pin 12\"};  \/\/ also leave out pin 13\nint digitalPinStates[digitalPinCount] = {HIGH}; \/\/ Currently working with pull-up resistors.  To be changed.\n\n\/\/ analogue (measurement) pins\n\nint analoguePinNumbers[] = {A0, A1, A2, A3, A4, A5}; \/\/ A4 & A5 are hard to get to thus likely to float\nconst int analoguePinCount = sizeof(analoguePinNumbers)\/sizeof(int);\nchar *analoguePinNames[analoguePinCount] = {\"Pin A0\", \"Pin A1\", \"Pin A2\", \"Pin A3\", \"Pin A4\", \"Pin A5\"};\nint analoguePinStates[analoguePinCount] = {};\n\n\n\/\/ misplaced functions I will later regret\n\n\/\/ !!! TO DO - add binary output !!!\n\nvoid HumanPrint(){\n\n  Serial.println(\"Digital Pins\");\n  for(int i = 0; i < digitalPinCount; i++){\n    digitalPinStates[i] = digitalRead(digitalPinNumbers[i]);\n    Serial.print(digitalPinNames[i]);\n    Serial.print(\": \");\n    Serial.println(digitalPinStates[i]);\n  }\n  Serial.println(\"Analogue Pins\");\n  for(int i = 0; i < analoguePinCount; i++){\n    analoguePinStates[i] = analogRead(analoguePinNumbers[i]);\n    Serial.print(analoguePinNames[i]);\n    Serial.print(\": \");\n    Serial.println(analoguePinStates[i]);\n  }\n\n}\nvoid setup(){\n  Serial.begin(9600);\n  \n  for(int i = 0; i < digitalPinCount; i++){\n    pinMode(digitalPinNumbers[i], INPUT_PULLUP);\n  }\n  for(int i = 0; i < analoguePinCount; i++){\n    pinMode(analoguePinNumbers[i], INPUT);\n  }\n  pinMode(ledPin, OUTPUT);\n  Serial.println(\"Initialised.\"); \n}\n\n\/\/Do this later - Mat to demo\n\/\/void SerialPrint(char *v0, varargs ...)\n\/\/{\n\/\/}\n\nvoid loop() {\n\n  HumanPrint();\n  \n  ledState = !ledState;\n  digitalWrite(ledPin, ledState);\n  delay(1000);\n}\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Sparky is pretty much functional, and needs making into something good. I&#8217;ve re-written the Arduino code with what I&#8217;ve learned so far in Learn C the Hard Way, and I&#8217;ll use the same board as the Arduino&#8217;s on to trigger electronically isolated LEDs, meaning I don&#8217;t have to have any outputs, so I can use [&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,11,4,12,13],"class_list":["post-76","post","type-post","status-publish","format-standard","hentry","category-coding","category-electronics","category-sparky","tag-c","tag-coding","tag-fun-with-arduinos","tag-learn-c-the-hard-way","tag-making","tag-sparky","tag-things-i-do-because-im-stubborn","missing-thumbnail"],"_links":{"self":[{"href":"https:\/\/watchdiana.fail\/blog\/wp-json\/wp\/v2\/posts\/76","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=76"}],"version-history":[{"count":0,"href":"https:\/\/watchdiana.fail\/blog\/wp-json\/wp\/v2\/posts\/76\/revisions"}],"wp:attachment":[{"href":"https:\/\/watchdiana.fail\/blog\/wp-json\/wp\/v2\/media?parent=76"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/watchdiana.fail\/blog\/wp-json\/wp\/v2\/categories?post=76"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/watchdiana.fail\/blog\/wp-json\/wp\/v2\/tags?post=76"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}