Stap 4: De Code van de Arduino
1) de code van de Arduino voor de breadboard versie is in de 123D Circuits project, met één-klik gewoon op de Arduino klik dan op "Arduino Code Editor"
2) de code van de Arduino voor de grotere krachtigere versie is op deze pagina - scroll naar beneden.
Hier is een link naar de bibliotheek van de versnellingsmeter. https://code.Google.com/p/mma7361-Library/
#include <AcceleroMMA7361.h> //should be in Documents/Arduino/Libraries (on a Mac) //download it here https://code.google.com/p/mma7361-library/ //mapping of the Arduino board to the large 7-segment from Sparkfun, letter is segment, number is Aruino Digital Output // https://www.sparkfun.com/products/8530 // a // f b // g // e c // d int a = 0; int b = 6; int c = 5; int d = 4; int e = 3; int f = 1; int g = 2; //mapping of the accelerometer board from sparkfun, these are Arduino outputs // https://www.sparkfun.com/products/9652 int st = 8; //self test (output from Arduino) int gsel = 9; //g-force range, +-1.5g and +-6g (output from Arduino) int zg = 10; //zero g (input to Arduino) int slp = 11; //sleep (output from Arduino) AcceleroMMA7361 accelero; //create the accelero object // the setup routine runs once when you press reset button on Arduino or power up: void setup() { // initialize the digital pin as an output. pinMode(a, OUTPUT); pinMode(b, OUTPUT); pinMode(c, OUTPUT); pinMode(d, OUTPUT); pinMode(e, OUTPUT); pinMode(f, OUTPUT); pinMode(g, OUTPUT); writeDigit(-1); // turn off all segments // Serial.begin(9600); //uncomment this line for debugging, it will otherwise mess with the numbers! accelero.begin(slp, st, zg, gsel, A0, A1, A2); //config the accelero to use the pins from above and to read analog values from A0, A1, A2 accelero.setARefVoltage(5); //telling accelero that the AREF voltage is 5V (to get higher resolution you'd set this to 3.3 and blue-wire it on the board) accelero.setSensitivity(LOW); //sets the sensitivity to +-1.5G accelero.calibrate(); //you need to do this for brake light, probably not for dice. } void writeDigit(int digit) { //calling this function sets the segments to look like an intiger 0-9, we never show 0, or 9. -1 is all off. Serial.print("digit: "); Serial.print(digit); Serial.print("\n"); switch(digit) { case 0: digitalWrite(a, HIGH); digitalWrite(b, HIGH); digitalWrite(c, HIGH); digitalWrite(d, HIGH); digitalWrite(e, HIGH); digitalWrite(f, HIGH); digitalWrite(g, LOW); break; case 1: digitalWrite(a, LOW); digitalWrite(b, HIGH); digitalWrite(c, HIGH); digitalWrite(d, LOW); digitalWrite(e, LOW); digitalWrite(f, LOW); digitalWrite(g, LOW); break; case 2: digitalWrite(a, HIGH); digitalWrite(b, HIGH); digitalWrite(c, LOW); digitalWrite(d, HIGH); digitalWrite(e, HIGH); digitalWrite(f, LOW); digitalWrite(g, HIGH); break; case 3: digitalWrite(a, HIGH); digitalWrite(b, HIGH); digitalWrite(c, HIGH); digitalWrite(d, HIGH); digitalWrite(e, LOW); digitalWrite(f, LOW); digitalWrite(g, HIGH); break; case 4: digitalWrite(a, LOW); digitalWrite(b, HIGH); digitalWrite(c, HIGH); digitalWrite(d, LOW); digitalWrite(e, LOW); digitalWrite(f, HIGH); digitalWrite(g, HIGH); break; case 5: digitalWrite(a, HIGH); digitalWrite(b, LOW); digitalWrite(c, HIGH); digitalWrite(d, HIGH); digitalWrite(e, LOW); digitalWrite(f, HIGH); digitalWrite(g, HIGH); break; case 6: digitalWrite(a, HIGH); digitalWrite(b, LOW); digitalWrite(c, HIGH); digitalWrite(d, HIGH); digitalWrite(e, HIGH); digitalWrite(f, HIGH); digitalWrite(g, HIGH); break; case 7: digitalWrite(a, HIGH); digitalWrite(b, HIGH); digitalWrite(c, HIGH); digitalWrite(d, LOW); digitalWrite(e, LOW); digitalWrite(f, LOW); digitalWrite(g, LOW); break; case 8: digitalWrite(a, HIGH); digitalWrite(b, HIGH); digitalWrite(c, HIGH); digitalWrite(d, HIGH); digitalWrite(e, HIGH); digitalWrite(f, HIGH); digitalWrite(g, HIGH); break; case 9: digitalWrite(a, HIGH); digitalWrite(b, HIGH); digitalWrite(c, HIGH); digitalWrite(d, HIGH); digitalWrite(e, LOW); digitalWrite(f, HIGH); digitalWrite(g, HIGH); break; case -1: digitalWrite(a, LOW); digitalWrite(b, LOW); digitalWrite(c, LOW); digitalWrite(d, LOW); digitalWrite(e, LOW); digitalWrite(f, LOW); digitalWrite(g, LOW); break; } } int get_g() { //gets the length of the g force vectors int x = accelero.getXAccel(); int y = accelero.getYAccel(); int z = accelero.getZAccel(); int gf = abs(sqrt(x*x+y*y+z*z)-100); // the "-100" is roughly subtracting out gravity if (gf > 50) { Serial.print("g: "); Serial.print(gf); Serial.print("\n"); } return gf; } int digit = 0; int count = 0; // counter int count2 = 0; // another counter int state = 0; // 0 = idle // 1 = shaking // 2 = show_result int sum = 0; int cur = 0; int res; // the loop routine runs over and over again forever: void loop() { cur = get_g(); sum += cur; if (sum < 0) sum = 0; switch(state) { case 0: // idle state, if cur is >70 then go to state 1 if (cur > 70) { state = 1; count = 0; count2 = 0; } break; case 1: // actively shaking, if (count2 == 10) { // 10 is 100ms writeDigit(sum % 6 + 1); // show a random number every 100ms. Sum is a random number depending on how long the code is running and how you shake the die. count2=0; } else { count2++; } if (cur > 55) { // from here until the break it is checking how long it has been since shaking stopped, if over 250ms without seeing g>55 go to state 2 state = 1; count = 0; } else { if (count > 25) { state = 2; count = 0; } else { count ++; } } break; case 2: // showing the result, the delay times are in ~ centi-seconds if (count == 0) writeDigit(sum % 6 + 1); if (count == 10) writeDigit(sum % 6 + 1); if (count == 25) writeDigit(sum % 6 + 1); if (count == 45) writeDigit(sum % 6 + 1); if (count == 70) writeDigit(sum % 6 + 1); if (count == 100) writeDigit(sum % 6 + 1); if (count == 135) { // from here to close of {} it is showing the final result, slowing down as it goes res = sum % 6 + 1; writeDigit(res); } if (count == 185) writeDigit(-1); if (count == 235) writeDigit(res); if (count == 285) writeDigit(-1); if (count == 335) { writeDigit(res); state = 0; count = 0; } count++; break; } delay(10); //uncomment out this small loop to test that all segments are working. (comment out the other "void loop" lines) //void loop() { // writeDigit(8); // delay(10); }