Browse files

added autonomous project

  • Loading branch information...
1 parent a1102c7 commit 1794863266c41d3946d7739b44e746efa7ca27a2 @svdgraaf committed Feb 25, 2012
View
148 autonomous/autonomous/autonomous.ino
@@ -0,0 +1,148 @@
+#include <Servo.h>
+
+Servo head;
+int distance = 0;
+int headRotation = 0;
+int pos = 0;
+
+//Arduino PWM Speed Control
+int E1 = 6;
+int M1 = 7;
+int E2 = 5;
+int M2 = 4;
+
+void setup()
+{
+ Serial.begin(9600);
+ Serial.println("start");
+
+ // mark forward
+ digitalWrite(M1, HIGH);
+ digitalWrite(M2, HIGH);
+
+ // servo
+ head.attach(9);
+
+ centerHead();
+}
+
+void loop()
+{
+
+ distance = analogRead(0);
+ Serial.println(distance);
+
+ // if close by an object, start rotating
+ if(distance > 100)
+ {
+ Serial.println("uhoh, possible collision detected, sweeping")
+ sweep();
+ }
+ else
+ {
+ forward();
+ }
+}
+
+void forward()
+{
+ digitalWrite(M1, HIGH);
+ digitalWrite(M2, HIGH);
+ analogWrite(E1, 128);
+ analogWrite(E2, 128 );
+}
+
+void standstill()
+{
+ digitalWrite(M1, HIGH);
+ digitalWrite(M2, HIGH);
+ analogWrite(E1, 0);
+ analogWrite(E2, 0);
+}
+
+void sweep()
+{
+ boolean foundNext = false;
+
+ // stop any movement
+ standstill();
+
+ // move the head, and measure the distance
+ for(pos=0;pos<180;pos+=5)
+ {
+ head.write(pos); // sweep start position
+ delay(15);
+ if(measure())
+ {
+ foundNext = true;
+ break;
+ }
+ }
+
+ if(foundNext == false)
+ {
+ for(pos = 180; pos>=1; pos-=5) // goes from 180 degrees to 0 degrees
+ {
+ head.write(pos); // tell servo to go to position in variable 'pos'
+ delay(15);
+ if(measure())
+ {
+ foundNext = true;
+ break;
+ }
+ }
+ }
+
+ centerHead();
+
+ // next step found?
+ if(foundNext)
+ {
+ if(headRotation > 90)
+ {
+ // turn left
+ left();
+ delay(500);
+ }
+ else
+ {
+ right();
+ delay(500);
+ }
+ }
+}
+
+void turnLeft()
+{
+ digitalWrite(M1, HIGH);
+ digitalWrite(M2, LOW);
+ analogWrite(E1, 128);
+ analogWrite(E2, 128);
+}
+
+void turnRight()
+{
+ digitalWrite(M1, LOW);
+ digitalWrite(M2, HIGH);
+ analogWrite(E1, 128);
+ analogWrite(E2, 128 );
+}
+
+void centerHead()
+{
+ head.write(90);
+ delay(15);
+}
+
+boolean measure()
+{
+ currentReadout = analogRead(0);
+ Serial.print("sees: ");
+ Serial.println(currentReadout);
+
+ // if more distant, set the direction
+ if(currentReadout < distance)
+ {
+ headRotation = head.read()
+ }
+}
View
BIN engine/engines/applet/core.a
Binary file not shown.
View
152 engine/engines/applet/engines.cpp
@@ -0,0 +1,152 @@
+#include "Arduino.h"
+void setup();
+void loop();
+//Arduino PWM Speed Control
+int E1 = 6;
+int M1 = 7;
+int E2 = 5;
+int M2 = 4;
+
+// used for detecting incoming bytes
+int incomingByte = 0;
+
+bool w = false;
+bool a = false;
+bool s = false;
+bool d = false;
+
+bool keyUp = false;
+
+void setup()
+{
+ pinMode(M1, OUTPUT);
+ pinMode(M2, OUTPUT);
+
+ Serial.begin(9600);
+ Serial.println("start");
+}
+
+void loop()
+{
+ // send data only when you receive data:
+ if (Serial.available() > 0) {
+ // read the incoming byte:
+ incomingByte = Serial.read();
+
+ // say what you got:
+ Serial.print("I received: ");
+ Serial.println(incomingByte, DEC);
+ Serial.println(incomingByte);
+
+ switch(incomingByte)
+ {
+ case 64:
+ keyUp = true;
+ break;
+ case 119:
+ // w
+ if(keyUp == true)
+ {
+ Serial.println("w = false, keyUp = true");
+ w = false;
+ keyUp = false;
+ }
+ else
+ {
+ Serial.println("w = true, keyUp = false");
+ w = true;
+ }
+ break;
+ case 97:
+ // a
+ if(keyUp == true)
+ {
+ Serial.println("a = false, keyUp = true");
+ a = false;
+ keyUp = false;
+ }
+ else
+ {
+ Serial.println("a = true, keyUp = false");
+ a = true;
+ }
+ break;
+ case 115:
+ // s
+ if(keyUp == true)
+ {
+ Serial.println("s = false, keyUp = true");
+ s = false;
+ keyUp = false;
+ }
+ else
+ {
+ Serial.println("s = true, keyUp = false");
+ s = true;
+ }
+ break;
+ case 100:
+ // d
+ if(keyUp == true)
+ {
+ Serial.println("d = false, keyUp = true");
+ d = false;
+ keyUp = false;
+ }
+ else
+ {
+ Serial.println("d = true, keyUp = false");
+ d = true;
+ }
+ break;
+ default:
+ Serial.println("got nothing, full stop");
+ // full stop
+ keyUp = false;
+ w = false;
+ a = false;
+ s = false;
+ d = false;
+ analogWrite(E1, 0);
+ analogWrite(E2, 0);
+ }
+ }
+
+ // if forward
+ if(w)
+ {
+ // Serial.println("w active");
+ digitalWrite(M1, HIGH);
+ digitalWrite(M2, HIGH);
+ analogWrite(E1, 255);
+ analogWrite(E2, 255);
+ }
+ else if(s)
+ {
+ // Serial.println("s active");
+ digitalWrite(M1, LOW);
+ digitalWrite(M2, LOW);
+ analogWrite(E1, 255);
+ analogWrite(E2, 255);
+ }
+ else
+ {
+ // Serial.println("s/w not active");
+ analogWrite(E1, 0);
+ analogWrite(E2, 0);
+ }
+
+ if(a)
+ {
+ // Serial.println("a active");
+ analogWrite(E1, 255);
+ analogWrite(E2, 64);
+ }
+ else if(d)
+ {
+ // Serial.println("d active");
+ analogWrite(E1, 64);
+ analogWrite(E2, 255);
+ }
+
+}
View
BIN engine/engines/applet/engines.elf
Binary file not shown.
View
239 engine/engines/applet/engines.hex
@@ -0,0 +1,239 @@
+:100000000C9462000C948A000C948A000C948A0070
+:100010000C948A000C948A000C948A000C948A0038
+:100020000C948A000C948A000C948A000C948A0028
+:100030000C948A000C948A000C948A000C948A0018
+:100040000C94EF050C948A000C94E1030C942804A2
+:100050000C948A000C948A000C948A000C948A00F8
+:100060000C948A000C948A000000000024002700F1
+:100070002A0000000000250028002B0000000000DE
+:1000800023002600290004040404040404040202DA
+:100090000202020203030303030301020408102007
+:1000A0004080010204081020010204081020000012
+:1000B0000007000201000003040600000000000029
+:1000C00000009F0511241FBECFEFD8E0DEBFCDBFDB
+:1000D00012E0A0E0B1E0E4ECFDE002C005900D927A
+:1000E000A230B107D9F712E0A2E0B2E001C01D9240
+:1000F000A93BB107E1F710E0C4ECD0E004C02297BF
+:10010000FE010E94DC06C23CD107C9F70E949A0298
+:100110000C94E0060C94000081E992E00E94F70440
+:10012000181619060CF0BBC081E992E00E94280560
+:10013000909303028093020281E992E060E071E013
+:100140000E94CA03609102027091030281E992E069
+:100150004AE050E00E94AB036091020270910302FA
+:1001600081E992E04AE050E00E94AB0380910202F4
+:10017000909103028436910509F45EC0853691059D
+:1001800044F48034910569F08136910509F06BC023
+:1001900023C083379105C9F18737910509F063C002
+:1001A00004C081E0809308027AC08091080288230D
+:1001B00049F081E992E06DE071E00E94CD03109278
+:1001C000040216C081E992E065E271E00E94CD036D
+:1001D00081E08093040263C080910802882359F073
+:1001E00081E992E06DE371E00E94CD031092050277
+:1001F0001092080254C081E992E065E571E00E9426
+:10020000CD0381E0809305024AC0809108028823D3
+:1002100049F081E992E06DE671E00E94CD03109211
+:100220000602E6CF81E992E065E871E00E94CD0325
+:1002300081E08093060233C080910802882349F050
+:1002400081E992E06DE971E00E94CD03109207020E
+:10025000CFCF81E992E065EB71E00E94CD0381E0B0
+:10026000809307021CC081E992E06DEC71E00E946E
+:10027000CD0310920802109204021092050210920F
+:100280000602109207028091EA0160E070E00E948D
+:10029000C0018091EE0160E070E00E94C001809199
+:1002A0000402882349F08091EC0161E00E9446023B
+:1002B0008091F00161E00CC0809106028823A9F0D2
+:1002C0008091EC0160E00E9446028091F00160E0C4
+:1002D0000E9446028091EA016FEF70E00E94C00127
+:1002E0008091EE016FEF70E00AC08091EA0160E05A
+:1002F00070E00E94C0018091EE0160E070E00E9419
+:10030000C00180910502882369F08091EA016FEFB6
+:1003100070E00E94C0018091EE0160E470E00E94F4
+:10032000C001089580910702882361F08091EA015D
+:1003300060E470E00E94C0018091EE016FEF70E018
+:100340000E94C00108950F931F938091EC0161E01A
+:100350000E9420028091F00161E00E94200201E9E8
+:1003600012E0C80140E855E260E070E00E947104CC
+:10037000C80164EE71E00E94CD031F910F910895B2
+:100380001F93CF93DF93182FEB0161E00E942002AF
+:10039000209709F44AC0CF3FD10509F449C0E12FA5
+:1003A000F0E0E255FF4F84918330C1F0843028F4AF
+:1003B000813051F08230B1F50CC0863019F18730B0
+:1003C00049F1843079F514C084B5806884BDC7BD17
+:1003D00033C084B5806284BDC8BD2EC080918000CA
+:1003E000806880938000D0938900C093880024C0E7
+:1003F00080918000806280938000D0938B00C093B6
+:100400008A001AC08091B00080688093B000C093C9
+:10041000B30012C08091B00080628093B000C0939E
+:10042000B4000AC0C038D1051CF4812F60E002C0BE
+:10043000812F61E00E944602DF91CF911F910895C4
+:10044000482F50E0CA0186569F4FFC0124914A571D
+:100450005F4FFA0184918823C1F0E82FF0E0EE0F9E
+:10046000FF1FE859FF4FA591B491662341F49FB750
+:10047000F8948C91209582238C939FBF08959FB709
+:10048000F8948C91822B8C939FBF0895482F50E055
+:10049000CA0182559F4FFC012491CA0186569F4F85
+:1004A000FC0194914A575F4FFA013491332309F4C8
+:1004B00040C0222351F1233071F0243028F4213040
+:1004C000A1F0223011F514C02630B1F02730C1F070
+:1004D0002430D9F404C0809180008F7703C08091CC
+:1004E00080008F7D8093800010C084B58F7702C01C
+:1004F00084B58F7D84BD09C08091B0008F7703C023
+:100500008091B0008F7D8093B000E32FF0E0EE0F7C
+:10051000FF1FEE58FF4FA591B4912FB7F8946623B3
+:1005200021F48C919095892302C08C91892B8C9316
+:100530002FBF0895CF93DF930E9437060E94A30137
+:10054000C5E1D4E00E948C002097E1F30E941504DD
+:10055000F9CFCF92DF92EF92FF920F931F93CF9339
+:10056000DF937C016B018A01C0E0D0E00FC0D601AF
+:100570006D916D01D701ED91FC910190F081E02D1D
+:10058000C7010995C80FD91F015010400115110569
+:1005900071F7CE01DF91CF911F910F91FF90EF90F6
+:1005A000DF90CF900895EF92FF920F931F938C01ED
+:1005B000DC01ED91FC910190F081E02D6DE0099559
+:1005C0007C01D801ED91FC910190F081E02DC801F2
+:1005D0006AE009959C012E0D3F1DC9011F910F91E5
+:1005E000FF90EF900895CF93DF93DB010D900020F3
+:1005F000E9F71197A61BB70BEC01E881F98102809E
+:10060000F381E02DAD010995DF91CF9108954F92CF
+:100610005F927F928F929F92AF92BF92CF92DF9222
+:10062000EF92FF920F931F93DF93CF93CDB7DEB777
+:10063000A1970FB6F894DEBF0FBECDBF2C01742E6C
+:10064000CB01223008F42AE019A231E2C32ED12CCA
+:10065000CC0EDD1E822E9924AA24BB24672D752F73
+:10066000A50194010E94A40679018A01C801B7017D
+:10067000A50194010E947206472D461B0894C108EB
+:10068000D1084A3014F4405D01C0495CF601408352
+:10069000E114F1040105110521F07E2C5F2DC80144
+:1006A000DDCFC201B6010E94F302A1960FB6F89405
+:1006B000DEBF0FBECDBFCF91DF911F910F91FF9095
+:1006C000EF90DF90CF90BF90AF909F908F907F90F2
+:1006D0005F904F900895CF92DF92EF92FF920F9329
+:1006E0001F93CF93DF93EC016A017B012115310544
+:1006F00041F4E881F9810190F081E02D642F0995A2
+:100700001FC02A303105D1F477FF17C0E881F98185
+:100710000190F081E02D6DE209958C014427552769
+:10072000BA014C195D096E097F09CE012AE00E94C9
+:1007300007039801280F391F04C02AE00E9407030D
+:100740009C01C901DF91CF911F910F91FF90EF9014
+:10075000DF90CF900895CF92DF92EF92FF920F93A8
+:100760001F936C017B019A010027F7FC0095102F65
+:10077000B801A7010E946B038C01C6010E94D3023D
+:10078000080F191FC8011F910F91FF90EF90DF9084
+:10079000CF9008950E94F3020895EF92FF920F9375
+:1007A0001F937C010E94F3028C01C7010E94D302B7
+:1007B000080F191FC8011F910F91FF90EF90089526
+:1007C00008951F920F920FB60F9211242F933F930B
+:1007D0004F938F939F93EF93FF934091C600209187
+:1007E000490230914A022F5F3F4F2F733070809142
+:1007F0004B0290914C022817390759F0E0914902B9
+:10080000F0914A02E75FFD4F408330934A02209304
+:100810004902FF91EF919F918F914F913F912F91BD
+:100820000F900FBE0F901F901895E0919D02F091D0
+:100830009E02E05CFF4F8191919120813181821B6A
+:10084000930B8F739070892B11F00E94E003089531
+:100850001F920F920FB60F9211242F933F934F9335
+:100860005F936F937F938F939F93AF93BF93EF9318
+:10087000FF9320918D0230918E0280918F02909192
+:1008800090022817390731F48091C1008F7D809341
+:10089000C10016C0E0918F02F0919002E35BFD4F22
+:1008A000408180918F0290919002019660E470E007
+:1008B0000E9491069093900280938F024093C6000D
+:1008C000FF91EF91BF91AF919F918F917F916F9128
+:1008D0005F914F913F912F910F900FBE0F901F90FE
+:1008E0001895AF92BF92DF92EF92FF920F931F93F2
+:1008F000CF93DF93EC017A018B01DD24403081EE50
+:10090000580780E0680780E0780711F0DD24D39471
+:1009100091E0A92EB12CEC89FD89DD2069F0C5019B
+:100920000E8C02C0880F991F0A94E2F7808360E062
+:1009300079E08DE390E005C0108260E874E88EE114
+:1009400090E0A80197010E94A40621503040404049
+:100950005040569547953795279580E120303807C8
+:1009600020F0DD2011F0DD24D6CFE889F98930832D
+:10097000EA89FB892083EE89FF89408121E030E00C
+:10098000C9010A8C02C0880F991F0A94E2F7482B0C
+:100990004083EE89FF894081C9010B8C02C0880F1A
+:1009A000991F0A94E2F7482B4083EE89FF89408122
+:1009B000C9010C8C02C0880F991F0A94E2F7482BDA
+:1009C0004083EE89FF8980810D8C02C0220F331F86
+:1009D0000A94E2F7209528232083DF91CF911F917D
+:1009E0000F91FF90EF90DF90BF90AF900895DC01E2
+:1009F0001C96ED91FC911D97E05CFF4F2191319188
+:100A000080819181281B390B2F733070C9010895A3
+:100A1000DC011C96ED91FC911D97E05CFF4F20815D
+:100A20003181E054F040DF01AE5BBF4F8D919C916E
+:100A300011972817390719F42FEF3FEF07C08D9151
+:100A40009C91E80FF91F8081282F30E0C90108959B
+:100A5000DC011C96ED91FC911D97E05CFF4F20811D
+:100A60003181E054F040DF01AE5BBF4F8D919C912E
+:100A700011972817390719F42FEF3FEF10C08D9108
+:100A80009C911197E80FF91F20818D919C911197EE
+:100A900001968F73907011969C938E9330E0C901EC
+:100AA0000895FC0186859785DC01A05CBF4FFC01A1
+:100AB000EE5BFF4F2D913C9111978081918128171A
+:100AC0003907C1F70895CF93DF93EC01462FEE85E8
+:100AD000FF85E05CFF4F80819181E054F0400196FA
+:100AE00060E470E00E9491069C01DF01AE5BBF4FA5
+:100AF0008D919C91119728173907D1F3E05CFF4F36
+:100B000080819181E054F040E80FF91F4083EE8529
+:100B1000FF85E05CFF4F31832083EE89FF892081D0
+:100B200081E090E00D8C02C0880F991F0A94E2F7D3
+:100B3000282B208381E090E0DF91CF9108951092DF
+:100B400094021092930288EE93E0A0E0B0E08093CC
+:100B5000950290939602A0939702B093980286EF25
+:100B600091E0909392028093910289E092E09093B9
+:100B70009E0280939D028DE492E09093A002809368
+:100B80009F0285EC90E09093A2028093A10284ECF6
+:100B900090E09093A4028093A30280EC90E0909365
+:100BA000A6028093A50281EC90E09093A802809326
+:100BB000A70286EC90E09093AA028093A90284E0B9
+:100BC0008093AB0283E08093AC0287E08093AD0218
+:100BD00085E08093AE0281E08093AF0208951F927A
+:100BE0000F920FB60F9211242F933F938F939F93E1
+:100BF000AF93BF938091B4029091B502A091B602D9
+:100C0000B091B7023091B8020196A11DB11D232FFA
+:100C10002D5F2D3720F02D570196A11DB11D20937A
+:100C2000B8028093B4029093B502A093B602B09339
+:100C3000B7028091B0029091B102A091B202B0913E
+:100C4000B3020196A11DB11D8093B0029093B10231
+:100C5000A093B202B093B302BF91AF919F918F91D5
+:100C60003F912F910F900FBE0F901F901895789481
+:100C700084B5826084BD84B5816084BD85B58260A1
+:100C800085BD85B5816085BDEEE6F0E0808181603F
+:100C90008083E1E8F0E0108280818260808380813F
+:100CA00081608083E0E8F0E0808181608083E1EB17
+:100CB000F0E0808184608083E0EBF0E080818160FF
+:100CC0008083EAE7F0E080818460808380818260B5
+:100CD000808380818160808380818068808310929E
+:100CE000C1000895629FD001739FF001829FE00DC3
+:100CF000F11D649FE00DF11D929FF00D839FF00D9B
+:100D0000749FF00D659FF00D9927729FB00DE11D46
+:100D1000F91F639FB00DE11DF91FBD01CF01112423
+:100D2000089597FB092E07260AD077FD04D02ED010
+:100D300006D000201AF4709561957F4F0895F6F75C
+:100D4000909581959F4F0895A1E21A2EAA1BBB1B77
+:100D5000FD010DC0AA1FBB1FEE1FFF1FA217B30787
+:100D6000E407F50720F0A21BB30BE40BF50B661F9D
+:100D7000771F881F991F1A9469F760957095809561
+:100D800090959B01AC01BD01CF010895AA1BBB1B2F
+:100D900051E107C0AA1FBB1FA617B70710F0A61B7B
+:100DA000B70B881F991F5A95A9F780959095BC019C
+:100DB000CD010895EE0FFF1F0590F491E02D0994E9
+:040DC000F894FFCFD5
+:100DC400492072656365697665643A200077203D41
+:100DD4002066616C73652C206B65795570203D200D
+:100DE400747275650077203D20747275652C206BD4
+:100DF40065795570203D2066616C73650061203D06
+:100E04002066616C73652C206B65795570203D20DC
+:100E1400747275650061203D20747275652C206BB9
+:100E240065795570203D2066616C73650073203DC3
+:100E34002066616C73652C206B65795570203D20AC
+:100E4400747275650073203D20747275652C206B77
+:100E540065795570203D2066616C73650064203DA2
+:100E64002066616C73652C206B65795570203D207C
+:100E7400747275650064203D20747275652C206B56
+:100E840065795570203D2066616C736500676F74E9
+:100E9400206E6F7468696E672C2066756C6C2073A5
+:100EA400746F7000737461727400060007000500AB
+:100EB4000400000000006305A902F70428050805E2
+:020EC4005105D6
+:00000001FF
View
BIN engine/engines/applet/engines.o
Binary file not shown.
View
419 mp3/mp3.ino
@@ -1,85 +1,364 @@
-//Arduino PWM Speed ControlÔºö
-int E1 = 6;
-int M1 = 7;
-int E2 = 5;
-int M2 = 4;
-
-void setup()
-{
- pinMode(M1, OUTPUT);
- pinMode(M2, OUTPUT);
-
- Serial.begin(9600);
- Serial.println("start");
-
-}
-
-void loop()
-{
-// forward(5);
-// left(1);
-// right(1);
-// backward(5);
- delay(2000);
-}
+/*
+ 4-28-2011
+ Spark Fun Electronics 2011
+ Nathan Seidle
+
+ This code is public domain but you buy me a beer if you use this and we meet someday (Beerware license).
+
+ This example code plays a MP3 from the SD card called 'track001.mp3'. The theory is that you can load a
+ microSD card up with a bunch of MP3s and then play a given 'track' depending on some sort of input such
+ as which pin is pulled low.
+
+ It relies on the sdfatlib from Bill Greiman:
+ http://code.google.com/p/sdfatlib/
+ You will need to download and install his library. To compile, you MUST change Sd2PinMap.h of the SDfatlib!
+ The default SS_PIN = 10;. You must change this line under the ATmega328/Arduino area of code to
+ uint8_t const SS_PIN = 9;. This will cause the sdfatlib to use pin 9 as the 'chip select' for the
+ microSD card on pin 9 of the Arduino so that the layout of the shield works.
+
+ Attach the shield to an Arduino. Load code (after editing Sd2PinMap.h) then open the terminal at 57600bps. This
+ example shows that it takes ~30ms to load up the VS1053 buffer. We can then do whatever we want for ~100ms
+ before we need to return to filling the buffer (for another 30ms).
+
+ This code is heavily based on the example code I wrote to control the MP3 shield found here:
+ http://www.sparkfun.com/products/9736
+ This example code extends the previous example by reading the MP3 from an SD card and file rather than from internal
+ memory of the ATmega. Because the current MP3 shield does not have a microSD socket, you will need to add the microSD
+ shield to your Arduino stack.
+
+ The main gotcha from all of this is that you have to make sure your CS pins for each device on an SPI bus is carefully
+ declared. For the SS pin (aka CS) on the SD FAT libaray, you need to correctly set it within Sd2PinMap.h. The default
+ pin in Sd2PinMap.h is 10. If you're using the SparkFun microSD shield with the SparkFun MP3 shield, the SD CS pin
+ is pin 9.
+
+ Four pins are needed to control the VS1503:
+ DREQ
+ CS
+ DCS
+ Reset (optional but good to have access to)
+ Plus the SPI bus
+
+ Only the SPI bus pins and another CS pin are needed to control the microSD card.
+
+ What surprised me is the fact that with a normal MP3 we can do other things for up to 100ms while the MP3 IC crunches
+ through it's fairly large buffer of 2048 bytes. As long as you keep your sensor checks or serial reporting to under
+ 100ms and leave ~30ms to then replenish the MP3 buffer, you can do quite a lot while the MP3 is playing glitch free.
+
+ */
-void forward(int sec)
-{
- Serial.println("forward");
- // set the engines forward
- digitalWrite(M1, LOW);
- digitalWrite(M2, LOW);
-
- // full power to the thrusters!
- analogWrite(E1, 255);
- analogWrite(E2, 255);
+#include <SPI.h>
+
+//Add the SdFat Libraries
+#include <SdFat.h>
+#include <SdFatUtil.h>
+
+//Create the variables to be used by SdFat Library
+Sd2Card card;
+SdVolume volume;
+SdFile root;
+SdFile track;
+
+//This is the name of the file on the microSD card you would like to play
+//Stick with normal 8.3 nomeclature. All lower-case works well.
+//Note: you must name the tracks on the SD card with 001, 002, 003, etc.
+//For example, the code is expecting to play 'track002.mp3', not track2.mp3.
+char trackName[] = "track001.mp3";
+int trackNumber = 1;
+
+char errorMsg[100]; //This is a generic array used for sprintf of error messages
+
+#define TRUE 0
+#define FALSE 1
+
+//MP3 Player Shield pin mapping. See the schematic
+#define MP3_XCS 6 //Control Chip Select Pin (for accessing SPI Control/Status registers)
+#define MP3_XDCS 7 //Data Chip Select / BSYNC Pin
+#define MP3_DREQ 2 //Data Request Pin: Player asks for more data
+#define MP3_RESET 8 //Reset is active low
+//Remember you have to edit the Sd2PinMap.h of the sdfatlib library to correct control the SD card.
+
+//VS10xx SCI Registers
+#define SCI_MODE 0x00
+#define SCI_STATUS 0x01
+#define SCI_BASS 0x02
+#define SCI_CLOCKF 0x03
+#define SCI_DECODE_TIME 0x04
+#define SCI_AUDATA 0x05
+#define SCI_WRAM 0x06
+#define SCI_WRAMADDR 0x07
+#define SCI_HDAT0 0x08
+#define SCI_HDAT1 0x09
+#define SCI_AIADDR 0x0A
+#define SCI_VOL 0x0B
+#define SCI_AICTRL0 0x0C
+#define SCI_AICTRL1 0x0D
+#define SCI_AICTRL2 0x0E
+#define SCI_AICTRL3 0x0F
+
+const int buttonPin = 5; // the number of the pushbutton pin
+const int ledPin = 13; // the number of the LED pin
+
+// Variables will change:
+int ledState = HIGH; // the current state of the output pin
+int buttonState; // the current reading from the input pin
+int lastButtonState = LOW; // the previous reading from the input pin
+int lastButtonReading = 0;
+long lastDebounceTime = 0; // the last time the output pin was toggled
+long debounceDelay = 1500; // the debounce time; increase if the output flickers
+
+
+void setup() {
+ pinMode(MP3_DREQ, INPUT);
+ pinMode(MP3_XCS, OUTPUT);
+ pinMode(MP3_XDCS, OUTPUT);
+ pinMode(MP3_RESET, OUTPUT);
- delay(1000*sec);
+ // initialize the LED pin as an output:
+ pinMode(ledPin, OUTPUT);
+ // initialize the pushbutton pin as an input:
+ pinMode(buttonPin, INPUT);
+
+
+ digitalWrite(MP3_XCS, HIGH); //Deselect Control
+ digitalWrite(MP3_XDCS, HIGH); //Deselect Data
+ digitalWrite(MP3_RESET, LOW); //Put VS1053 into hardware reset
+
+ Serial.begin(57600); //Use serial for debugging
+ Serial.println("MP3 Testing");
+
+ //Setup SD card interface
+ pinMode(10, OUTPUT); //Pin 10 must be set as an output for the SD communication to work.
+ if (!card.init(SPI_FULL_SPEED,9)) Serial.println("Error: Card init"); //Initialize the SD card and configure the I/O pins.
+ if (!volume.init(&card)) Serial.println("Error: Volume ini"); //Initialize a volume on the SD card.
+ if (!root.openRoot(&volume)) Serial.println("Error: Opening root"); //Open the root directory in the volume.
+
+ //We have no need to setup SPI for VS1053 because this has already been done by the SDfatlib
+
+ //From page 12 of datasheet, max SCI reads are CLKI/7. Input clock is 12.288MHz.
+ //Internal clock multiplier is 1.0x after power up.
+ //Therefore, max SPI speed is 1.75MHz. We will use 1MHz to be safe.
+ SPI.setClockDivider(SPI_CLOCK_DIV16); //Set SPI bus speed to 1MHz (16MHz / 16 = 1MHz)
+ SPI.transfer(0xFF); //Throw a dummy byte at the bus
+ //Initialize VS1053 chip
+ delay(10);
+ digitalWrite(MP3_RESET, HIGH); //Bring up VS1053
+ //delay(10); //We don't need this delay because any register changes will check for a high DREQ
+
+ Mp3SetVolume(20, 20); //Set initial volume (20 = -10dB) LOUD
+// Mp3SetVolume(40, 40); //Set initial volume (20 = -10dB) Manageable
+ //Mp3SetVolume(80, 80); //Set initial volume (20 = -10dB) More quiet
+
+ //Let's check the status of the VS1053
+ int MP3Mode = Mp3ReadRegister(SCI_MODE);
+ int MP3Status = Mp3ReadRegister(SCI_STATUS);
+ int MP3Clock = Mp3ReadRegister(SCI_CLOCKF);
+
+ Serial.print("SCI_Mode (0x4800) = 0x");
+ Serial.println(MP3Mode, HEX);
+
+ Serial.print("SCI_Status (0x48) = 0x");
+ Serial.println(MP3Status, HEX);
+
+ int vsVersion = (MP3Status >> 4) & 0x000F; //Mask out only the four version bits
+ Serial.print("VS Version (VS1053 is 4) = ");
+ Serial.println(vsVersion, DEC); //The 1053B should respond with 4. VS1001 = 0, VS1011 = 1, VS1002 = 2, VS1003 = 3
+
+ Serial.print("SCI_ClockF = 0x");
+ Serial.println(MP3Clock, HEX);
+
+ //Now that we have the VS1053 up and running, increase the internal clock multiplier and up our SPI rate
+ Mp3WriteRegister(SCI_CLOCKF, 0x60, 0x00); //Set multiplier to 3.0x
+
+ //From page 12 of datasheet, max SCI reads are CLKI/7. Input clock is 12.288MHz.
+ //Internal clock multiplier is now 3x.
+ //Therefore, max SPI speed is 5MHz. 4MHz will be safe.
+ SPI.setClockDivider(SPI_CLOCK_DIV4); //Set SPI bus speed to 4MHz (16MHz / 4 = 4MHz)
+
+ MP3Clock = Mp3ReadRegister(SCI_CLOCKF);
+ Serial.print("SCI_ClockF = 0x");
+ Serial.println(MP3Clock, HEX);
+
+ //MP3 IC setup complete
+}
+
+void loop(){
+ // read the state of the pushbutton value:
+ int reading = digitalRead(buttonPin);
+// if(reading > 1022)
+// {
+ Serial.println(reading);
+// }
+// if(reading > 1022 && lastButtonReading < 1000)
+// {
+// Serial.println(reading);
+// }
+// if(lastButtonReading > 1000 && reading > 1022)
+// {
+// Serial.println("no change");
+// }
- analogWrite(E1, 0);
- analogWrite(E2, 0);
+ lastButtonReading = reading;
+
+//
+// if (reading != lastButtonState) {
+// // reset the debouncing timer
+// lastDebounceTime = millis();
+// }
+//
+// if ((millis() - lastDebounceTime) > debounceDelay) {
+// // whatever the reading is at, it's been there for longer
+// // than the debounce delay, so take it as the actual current state:
+// buttonState = reading;
+//
+//// playMP3("track001.mp3");
+// }
+// digitalWrite(ledPin, buttonState);
+// if(buttonState)
+// {
+// Serial.println(buttonState);
+// Serial.println(lastButtonState);
+// Serial.println("click");
+//// reading = LOW;
+// }
+//
+// // save the reading. Next time through the loop,
+// // it'll be the lastButtonState:
+// lastButtonState = reading;
}
-void left(int sec)
-{
- Serial.println("left");
- digitalWrite(M1, LOW);
+//PlayMP3 pulls 32 byte chunks from the SD card and throws them at the VS1053
+//We monitor the DREQ (data request pin). If it goes low then we determine if
+//we need new data or not. If yes, pull new from SD card. Then throw the data
+//at the VS1053 until it is full.
+void playMP3(char* fileName) {
+ if (!track.open(&root, fileName, O_READ)) { //Open the file in read mode.
+ sprintf(errorMsg, "Failed to open %s", fileName);
+ Serial.println(errorMsg);
+ return;
+ }
- // full power to the thrusters!
- analogWrite(E1, 255);
-
- delay(1000*sec);
+ Serial.println("Track open");
+
+ uint8_t mp3DataBuffer[32]; //Buffer of 32 bytes. VS1053 can take 32 bytes at a go.
+ //track.read(mp3DataBuffer, sizeof(mp3DataBuffer)); //Read the first 32 bytes of the song
+ int need_data = TRUE;
+ long replenish_time = millis();
+
+ Serial.println("Start MP3 decoding");
+
+ while(1) {
+ while(!digitalRead(MP3_DREQ)) {
+ //DREQ is low while the receive buffer is full
+ //You can do something else here, the buffer of the MP3 is full and happy.
+ //Maybe set the or test to see how much we can delay before we hear audible glitches
+
+ //If the MP3 IC is happy, but we need to read new data from the SD, now is a great time to do so
+ if(need_data == TRUE) {
+ if(!track.read(mp3DataBuffer, sizeof(mp3DataBuffer))) { //Try reading 32 new bytes of the song
+ //Oh no! There is no data left to read!
+ //Time to exit
+ break;
+ }
+ need_data = FALSE;
+ }
+
+ //Serial.println("."); //Print a character to show we are doing nothing
+
+ //This is here to show how much time is spent transferring new bytes to the VS1053 buffer. Relies on replenish_time below.
+ Serial.print("Time to replenish buffer: ");
+ Serial.print(millis() - replenish_time, DEC);
+ Serial.print("ms");
+
+ //Test to see just how much we can do before the audio starts to glitch
+ long start_time = millis();
+ //delay(150); //Do NOTHING - audible glitches
+ //delay(135); //Do NOTHING - audible glitches
+ //delay(120); //Do NOTHING - barely audible glitches
+ delay(100); //Do NOTHING - sounds fine
+ Serial.print(" Idle time: ");
+ Serial.print(millis() - start_time, DEC);
+ Serial.println("ms");
+ //Look at that! We can actually do quite a lot without the audio glitching
+
+ //Now that we've completely emptied the VS1053 buffer (2048 bytes) let's see how much
+ //time the VS1053 keeps the DREQ line high, indicating it needs to be fed
+ replenish_time = millis();
+ }
+
+
+ if(need_data == TRUE){ //This is here in case we haven't had any free time to load new data
+ if(!track.read(mp3DataBuffer, sizeof(mp3DataBuffer))) { //Go out to SD card and try reading 32 new bytes of the song
+ //Oh no! There is no data left to read!
+ //Time to exit
+ break;
+ }
+ need_data = FALSE;
+ }
+
+ //Once DREQ is released (high) we now feed 32 bytes of data to the VS1053 from our SD read buffer
+ digitalWrite(MP3_XDCS, LOW); //Select Data
+ for(int y = 0 ; y < sizeof(mp3DataBuffer) ; y++) {
+ SPI.transfer(mp3DataBuffer[y]); // Send SPI byte
+ }
+
+ digitalWrite(MP3_XDCS, HIGH); //Deselect Data
+ need_data = TRUE; //We've just dumped 32 bytes into VS1053 so our SD read buffer is empty. Set flag so we go get more data
+ }
+
+ while(!digitalRead(MP3_DREQ)) ; //Wait for DREQ to go high indicating transfer is complete
+ digitalWrite(MP3_XDCS, HIGH); //Deselect Data
- analogWrite(E1, 0);
+ track.close(); //Close out this track
+
+ sprintf(errorMsg, "Track %s done!", fileName);
+ Serial.println(errorMsg);
}
-void right(int sec)
-{
- Serial.println("right");
- digitalWrite(M2, LOW);
+//Write to VS10xx register
+//SCI: Data transfers are always 16bit. When a new SCI operation comes in
+//DREQ goes low. We then have to wait for DREQ to go high again.
+//XCS should be low for the full duration of operation.
+void Mp3WriteRegister(unsigned char addressbyte, unsigned char highbyte, unsigned char lowbyte){
+ while(!digitalRead(MP3_DREQ)) ; //Wait for DREQ to go high indicating IC is available
+ digitalWrite(MP3_XCS, LOW); //Select control
- // full power to the thrusters!
- analogWrite(E2, 255);
-
- delay(1000*sec);
-
- analogWrite(E2, 0);
+ //SCI consists of instruction byte, address byte, and 16-bit data word.
+ SPI.transfer(0x02); //Write instruction
+ SPI.transfer(addressbyte);
+ SPI.transfer(highbyte);
+ SPI.transfer(lowbyte);
+ while(!digitalRead(MP3_DREQ)) ; //Wait for DREQ to go high indicating command is complete
+ digitalWrite(MP3_XCS, HIGH); //Deselect Control
}
-void backward(int sec)
-{
- Serial.println("backward");
- digitalWrite(M1, HIGH);
- digitalWrite(M2, HIGH);
-
- // full power to the thrusters!
- analogWrite(E1, 255);
- analogWrite(E2, 255);
-
- delay(1000*sec);
-
- analogWrite(E1, 0);
- analogWrite(E2, 0);
+//Read the 16-bit value of a VS10xx register
+unsigned int Mp3ReadRegister (unsigned char addressbyte){
+ while(!digitalRead(MP3_DREQ)) ; //Wait for DREQ to go high indicating IC is available
+ digitalWrite(MP3_XCS, LOW); //Select control
+
+ //SCI consists of instruction byte, address byte, and 16-bit data word.
+ SPI.transfer(0x03); //Read instruction
+ SPI.transfer(addressbyte);
+
+ char response1 = SPI.transfer(0xFF); //Read the first byte
+ while(!digitalRead(MP3_DREQ)) ; //Wait for DREQ to go high indicating command is complete
+ char response2 = SPI.transfer(0xFF); //Read the second byte
+ while(!digitalRead(MP3_DREQ)) ; //Wait for DREQ to go high indicating command is complete
+
+ digitalWrite(MP3_XCS, HIGH); //Deselect Control
+
+ int resultvalue = response1 << 8;
+ resultvalue |= response2;
+ return resultvalue;
+}
+
+//Set VS10xx Volume Register
+void Mp3SetVolume(unsigned char leftchannel, unsigned char rightchannel){
+ Mp3WriteRegister(SCI_VOL, leftchannel, rightchannel);
}
+
+
+
+
View
49 sweep/Capfile/Capfile.ino
@@ -0,0 +1,49 @@
+#include <Servo.h>
+
+Servo myservo; // create servo object to control a servo
+ // a maximum of eight servo objects can be created
+
+int pos = 0; // variable to store the servo position
+int near = 0;
+
+void setup()
+{
+ Serial.begin(9600);
+ Serial.println("start");
+ myservo.attach(9); // attaches the servo on pin 9 to the servo object
+}
+
+
+void loop()
+{
+ Serial.println("start");
+ near = analogRead(0);
+ Serial.print("distance: ");
+ Serial.println(near);
+ sweep();
+}
+
+void sweep()
+{
+ for(pos=0;pos<180;pos+=5)
+ {
+ myservo.write(pos); // sweep start position
+ delay(15);
+ measure();
+ }
+
+ for(pos = 180; pos>=1; pos-=5) // goes from 180 degrees to 0 degrees
+ {
+ myservo.write(pos); // tell servo to go to position in variable 'pos'
+ delay(15);
+ measure();
+ }
+
+}
+
+void measure()
+{
+ near = analogRead(0);
+ Serial.print("distance: ");
+ Serial.println(near);
+}

0 comments on commit 1794863

Please sign in to comment.