-Ottima lezione in italiano:
+La serie degli esercizi sui LED RGB introduce argomenti come:
+
+- Forme di dati strutturate: array associativi
+- Programmazione ad oggetti per rappresentare un LED RGB (vedi anche
+ la serie multitasking per refactoring, scope delle variabili /
+ proprieta').
+
+Ottima lezione in italiano sul componente:
- http://www.maffucci.it/2014/09/27/arduino-lezione-09-uso-di-led-rgb-parte-1/
--- /dev/null
+##############################################################\r
+\r
+atmega328bb.name=ATmega328 on a breadboard (8 MHz internal clock)\r
+\r
+atmega328bb.upload.protocol=arduino\r
+atmega328bb.upload.maximum_size=30720\r
+atmega328bb.upload.speed=57600\r
+\r
+atmega328bb.bootloader.low_fuses=0xE2\r
+atmega328bb.bootloader.high_fuses=0xDA\r
+atmega328bb.bootloader.extended_fuses=0x05\r
+\r
+atmega328bb.bootloader.file=atmega/ATmegaBOOT_168_atmega328_pro_8MHz.hex\r
+atmega328bb.bootloader.unlock_bits=0x3F\r
+atmega328bb.bootloader.lock_bits=0x0F\r
+\r
+atmega328bb.build.mcu=atmega328p\r
+atmega328bb.build.f_cpu=8000000L\r
+atmega328bb.build.core=arduino:arduino\r
+atmega328bb.build.variant=arduino:standard\r
+\r
+\r
+atmega328bb.bootloader.tool=arduino:avrdude\r
+atmega328bb.upload.tool=arduino:avrdude\r
--- /dev/null
+:107800000C94343C0C94513C0C94513C0C94513CE1\r
+:107810000C94513C0C94513C0C94513C0C94513CB4\r
+:107820000C94513C0C94513C0C94513C0C94513CA4\r
+:107830000C94513C0C94513C0C94513C0C94513C94\r
+:107840000C94513C0C94513C0C94513C0C94513C84\r
+:107850000C94513C0C94513C0C94513C0C94513C74\r
+:107860000C94513C0C94513C11241FBECFEFD8E036\r
+:10787000DEBFCDBF11E0A0E0B1E0EAE8FFE702C063\r
+:1078800005900D92A230B107D9F712E0A2E0B1E065\r
+:1078900001C01D92AD30B107E1F70E942D3D0C945F\r
+:1078A000C33F0C94003C982F95959595959595958B\r
+:1078B000905D8F708A307CF0282F295A8091C0000B\r
+:1078C00085FFFCCF9093C6008091C00085FFFCCF60\r
+:1078D0002093C6000895282F205DF0CF982F809127\r
+:1078E000C00085FFFCCF9093C6000895EF92FF92F1\r
+:1078F0000F931F93EE24FF2487018091C00087FD22\r
+:1079000017C00894E11CF11C011D111D81E2E8164D\r
+:1079100081EAF80687E0080780E0180770F3E09135\r
+:107920000401F091050109958091C00087FFE9CF1E\r
+:107930008091C6001F910F91FF90EF9008950E94D3\r
+:10794000763C982F8091C00085FFFCCF9093C600B5\r
+:1079500091362CF490330CF09053892F089597555D\r
+:10796000892F08951F930E949F3C182F0E949F3CCF\r
+:107970001295107F810F1F9108951F93182F882350\r
+:1079800021F00E94763C1150E1F71F9108951F935A\r
+:10799000182F0E94763C803249F0809103018F5F5E\r
+:1079A000809303018530C1F01F9108958091C0003C\r
+:1079B00085FFFCCF84E18093C6008091C00085FFE5\r
+:1079C000FCCF1093C6008091C00085FFFCCF80E102\r
+:1079D0008093C6001F910895E0910401F091050184\r
+:1079E00009951F9108950E94763C803241F0809164\r
+:1079F00003018F5F80930301853081F008958091AA\r
+:107A0000C00085FFFCCF84E18093C6008091C00058\r
+:107A100085FFFCCF80E18093C6000895E0910401CA\r
+:107A2000F09105010995089548EC50E08823A1F0F4\r
+:107A30002D9A28EE33E0FA013197F1F721503040CA\r
+:107A4000D1F72D9828EE33E0FA013197F1F7215064\r
+:107A50003040D1F7815061F708953F924F925F9285\r
+:107A60006F927F928F929F92AF92BF92CF92DF924E\r
+:107A7000EF92FF920F931F93CF93DF93000082E06A\r
+:107A80008093C00080E18093C4001092C50088E11B\r
+:107A90008093C10086E08093C2005098589A259A3E\r
+:107AA00081E00E94143D24E1F22E9EE1E92E85E959\r
+:107AB000D82E0FE0C02E10E1B12EAA24A394B1E479\r
+:107AC0009B2EA6E58A2EF2E57F2EE0E26E2E79E46B\r
+:107AD000572E63E5462E50E5352E0E94763C8033C6\r
+:107AE000B1F18133B9F1803409F46FC0813409F404\r
+:107AF00076C0823409F485C0853409F488C08035A5\r
+:107B000031F1823521F1813511F1853509F485C0D6\r
+:107B1000863509F48DC0843609F496C0843709F49B\r
+:107B200003C1853709F472C1863709F466C08091B4\r
+:107B300003018F5F80930301853079F6E0910401A2\r
+:107B4000F091050109950E94763C803351F60E9420\r
+:107B5000F33CC3CF0E94763C803249F78091C0004D\r
+:107B600085FFFCCFF092C6008091C00085FFFCCF5E\r
+:107B70009092C6008091C00085FFFCCF8092C60025\r
+:107B80008091C00085FFFCCF7092C6008091C0003C\r
+:107B900085FFFCCF6092C6008091C00085FFFCCFBE\r
+:107BA0005092C6008091C00085FFFCCF4092C60075\r
+:107BB0008091C00085FFFCCF3092C6008091C0004C\r
+:107BC00085FFFCCFB092C60088CF0E94763C8638F5\r
+:107BD00008F4BDCF0E94763C0E94F33C7ECF0E9409\r
+:107BE000763C803809F49CC0813809F40BC1823896\r
+:107BF00009F430C1883909F48FC080E00E94C73C85\r
+:107C00006CCF84E10E94BD3C0E94F33C66CF85E0CE\r
+:107C10000E94BD3C0E94F33C60CF0E94763C809362\r
+:107C200006010E94763C809307010E94F33C55CFE9\r
+:107C30000E94763C803309F411C183E00E94BD3C70\r
+:107C400080E00E94C73C49CF0E94763C80930902A5\r
+:107C50000E94763C8093080280910C028E7F809374\r
+:107C60000C020E94763C853409F409C18091080217\r
+:107C700090910902892B89F000E010E00E94763C87\r
+:107C8000F801E85FFE4F80830F5F1F4F809108026D\r
+:107C9000909109020817190788F30E94763C8032F8\r
+:107CA00009F045CF80910C0280FFF5C0609106017C\r
+:107CB00070910701660F771F7093070160930601AB\r
+:107CC000A0910802B09109021097C9F0E8E0F1E034\r
+:107CD0009B01AD014E0F5F1FF999FECF32BD21BD53\r
+:107CE000819180BDFA9AF99A2F5F3F4FE417F5070B\r
+:107CF00099F76A0F7B1F70930701609306018091CB\r
+:107D0000C00085FFFCCFF092C6008091C00085FFC7\r
+:107D1000FCCFB092C600E1CE83E00E94C73CDDCE2E\r
+:107D200082E00E94C73CD9CE0E94763C8093090233\r
+:107D30000E94763C80930802809106019091070191\r
+:107D4000880F991F90930701809306010E94763C4B\r
+:107D5000853409F49AC080910C028E7F80930C02C6\r
+:107D60000E94763C803209F0B8CE8091C00085FF39\r
+:107D7000FCCFF092C600A0910802B09109021097C2\r
+:107D8000C1F180910C02082F0170182F1695117007\r
+:107D9000E0910601F0910701AF014F5F5F4FBA011B\r
+:107DA00020E030E00023B1F4112339F49491809164\r
+:107DB000C00085FFFCCF9093C6002F5F3F4FCB01E3\r
+:107DC0000196FA012A173B0780F4BC014F5F5F4F11\r
+:107DD000002351F3F999FECFF2BDE1BDF89A90B5B9\r
+:107DE0008091C00085FFFCCFE6CF709307016093C0\r
+:107DF00006018091C00085FDE5CE8091C00085FF21\r
+:107E0000F8CFE0CE81E00E94C73C67CE0E94763C6E\r
+:107E1000803209F08CCE8091C00085FFFCCFF092BB\r
+:107E2000C6008091C00085FFFCCFE092C600809123\r
+:107E3000C00085FFFCCFD092C6008091C00085FFB6\r
+:107E4000FCCFC092C6008091C00085FFFCCFB092ED\r
+:107E5000C60043CE80E10E94C73C3FCE0E94763CE4\r
+:107E60000E94763C182F0E94763C112309F483C0AF\r
+:107E7000113009F484C08FE00E94C73C2ECE80915F\r
+:107E80000C02816080930C02F1CE80910C02816023\r
+:107E900080930C0265CF809107018823880F880B9F\r
+:107EA0008A2180930B028091060190910701880F2F\r
+:107EB000991F90930701809306018091080280FF2B\r
+:107EC00009C08091080290910902019690930902DD\r
+:107ED00080930802F894F999FECF1127E0910601EA\r
+:107EE000F0910701C8E0D1E0809108029091090269\r
+:107EF000103091F40091570001700130D9F303E084\r
+:107F000000935700E8950091570001700130D9F3B4\r
+:107F100001E100935700E8950990199000915700EE\r
+:107F200001700130D9F301E000935700E8951395F3\r
+:107F3000103498F011270091570001700130D9F3E7\r
+:107F400005E000935700E89500915700017001305B\r
+:107F5000D9F301E100935700E8953296029709F0B2\r
+:107F6000C7CF103011F00296E5CF11248091C000E8\r
+:107F700085FFC5CEC8CE8EE10E94C73CAECD85E957\r
+:0A7F80000E94C73CAACDF894FFCF81\r
+:027F8A00800075\r
+:040000030000780081\r
+:00000001FF\r
+++ /dev/null
-##############################################################\r
-\r
-atmega328bb.name=ATmega328 on a breadboard (8 MHz internal clock)\r
-\r
-atmega328bb.upload.protocol=arduino\r
-atmega328bb.upload.maximum_size=30720\r
-atmega328bb.upload.speed=57600\r
-\r
-atmega328bb.bootloader.low_fuses=0xE2\r
-atmega328bb.bootloader.high_fuses=0xDA\r
-atmega328bb.bootloader.extended_fuses=0x05\r
-\r
-atmega328bb.bootloader.file=atmega/ATmegaBOOT_168_atmega328_pro_8MHz.hex\r
-atmega328bb.bootloader.unlock_bits=0x3F\r
-atmega328bb.bootloader.lock_bits=0x0F\r
-\r
-atmega328bb.build.mcu=atmega328p\r
-atmega328bb.build.f_cpu=8000000L\r
-atmega328bb.build.core=arduino:arduino\r
-atmega328bb.build.variant=arduino:standard\r
-\r
-\r
-atmega328bb.bootloader.tool=arduino:avrdude\r
-atmega328bb.upload.tool=arduino:avrdude\r
+++ /dev/null
-:107800000C94343C0C94513C0C94513C0C94513CE1\r
-:107810000C94513C0C94513C0C94513C0C94513CB4\r
-:107820000C94513C0C94513C0C94513C0C94513CA4\r
-:107830000C94513C0C94513C0C94513C0C94513C94\r
-:107840000C94513C0C94513C0C94513C0C94513C84\r
-:107850000C94513C0C94513C0C94513C0C94513C74\r
-:107860000C94513C0C94513C11241FBECFEFD8E036\r
-:10787000DEBFCDBF11E0A0E0B1E0EAE8FFE702C063\r
-:1078800005900D92A230B107D9F712E0A2E0B1E065\r
-:1078900001C01D92AD30B107E1F70E942D3D0C945F\r
-:1078A000C33F0C94003C982F95959595959595958B\r
-:1078B000905D8F708A307CF0282F295A8091C0000B\r
-:1078C00085FFFCCF9093C6008091C00085FFFCCF60\r
-:1078D0002093C6000895282F205DF0CF982F809127\r
-:1078E000C00085FFFCCF9093C6000895EF92FF92F1\r
-:1078F0000F931F93EE24FF2487018091C00087FD22\r
-:1079000017C00894E11CF11C011D111D81E2E8164D\r
-:1079100081EAF80687E0080780E0180770F3E09135\r
-:107920000401F091050109958091C00087FFE9CF1E\r
-:107930008091C6001F910F91FF90EF9008950E94D3\r
-:10794000763C982F8091C00085FFFCCF9093C600B5\r
-:1079500091362CF490330CF09053892F089597555D\r
-:10796000892F08951F930E949F3C182F0E949F3CCF\r
-:107970001295107F810F1F9108951F93182F882350\r
-:1079800021F00E94763C1150E1F71F9108951F935A\r
-:10799000182F0E94763C803249F0809103018F5F5E\r
-:1079A000809303018530C1F01F9108958091C0003C\r
-:1079B00085FFFCCF84E18093C6008091C00085FFE5\r
-:1079C000FCCF1093C6008091C00085FFFCCF80E102\r
-:1079D0008093C6001F910895E0910401F091050184\r
-:1079E00009951F9108950E94763C803241F0809164\r
-:1079F00003018F5F80930301853081F008958091AA\r
-:107A0000C00085FFFCCF84E18093C6008091C00058\r
-:107A100085FFFCCF80E18093C6000895E0910401CA\r
-:107A2000F09105010995089548EC50E08823A1F0F4\r
-:107A30002D9A28EE33E0FA013197F1F721503040CA\r
-:107A4000D1F72D9828EE33E0FA013197F1F7215064\r
-:107A50003040D1F7815061F708953F924F925F9285\r
-:107A60006F927F928F929F92AF92BF92CF92DF924E\r
-:107A7000EF92FF920F931F93CF93DF93000082E06A\r
-:107A80008093C00080E18093C4001092C50088E11B\r
-:107A90008093C10086E08093C2005098589A259A3E\r
-:107AA00081E00E94143D24E1F22E9EE1E92E85E959\r
-:107AB000D82E0FE0C02E10E1B12EAA24A394B1E479\r
-:107AC0009B2EA6E58A2EF2E57F2EE0E26E2E79E46B\r
-:107AD000572E63E5462E50E5352E0E94763C8033C6\r
-:107AE000B1F18133B9F1803409F46FC0813409F404\r
-:107AF00076C0823409F485C0853409F488C08035A5\r
-:107B000031F1823521F1813511F1853509F485C0D6\r
-:107B1000863509F48DC0843609F496C0843709F49B\r
-:107B200003C1853709F472C1863709F466C08091B4\r
-:107B300003018F5F80930301853079F6E0910401A2\r
-:107B4000F091050109950E94763C803351F60E9420\r
-:107B5000F33CC3CF0E94763C803249F78091C0004D\r
-:107B600085FFFCCFF092C6008091C00085FFFCCF5E\r
-:107B70009092C6008091C00085FFFCCF8092C60025\r
-:107B80008091C00085FFFCCF7092C6008091C0003C\r
-:107B900085FFFCCF6092C6008091C00085FFFCCFBE\r
-:107BA0005092C6008091C00085FFFCCF4092C60075\r
-:107BB0008091C00085FFFCCF3092C6008091C0004C\r
-:107BC00085FFFCCFB092C60088CF0E94763C8638F5\r
-:107BD00008F4BDCF0E94763C0E94F33C7ECF0E9409\r
-:107BE000763C803809F49CC0813809F40BC1823896\r
-:107BF00009F430C1883909F48FC080E00E94C73C85\r
-:107C00006CCF84E10E94BD3C0E94F33C66CF85E0CE\r
-:107C10000E94BD3C0E94F33C60CF0E94763C809362\r
-:107C200006010E94763C809307010E94F33C55CFE9\r
-:107C30000E94763C803309F411C183E00E94BD3C70\r
-:107C400080E00E94C73C49CF0E94763C80930902A5\r
-:107C50000E94763C8093080280910C028E7F809374\r
-:107C60000C020E94763C853409F409C18091080217\r
-:107C700090910902892B89F000E010E00E94763C87\r
-:107C8000F801E85FFE4F80830F5F1F4F809108026D\r
-:107C9000909109020817190788F30E94763C8032F8\r
-:107CA00009F045CF80910C0280FFF5C0609106017C\r
-:107CB00070910701660F771F7093070160930601AB\r
-:107CC000A0910802B09109021097C9F0E8E0F1E034\r
-:107CD0009B01AD014E0F5F1FF999FECF32BD21BD53\r
-:107CE000819180BDFA9AF99A2F5F3F4FE417F5070B\r
-:107CF00099F76A0F7B1F70930701609306018091CB\r
-:107D0000C00085FFFCCFF092C6008091C00085FFC7\r
-:107D1000FCCFB092C600E1CE83E00E94C73CDDCE2E\r
-:107D200082E00E94C73CD9CE0E94763C8093090233\r
-:107D30000E94763C80930802809106019091070191\r
-:107D4000880F991F90930701809306010E94763C4B\r
-:107D5000853409F49AC080910C028E7F80930C02C6\r
-:107D60000E94763C803209F0B8CE8091C00085FF39\r
-:107D7000FCCFF092C600A0910802B09109021097C2\r
-:107D8000C1F180910C02082F0170182F1695117007\r
-:107D9000E0910601F0910701AF014F5F5F4FBA011B\r
-:107DA00020E030E00023B1F4112339F49491809164\r
-:107DB000C00085FFFCCF9093C6002F5F3F4FCB01E3\r
-:107DC0000196FA012A173B0780F4BC014F5F5F4F11\r
-:107DD000002351F3F999FECFF2BDE1BDF89A90B5B9\r
-:107DE0008091C00085FFFCCFE6CF709307016093C0\r
-:107DF00006018091C00085FDE5CE8091C00085FF21\r
-:107E0000F8CFE0CE81E00E94C73C67CE0E94763C6E\r
-:107E1000803209F08CCE8091C00085FFFCCFF092BB\r
-:107E2000C6008091C00085FFFCCFE092C600809123\r
-:107E3000C00085FFFCCFD092C6008091C00085FFB6\r
-:107E4000FCCFC092C6008091C00085FFFCCFB092ED\r
-:107E5000C60043CE80E10E94C73C3FCE0E94763CE4\r
-:107E60000E94763C182F0E94763C112309F483C0AF\r
-:107E7000113009F484C08FE00E94C73C2ECE80915F\r
-:107E80000C02816080930C02F1CE80910C02816023\r
-:107E900080930C0265CF809107018823880F880B9F\r
-:107EA0008A2180930B028091060190910701880F2F\r
-:107EB000991F90930701809306018091080280FF2B\r
-:107EC00009C08091080290910902019690930902DD\r
-:107ED00080930802F894F999FECF1127E0910601EA\r
-:107EE000F0910701C8E0D1E0809108029091090269\r
-:107EF000103091F40091570001700130D9F303E084\r
-:107F000000935700E8950091570001700130D9F3B4\r
-:107F100001E100935700E8950990199000915700EE\r
-:107F200001700130D9F301E000935700E8951395F3\r
-:107F3000103498F011270091570001700130D9F3E7\r
-:107F400005E000935700E89500915700017001305B\r
-:107F5000D9F301E100935700E8953296029709F0B2\r
-:107F6000C7CF103011F00296E5CF11248091C000E8\r
-:107F700085FFC5CEC8CE8EE10E94C73CAECD85E957\r
-:0A7F80000E94C73CAACDF894FFCF81\r
-:027F8A00800075\r
-:040000030000780081\r
-:00000001FF\r
--- /dev/null
+
+//**************************************************************//
+// Name : shiftOutCode, Hello World
+// Author : Carlyn Maw,Tom Igoe, David A. Mellis
+// Date : 25 Oct, 2006
+// Modified: 23 Mar 2010
+// Version : 2.0
+// Notes : Code for using a 74HC595 Shift Register //
+// : to count from 0 to 255
+//****************************************************************
+
+//Pin connected to ST_CP of 74HC595
+int latchPin = 8;
+//Pin connected to SH_CP of 74HC595
+int clockPin = 12;
+////Pin connected to DS of 74HC595
+int dataPin = 11;
+
+
+
+void setup() {
+ //set pins to output so you can control the shift register
+ pinMode(latchPin, OUTPUT);
+ pinMode(clockPin, OUTPUT);
+ pinMode(dataPin, OUTPUT);
+ // Serial Debug
+ Serial.begin(9600);
+ Serial.print("Decimal");
+ Serial.print("\t");
+ Serial.println("Binary");
+}
+
+void loop() {
+ // count from 0 to 255 and display the number
+ // on the LEDs
+ for (int numberToDisplay = 0; numberToDisplay < 256; numberToDisplay++) {
+ // take the latchPin low so
+ // the LEDs don't change while you're sending in bits:
+ digitalWrite(latchPin, LOW);
+
+ // shift out the bits:
+ shiftOut(dataPin, clockPin, MSBFIRST, numberToDisplay);
+
+ //take the latch pin high so the LEDs will light up:
+ digitalWrite(latchPin, HIGH);
+ // Serial Debug
+ Serial.print(numberToDisplay);
+ Serial.print("\t");
+ Serial.println(numberToDisplay, BIN);
+ // pause before next value:
+ delay(200);
+ }
+}
+
+
+
+
--- /dev/null
+//**************************************************************//
+// Name : shiftOutCode, Hello World
+// Author : Carlyn Maw,Tom Igoe, David A. Mellis
+// Date : 25 Oct, 2006
+// Modified: 23 Mar 2010
+// Version : 2.0
+// Notes : Code for using a 74HC595 Shift Register
+//
+// : to count from 0 to 255
+//****************************************************************
+
+
+//Pin connected to ST_CP of 74HC595
+int latchPin = 8;
+//Pin connected to SH_CP of 74HC595
+int clockPin = 12;
+////Pin connected to DS of 74HC595
+int dataPin = 11;
+void setup() {
+ //set pins to output so you can control the shift register
+ pinMode(latchPin, OUTPUT);
+
+pinMode(clockPin, OUTPUT);
+pinMode(dataPin, OUTPUT);
+}
+void loop() {
+ // count from 0 to 255 and display the number
+ // on the LEDs
+ for (int numberToDisplay = 0; numberToDisplay < 256; numberToDisplay++) {
+ // take the latchPin low so
+ // the LEDs don’t change while you’re sending in bits:
+ digitalWrite(latchPin, LOW);
+ // shift out the bits:
+ shiftOut(dataPin, clockPin, MSBFIRST, numberToDisplay);
+ //take the latch pin high so the LEDs will light up:
+ digitalWrite(latchPin, HIGH);
+ // pause before next value:
+ delay(500);
+ }
+}
+
+
+
--- /dev/null
+
+/*
+ Shift Register Example
+ Turning on the outputs of a 74HC595 using an array
+
+ Hardware:
+ * 74HC595 shift register
+ * LEDs attached to each of the outputs of the shift register
+
+ */
+//Pin connected to ST_CP of 74HC595
+int latchPin = 8;
+//Pin connected to SH_CP of 74HC595
+int clockPin = 12;
+////Pin connected to DS of 74HC595
+int dataPin = 11;
+
+//holders for infromation you're going to pass to shifting function
+byte data;
+byte dataArray[10];
+
+void setup() {
+ //set pins to output because they are addressed in the main loop
+ pinMode(latchPin, OUTPUT);
+ Serial.begin(9600);
+
+ //Arduino doesn't seem to have a way to write binary straight into the code
+ //so these values are in HEX. Decimal would have been fine, too.
+ dataArray[0] = 0xFF; //11111111
+ dataArray[1] = 0xFE; //11111110
+ dataArray[2] = 0xFC; //11111100
+ dataArray[3] = 0xF8; //11111000
+ dataArray[4] = 0xF0; //11110000
+ dataArray[5] = 0xE0; //11100000
+ dataArray[6] = 0xC0; //11000000
+ dataArray[7] = 0x80; //10000000
+ dataArray[8] = 0x00; //00000000
+ dataArray[9] = 0x38; //11100000
+
+ //function that blinks all the LEDs
+ //gets passed the number of blinks and the pause time
+ blinkAll_2Bytes(2,500);
+}
+
+void loop() {
+
+ for (int j = 0; j < 10; j++) {
+ //load the light sequence you want from array
+ data = dataArray[j];
+ //ground latchPin and hold low for as long as you are transmitting
+ digitalWrite(latchPin, 0);
+ //move 'em out
+ shiftOut(dataPin, clockPin, data);
+ //return the latch pin high to signal chip that it
+ //no longer needs to listen for information
+ digitalWrite(latchPin, 1);
+ delay(300);
+ }
+}
+
+
+
+// the heart of the program
+void shiftOut(int myDataPin, int myClockPin, byte myDataOut) {
+ // This shifts 8 bits out MSB first,
+ //on the rising edge of the clock,
+ //clock idles low
+
+ //internal function setup
+ int i=0;
+ int pinState;
+ pinMode(myClockPin, OUTPUT);
+ pinMode(myDataPin, OUTPUT);
+
+ //clear everything out just in case to
+ //prepare shift register for bit shifting
+ digitalWrite(myDataPin, 0);
+ digitalWrite(myClockPin, 0);
+
+ //for each bit in the byte myDataOut�
+ //NOTICE THAT WE ARE COUNTING DOWN in our for loop
+ //This means that %00000001 or "1" will go through such
+ //that it will be pin Q0 that lights.
+ for (i=7; i>=0; i--) {
+ digitalWrite(myClockPin, 0);
+
+ //if the value passed to myDataOut and a bitmask result
+ // true then... so if we are at i=6 and our value is
+ // %11010100 it would the code compares it to %01000000
+ // and proceeds to set pinState to 1.
+ if ( myDataOut & (1<<i) ) {
+ pinState= 1;
+ }
+ else {
+ pinState= 0;
+ }
+
+ //Sets the pin to HIGH or LOW depending on pinState
+ digitalWrite(myDataPin, pinState);
+ //register shifts bits on upstroke of clock pin
+ digitalWrite(myClockPin, 1);
+ //zero the data pin after shift to prevent bleed through
+ digitalWrite(myDataPin, 0);
+ }
+
+ //stop shifting
+ digitalWrite(myClockPin, 0);
+}
+
+
+//blinks the whole register based on the number of times you want to
+//blink "n" and the pause between them "d"
+//starts with a moment of darkness to make sure the first blink
+//has its full visual effect.
+void blinkAll_2Bytes(int n, int d) {
+ digitalWrite(latchPin, 0);
+ shiftOut(dataPin, clockPin, 0);
+ shiftOut(dataPin, clockPin, 0);
+ digitalWrite(latchPin, 1);
+ delay(200);
+ for (int x = 0; x < n; x++) {
+ digitalWrite(latchPin, 0);
+ shiftOut(dataPin, clockPin, 255);
+ shiftOut(dataPin, clockPin, 255);
+ digitalWrite(latchPin, 1);
+ delay(d);
+ digitalWrite(latchPin, 0);
+ shiftOut(dataPin, clockPin, 0);
+ shiftOut(dataPin, clockPin, 0);
+ digitalWrite(latchPin, 1);
+ delay(d);
+ }
+}
+
+
+
--- /dev/null
+/* SuperCar like pattern with a shift register.
+ Note: first bit/LED is supposed to be 0 and not 7
+ as in many arduino example sketches.
+
+ Turning on the outputs of a 74HC595 using an array
+
+ Hardware:
+ * 74HC595 shift register
+ * LEDs attached to each of the outputs of the shift register
+ */
+
+int clockPin = 12; //IC Pin 11, Yellow Jumper
+int dataPin = 11; //IC Pin 14, Blue Jumper
+int latchPin = 8; //IC Pin 12, Green Jumper
+
+byte patterns[30] = {
+ B00000001, 100,
+ B00000010, 100,
+ B00000100, 100,
+ B00001000, 100,
+ B00010000, 100,
+ B00100000, 100,
+ B01000000, 100,
+ B10000000, 100,
+ B01000000, 100,
+ B00100000, 100,
+ B00010000, 100,
+ B00001000, 100,
+ B00000100, 100,
+ B00000010, 100
+};
+
+int index = 0;
+int count = sizeof(patterns) / 2;
+
+void setup() {
+ pinMode(latchPin, OUTPUT);
+ pinMode(clockPin, OUTPUT);
+ pinMode(dataPin, OUTPUT);
+}
+
+void loop() {
+ digitalWrite(latchPin, LOW);
+ shiftOut(dataPin, clockPin, MSBFIRST, patterns[index * 2]);
+ digitalWrite(latchPin, HIGH);
+ delay(patterns[(index * 2) + 1]);
+ index++;
+ if (index >= count){
+ index = 0;
+ }
+}
+
--- /dev/null
+
+/*
+ Shift Register Example
+ Turning on the outputs of a 74HC595 using an array
+
+ Hardware:
+ * 74HC595 shift register
+ * LEDs attached to each of the outputs of the shift register
+
+ */
+//Pin connected to ST_CP of 74HC595
+int latchPin = 8;
+//Pin connected to SH_CP of 74HC595
+int clockPin = 12;
+////Pin connected to DS of 74HC595
+int dataPin = 11;
+
+//holders for infromation you're going to pass to shifting function
+byte data;
+byte dataArray[10];
+
+void setup() {
+ //set pins to output because they are addressed in the main loop
+ pinMode(latchPin, OUTPUT);
+ Serial.begin(9600);
+
+ //Arduino doesn't seem to have a way to write binary straight into the code
+ //so these values are in HEX. Decimal would have been fine, too.
+ dataArray[0] = 0xFF; //11111111
+ dataArray[1] = 0xFE; //11111110
+ dataArray[2] = 0xFC; //11111100
+ dataArray[3] = 0xF8; //11111000
+ dataArray[4] = 0xF0; //11110000
+ dataArray[5] = 0xE0; //11100000
+ dataArray[6] = 0xC0; //11000000
+ dataArray[7] = 0x80; //10000000
+ dataArray[8] = 0x00; //00000000
+ dataArray[9] = 0x38; //11100000
+
+ //function that blinks all the LEDs
+ //gets passed the number of blinks and the pause time
+ blinkAll_2Bytes(2,500);
+}
+
+void loop() {
+
+ for (int j = 0; j < 10; j++) {
+ //load the light sequence you want from array
+ data = dataArray[j];
+ //ground latchPin and hold low for as long as you are transmitting
+ digitalWrite(latchPin, 0);
+ //move 'em out
+ shiftOut(dataPin, clockPin, data);
+ //return the latch pin high to signal chip that it
+ //no longer needs to listen for information
+ digitalWrite(latchPin, 1);
+ delay(300);
+ }
+}
+
+
+
+// the heart of the program
+void shiftOut(int myDataPin, int myClockPin, byte myDataOut) {
+ // This shifts 8 bits out MSB first,
+ //on the rising edge of the clock,
+ //clock idles low
+
+ //internal function setup
+ int i=0;
+ int pinState;
+ pinMode(myClockPin, OUTPUT);
+ pinMode(myDataPin, OUTPUT);
+
+ //clear everything out just in case to
+ //prepare shift register for bit shifting
+ digitalWrite(myDataPin, 0);
+ digitalWrite(myClockPin, 0);
+
+ //for each bit in the byte myDataOut�
+ //NOTICE THAT WE ARE COUNTING DOWN in our for loop
+ //This means that %00000001 or "1" will go through such
+ //that it will be pin Q0 that lights.
+ for (i=7; i>=0; i--) {
+ digitalWrite(myClockPin, 0);
+
+ //if the value passed to myDataOut and a bitmask result
+ // true then... so if we are at i=6 and our value is
+ // %11010100 it would the code compares it to %01000000
+ // and proceeds to set pinState to 1.
+ if ( myDataOut & (1<<i) ) {
+ pinState= 1;
+ }
+ else {
+ pinState= 0;
+ }
+
+ //Sets the pin to HIGH or LOW depending on pinState
+ digitalWrite(myDataPin, pinState);
+ //register shifts bits on upstroke of clock pin
+ digitalWrite(myClockPin, 1);
+ //zero the data pin after shift to prevent bleed through
+ digitalWrite(myDataPin, 0);
+ }
+
+ //stop shifting
+ digitalWrite(myClockPin, 0);
+}
+
+
+//blinks the whole register based on the number of times you want to
+//blink "n" and the pause between them "d"
+//starts with a moment of darkness to make sure the first blink
+//has its full visual effect.
+void blinkAll_2Bytes(int n, int d) {
+ digitalWrite(latchPin, 0);
+ shiftOut(dataPin, clockPin, 0);
+ shiftOut(dataPin, clockPin, 0);
+ digitalWrite(latchPin, 1);
+ delay(200);
+ for (int x = 0; x < n; x++) {
+ digitalWrite(latchPin, 0);
+ shiftOut(dataPin, clockPin, 255);
+ shiftOut(dataPin, clockPin, 255);
+ digitalWrite(latchPin, 1);
+ delay(d);
+ digitalWrite(latchPin, 0);
+ shiftOut(dataPin, clockPin, 0);
+ shiftOut(dataPin, clockPin, 0);
+ digitalWrite(latchPin, 1);
+ delay(d);
+ }
+}
+
+
+
--- /dev/null
+/*
+ Shift Register Example
+ for 74HC595 shift register
+
+ This sketch turns reads serial input and uses it to set the pins
+ of a 74HC595 shift register.
+
+ Hardware:
+ * 74HC595 shift register attached to pins 8, 12, and 11 of the Arduino,
+ as detailed below.
+ * LEDs attached to each of the outputs of the shift register
+
+ Created 22 May 2009
+ Created 23 Mar 2010
+ by Tom Igoe
+
+ */
+
+//Pin connected to latch pin (ST_CP) of 74HC595
+const int latchPin = 8;
+//Pin connected to clock pin (SH_CP) of 74HC595
+const int clockPin = 12;
+////Pin connected to Data in (DS) of 74HC595
+const int dataPin = 11;
+
+void setup() {
+ //set pins to output because they are addressed in the main loop
+ pinMode(latchPin, OUTPUT);
+ pinMode(dataPin, OUTPUT);
+ pinMode(clockPin, OUTPUT);
+ Serial.begin(9600);
+ Serial.println("reset");
+}
+
+void loop() {
+ if (Serial.available() > 0) {
+ // ASCII '0' through '9' characters are
+ // represented by the values 48 through 57.
+ // so if the user types a number from 0 through 9 in ASCII,
+ // you can subtract 48 to get the actual value:
+ int bitToSet = Serial.read() - 48;
+
+ // write to the shift register with the correct bit set high:
+ registerWrite(bitToSet, HIGH);
+ }
+}
+
+// This method sends bits to the shift register:
+
+void registerWrite(int whichPin, int whichState) {
+// the bits you want to send
+ byte bitsToSend = 0;
+
+ // turn off the output so the pins don't light up
+ // while you're shifting bits:
+ digitalWrite(latchPin, LOW);
+
+ // turn on the next highest bit in bitsToSend:
+ bitWrite(bitsToSend, whichPin, whichState);
+
+ // shift the bits out:
+ shiftOut(dataPin, clockPin, MSBFIRST, bitsToSend);
+
+ // turn on the output so the LEDs can light up:
+ digitalWrite(latchPin, HIGH);
+ delay(300);
+
+}
+
+++ /dev/null
-/* Blink without Delay
-
- Utilizziamo la funzione millis() al posto di delay()
- per poter gestire il lampeggio di un LED senza bloccare
- il processore.
-
- Questo esercizio e' strutturato in una serie di passaggi incrementali
- nei quali una versione minimale si evolve per introdurre
- programmazione ad oggetti, interrupts, pointers.
-
- Turns on and off a light emitting diode(LED) connected to a digital
- pin, without using the delay() function. This means that other code
- can run at the same time without being interrupted by the LED code.
-
- The circuit:
- * LED attached from pin 13 to ground.
- * Note: on most Arduinos, there is already an LED on the board
- that's attached to pin 13, so no hardware is needed for this example.
-
-
- created 2005
- by David A. Mellis
- modified 8 Feb 2010
- by Paul Stoffregen
- 2015 modified by Andrea Manni
-
- This example code is in the public domain.
-
-
- http://www.arduino.cc/en/Tutorial/BlinkWithoutDelay
- */
-
-// constants won't change. Used here to
-// set pin numbers:
-const int ledPin = 13;
-
-// Variables will change:
-int ledState = LOW; // ledState used to set the LED
-long previousMillis = 0; // will store last time LED was updated
-
-// the follow variables is a long because the time, measured in miliseconds,
-// will quickly become a bigger number than can be stored in an int.
-const long interval = 1000; // interval at which to blink (milliseconds)
-
-void setup() {
- // set the digital pin as output:
- pinMode(ledPin, OUTPUT);
-}
-
-void loop()
-{
- // here is where you'd put code that needs to be running all the time.
-
- // check to see if it's time to blink the LED; that is, if the
- // difference between the current time and last time you blinked
- // the LED is bigger than the interval at which you want to
- // blink the LED.
-
- if (millis() > previousMillis + interval) {
- // Aggiorniamo il contatore previousMillis
- previousMillis = millis();
-
- // if the LED is off turn it on and vice-versa:
- if (ledState == LOW)
- ledState = HIGH;
- else
- ledState = LOW;
- // e' possibile semplificare questa operazione?
- // Hint: lo stato del LED e' binario: ha solo due stati possibili.
-
- // set the LED with the ledState of the variable:
- digitalWrite(ledPin, ledState);
- }
-}
-
-/* Domande
- 1. Aggioungere un LED che brilli ogni 500ms: iniziare pensando
- a quali variabili gestiscono l'attuale LED e a quali si
- dovranno aggiungere.
- 2. E' ora agevole cambiare gli intervalli dei due LED?
- Modificare gli intervalli dei due led (es 500ms - 320ms)
- */
+++ /dev/null
-/* Blink without Delay - due led
-
- Turns on and off a light emitting diode(LED) connected to a digital
- pin, without using the delay() function. This means that other code
- can run at the same time without being interrupted by the LED code.
-
- The circuit:
- * LED attached from pin 13 to ground.
- * Note: on most Arduinos, there is already an LED on the board
- that's attached to pin 13, so no hardware is needed for this example.
-
-
- created 2005
- by David A. Mellis
- modified 8 Feb 2010
- by Paul Stoffregen
- modified by eaman
-
- This example code is in the public domain.
-
-
- http://www.arduino.cc/en/Tutorial/BlinkWithoutDelay
- */
-
-// constants won't change. Used here to
-// set pin numbers:
-const int ledA = 13; // Primo LED
-const int ledB = 12; // Secondo LED
-
-// Variabbili di stato
-int ledStateA = LOW; // ledState used to set the LED
-int ledStateB = LOW; // ledState used to set the LED
-
-long previousMillisA = 0; // will store last time LED was updated
-long previousMillisB = 0; // will store last time LED was updated
-
-// the follow variables is a long because the time, measured in miliseconds,
-// will quickly become a bigger number than can be stored in an int.
-long intervalA = 1000; // interval at which to blink (milliseconds)
-long intervalB = 500; // interval at which to blink (milliseconds)
-
-void setup() {
- // set the digital pin as output:
- pinMode(ledA, OUTPUT);
- pinMode(ledB, OUTPUT);
-}
-
-void loop()
-{
-// Primo LED
- if (millis() > previousMillisA + intervalA) {
- // save the last time you blinked the LED
- previousMillisA = millis();
-
- // if the LED is off turn it on and vice-versa:
- if (ledStateA == LOW)
- ledStateA = HIGH;
- else
- ledStateA = LOW;
- // set the LED with the ledState of the variable:
- digitalWrite(ledA, ledStateA);
- }
-
-// Secondo LED
- if (millis() > previousMillisB + intervalB) {
- // save the last time you blinked the LED
- previousMillisB = millis();
-
- // if the LED is off turn it on and vice-versa:
- if (ledStateB == LOW)
- ledStateB = HIGH;
- else
- ledStateB = LOW;
- // set the LED with the ledState of the variable:
- digitalWrite(ledB, ledStateB);
- }
-}
-
-/* Domande
- 1. Provare a isolare il codice per accendere ogni singolo led in una funzione:
- - Quali variabili determinano il comportamento del LED?
- - Come cambiano durante il corso dello script?
- - Sono globali o locali?
- - Quali parti vanno eseguite una sola volta e quali a ogni esecuzione?
- */
-
-
-
+++ /dev/null
-/* Blink without Delay - due led
-
- Turns on and off a light emitting diode(LED) connected to a digital
- pin, without using the delay() function. This means that other code
- can run at the same time without being interrupted by the LED code.
-
- The circuit:
- * LED attached from pin 13 to ground.
- * Note: on most Arduinos, there is already an LED on the board
- that's attached to pin 13, so no hardware is needed for this example.
-
-
- created 2005
- by David A. Mellis
- modified 8 Feb 2010
- by Paul Stoffregen
- modified by eaman
-
- This example code is in the public domain.
-
-
- http://www.arduino.cc/en/Tutorial/BlinkWithoutDelay
- */
-
-// constants won't change. Used here to
-// set pin numbers:
-const int ledA = 13; // Primo LED
-const int ledB = 12; // Secondo LED
-
-// Variabbili di stato
-int ledStateA = LOW; // ledState used to set the LED
-//int ledStateB = LOW; // Not used
-
-long previousMillisA = 0; // will store last time LED was updated
-unsigned long previousMillisB = 0; // will store last time LED was updated
-
-// the follow variables is a long because the time, measured in miliseconds,
-// will quickly become a bigger number than can be stored in an int.
-long intervalA = 1000; // interval at which to blink (milliseconds)
-long intervalB = 500; // interval at which to blink (milliseconds)
-
-void setup() {
- // set the digital pin as output:
- pinMode(ledA, OUTPUT);
- pinMode(ledB, OUTPUT);
-}
-
-void loop()
-{
-// Primo LED
- if (millis() - previousMillisA > intervalA) {
- // save the last time you blinked the LED
- previousMillisA = millis();
-
- // if the LED is off turn it on and vice-versa:
- ledStateA = !ledStateA;
- // set the LED with the ledState of the variable:
- digitalWrite(ledA, ledStateA);
- }
-
-// Secondo LED: contratta
- if (millis() - previousMillisB > intervalB) {
- digitalWrite(ledB, !digitalRead(ledB));
- previousMillisB = millis();
- }
-}
-
-/* Domande
- 1. Provare a isolare il codice per accendere ogni singolo led in una funzione:
- - Quali variabili determinano il comportamento del LED?
- - Come cambiano durante il corso dello script?
- - Sono globali o locali?
- - Quali parti vanno eseguite una sola volta e quali a ogni esecuzione?
- */
-
-
-
+++ /dev/null
-/*
- Blink v1
-
- Accensione e spegnimanto di un LED utilizzando variabili
- per impostare la velocita' del lampeggio.
-
- */
-
-// Pin 13 ha un LED collegato di default
-int led = 13;
-
-void setup() {
- // Inizializziamo il PIN 13 come OUTPUT
- pinMode(led, OUTPUT);
-}
-
-void loop() {
- digitalWrite(led, HIGH);
- delay(1000);
- digitalWrite(led, LOW);
- delay(1000);
-}
-
-/* Domande
- 1. Aggiungere un secondo LED e farlo brillare ogni 500ms
- mentre il primo brilla ogni 1000ms
- 2. Cosa succederebbe se dovessi anche leggere un input da un sensore / bottone?
- */
-
+++ /dev/null
-/*
- Blink due LED - Soluzione
-
- Aggiungere un secondo LED e farlo brillare ogni 500ms
- mentre il primo brilla ogni 1000ms
-
- Massimo comun denominatore 1000 MCD 500 = 500ms
- Durata Periodo = 500ms
-
-
- Stati:
-
- a | b Changes
- ======== =========
- 1 | 1 x | x
- 1 | 0 | x
- 0 | 1 x | x
- 0 | 0 | x
-
-
- */
-
-// Pin 13 has an LED connected on most Arduino boards.
-// give it a name:
-const int ledA = 13; //Primo LED
-const int ledB = 12; //Secondo LED, con resistenza
-
-// the setup routine runs once when you press reset:
-void setup() {
- // initialize the digital pin as an output.
- pinMode(ledA, OUTPUT);
- pinMode(ledB, OUTPUT);
-}
-
-// the loop routine runs over and over again forever:
-void loop() {
- // Primo periodo
- digitalWrite(ledA, HIGH); // turn the LED on (HIGH is the voltage level)
- digitalWrite(ledB, HIGH);
- delay(500); // Minimo comun denominatore del periodo
-
- // Secondo periodo
- //digitalWrite(ledA, HIGH); // ledA non cambia
- digitalWrite(ledB, LOW);
- delay(500);
-
- // Terzo periodo
- digitalWrite(ledA, LOW);
- digitalWrite(ledB, HIGH);
- delay(500);
-
- // Quarto periodo
- //digitalWrite(ledA, LOW);
- digitalWrite(ledB, LOW);
- delay(500);
-}
-
-/* Domande
- 1. Altro scenartio: fare brillare un LED ogni 300ms mentre il secondo brilla ogni 400m
- 2. ...valutare come aggiungere un terzo LED, gestire altri intevalli.
- */
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+++ /dev/null
-/*
- Blink con funzioni.
-
- Le funzioni sono una sequenza di istruzione raggruppate appunto in un a funzione.
- Le funzioni possono accettare argomenti e avere questi pre-impostati a valori di default se omessi.
- Le funzioni possono limitarsi a svolgere istruzionioppure elaborare valori restituendo un risultato.
-
- */
-
-// Pin 13 has an LED connected on most Arduino boards.
-// give it a name:
-int led = 13;
-
-
-// the setup routine runs once when you press reset:
-void setup() {
- // initialize the digital pin as an output.
- pinMode(led, OUTPUT);
-}
-
-// the loop routine runs over and over again forever:
-void loop() {
- lunga() ;
- lunga() ;
- breve();
- breve();
- varia(3000);
-}
-
-// Funzioni personalizzate: nella scheda funzioni.
+++ /dev/null
-// Funzioni personalizzate
-// Un scheda e' un documento che viene concatenato allo sketch originale
-
-void lunga() {
- // Blink con pausa lunga
-
- digitalWrite(led, HIGH); // turn the LED on (HIGH is the voltage level)
- delay(1000); // wait for a second
- digitalWrite(led, LOW); // turn the LED off by making the voltage LOW
- delay(1000); // wait for a second
-}
-
-void breve() {
- // Blink con pausa breve
-
- digitalWrite(led, HIGH); // turn the LED on (HIGH is the voltage level)
- delay(200); // wait for a second
- digitalWrite(led, LOW); // turn the LED off by making the voltage LOW
- delay(1000); // wait for a second
-}
-
-void varia(int a = 300) { // Varia has a default value, the user can override it with an argument
- // Lampeggia per un tempo impostato dall'utente,
- // il default e' 300ms
-
- digitalWrite(led, HIGH); // turn the LED on (HIGH is the voltage level)
- delay(a); // wait for a second
- digitalWrite(led, LOW); // turn the LED off by making the voltage LOW
- delay(a); // wait for a second
-
-}
-
-void lampeggia(int ripetizioni) {
- // Accende un LED per un numero stabilito di volte
-
- // Questa funziona accetta un parametro: ripetizioni
- int i = 0;
- while (i < ripetizioni) {
- rapido(); // accende e spegne rapidamente il LED
- i = i + 1 ; // incrementa l'iteratore
- // i++ ; // equivalente
- }
-}
-
-int area(int latoA, int latoB) {
- // Calcola l'area di un rettangolo
- // e ritorna il valore calcolato: questa funzione ha un valore di ritorno
- // dichiarato come int
-
- return(latoA * latoB);
-}
+++ /dev/null
-/*
- Head tails
- Generates a random number in order to simulate a coin toss.
-
-
- Phisical LEDS and serial debug.
-
- This example code is in the public domain.
- */
-
-// Pin 13 has an LED connected on most Arduino boards.
-// give it a name:
-const int head = 13 ; // LED for HEAD
-const int tail = 12 ; // LEAD for Tails
-const int PAUSE = 1000 ;
-const int REST = 50 ;
-long randomNumber = 0L; // Use the "L" to tell compiler it's a long data type, not an int.
-int hCount = 0;
-int tCount = 0;
-int runs = 0 ;
-
-// the setup routine runs once when you press reset:
-void setup() {
- // initialize the digital pin as an output.
- pinMode(head, OUTPUT);
- pinMode(tail, OUTPUT);
- randomSeed(analogRead(0)); // Random initializer
- Serial.begin(9600);
- Serial.println("Initializing random sequence, please wait for results.");
-}
-
-// the loop routine runs over and over again forever:
-void loop() {
- randomNumber = random();
- digitalWrite(head, LOW);
- digitalWrite(tail, LOW);
- delay(REST); // wait a bit
- if (randomNumber % 2 == 1) {
- digitalWrite(head, HIGH);// turn the LED on ON
- hCount++ ;
- }
- else {
- digitalWrite(tail, HIGH);// turn the LED ON
- tCount++ ;
- }
-
- delay(PAUSE); // Long pause
- runs++;
-
- if (runs % 10 == 0) { // Each 10 runs print a summary to serial
- Serial.print("Results after more 10 runs, for a total of: ");
- Serial.println(runs);
- Serial.print("Tails: \t") ;
- Serial.println(tCount);
- Serial.print("Heads: \t");
- Serial.println(hCount);
- }
-}
-
-
-
+++ /dev/null
-void setup() {
- // put your setup code here, to run once:
- Serial.begin(9600);
-
-}
-
-void loop() {
-
- transforma(5); // Leggete i risultati con [CTR]+[SHIFT]+M
- transforma(255);
-
- Serial.flush() ;
- exit(0); // Termina l'esecuzione
-}
-
-// Ignorate pure il resto del listato!
-
-/* Transforma
-
- Scrive su seriale il valore della variabile a
- trasformandolo in binario e esadecimale
- */
-
-void transforma(int var) {
- Serial.print("Valore in decimanle = ");
- Serial.println(var); // Serial.println(a, DEC);
-
- Serial.print("Valore in binario = ");
- Serial.println(var,BIN);
-
- Serial.print("Valore in esadecimanle = ");
- Serial.println(var,HEX);
-
- Serial.println();
-}
-
-
-
-
-
+++ /dev/null
-void setup(){
- Serial.begin(9600);
-}
-
-void loop() {
-int latoA = 4;
-int latoB = 3 ;
-
-Serial.print("Area rettangolo = ");
-Serial.println(calcolaArea(latoA, latoB));
-
-
-Serial.flush();
-exit(0);
-}
-
-// Funzioni
-
-int calcolaArea(int a, int b){
- int area = a * b ;
- return area;
-}
-
+++ /dev/null
-/*
- Scope
-
- Variabili globali e locali rispetto a una funzione
- Le variabili dichiarate al di fuori di funzioni
- sono globali e accessibili a tutti,
-
- Le variabili dichiarate all'interno di una funzione
- sono locali e accessibili solo a questa,
- */
-
-
-boolean sposato = 1 ; // Variabile globale
-void setup(){
- Serial.begin(9600);
-}
-
-void loop() {
-Serial.print("In standard mode sposato = ");
-Serial.println(sposato);
-
-Serial.print("In vacanza mode sposato = ");
-Serial.println(inVacanza(sposato));
-
-Serial.print("Tornato a casa sposato = ");
-Serial.println(sposato);
-
-// Global
-Serial.print("In vacanza global mode sposato = ");
-Serial.println(inVacanzaG());
-
-Serial.print("Stato di sposato = ");
-Serial.println(sposato);
-
-// Termine programma
-Serial.flush();
-exit(0);
-}
-
-// Funzioni
-
-boolean inVacanza(boolean sposato){ // variabile locale
- sposato = 0 ;
- return sposato;
-}
-boolean inVacanzaG(){
- sposato = 0 ; // Variabile globale
- return sposato;
-}
--- /dev/null
+/*
+Fuznioni: return
+
+La funzione area accetta due parametri di tipo int
+e ritorna un int corrispondente all'area.
+*/
+
+void setup(){
+ Serial.begin(9600);
+}
+
+void loop() {
+int latoA = 4;
+int latoB = 3 ;
+
+Serial.print("Area rettangolo = ");
+Serial.println(calcolaArea(latoA, latoB));
+
+
+Serial.flush();
+exit(0);
+}
+
+// Funzioni
+
+int calcolaArea(int a, int b){
+ int area = a * b ;
+ return area;
+}
+
--- /dev/null
+/*
+ Blink con funzioni.
+
+ Le funzioni sono una sequenza di istruzione raggruppate appunto in un a funzione.
+ Le funzioni possono accettare argomenti e avere questi pre-impostati a valori di default se omessi.
+ Le funzioni possono limitarsi a svolgere istruzioni oppure elaborare valori restituendo un risultato.
+
+ */
+
+// Pin 13 has an LED connected on most Arduino boards.
+// give it a name:
+int led = 13;
+
+
+// the setup routine runs once when you press reset:
+void setup() {
+ // initialize the digital pin as an output.
+ pinMode(led, OUTPUT);
+}
+
+// the loop routine runs over and over again forever:
+void loop() {
+ lunga() ;
+ lunga() ;
+ breve();
+ breve();
+ varia(3000);
+}
+
+// Funzioni personalizzate: nella scheda funzioni.
--- /dev/null
+// Funzioni personalizzate
+// Un scheda e' un documento che viene concatenato allo sketch originale
+
+void lunga() {
+ // Blink con pausa lunga
+
+ digitalWrite(led, HIGH); // turn the LED on (HIGH is the voltage level)
+ delay(1000); // wait for a second
+ digitalWrite(led, LOW); // turn the LED off by making the voltage LOW
+ delay(1000); // wait for a second
+}
+
+void breve() {
+ // Blink con pausa breve
+
+ digitalWrite(led, HIGH); // turn the LED on (HIGH is the voltage level)
+ delay(200); // wait for a second
+ digitalWrite(led, LOW); // turn the LED off by making the voltage LOW
+ delay(1000); // wait for a second
+}
+
+void varia(int a = 300) { // Varia has a default value, the user can override it with an argument
+ // Lampeggia per un tempo impostato dall'utente,
+ // il default e' 300ms
+
+ digitalWrite(led, HIGH); // turn the LED on (HIGH is the voltage level)
+ delay(a); // wait for a second
+ digitalWrite(led, LOW); // turn the LED off by making the voltage LOW
+ delay(a); // wait for a second
+
+}
+
+void lampeggia(int ripetizioni) {
+ // Accende un LED per un numero stabilito di volte
+
+ // Questa funziona accetta un parametro: ripetizioni
+ int i = 0;
+ while (i < ripetizioni) {
+ rapido(); // accende e spegne rapidamente il LED
+ i = i + 1 ; // incrementa l'iteratore
+ // i++ ; // equivalente
+ }
+}
+
+int area(int latoA, int latoB) {
+ // Calcola l'area di un rettangolo
+ // e ritorna il valore calcolato: questa funzione ha un valore di ritorno
+ // dichiarato come int
+
+ return(latoA * latoB);
+}
--- /dev/null
+/*
+ Scope
+
+ Variabili globali e locali rispetto a una funzione
+ Le variabili dichiarate al di fuori di funzioni
+ sono globali e accessibili a tutti,
+
+ Le variabili dichiarate all'interno di una funzione
+ sono locali e accessibili solo a questa,
+ */
+
+
+boolean sposato = 1 ; // Variabile globale
+void setup(){
+ Serial.begin(9600);
+}
+
+void loop() {
+Serial.print("In standard mode sposato = ");
+Serial.println(sposato);
+
+Serial.print("In vacanza mode sposato = ");
+Serial.println(inVacanza(sposato));
+
+Serial.print("Tornato a casa sposato = ");
+Serial.println(sposato);
+
+// Global
+Serial.print("In vacanza global mode sposato = ");
+Serial.println(inVacanzaG());
+
+Serial.print("Stato di sposato = ");
+Serial.println(sposato);
+
+// Termine programma
+Serial.flush();
+exit(0);
+}
+
+// Funzioni
+
+boolean inVacanza(boolean sposato){ // variabile locale
+ sposato = 0 ;
+ return sposato;
+}
+boolean inVacanzaG(){
+ sposato = 0 ; // Variabile globale
+ return sposato;
+}
--- /dev/null
+/*
+ Head tails
+ Generates a random number in order to simulate a coin toss.
+
+
+ Phisical LEDS and serial debug.
+
+ This example code is in the public domain.
+ */
+
+// Pin 13 has an LED connected on most Arduino boards.
+// give it a name:
+const int head = 13 ; // LED for HEAD
+const int tail = 12 ; // LED for Tails
+const int PAUSE = 1000 ;
+const int REST = 50 ;
+long randomNumber = 0L; // Use the "L" to tell compiler it's a long data type, not an int.
+int hCount = 0;
+int tCount = 0;
+int runs = 0 ;
+
+// the setup routine runs once when you press reset:
+void setup() {
+ // initialize the digital pin as an output.
+ pinMode(head, OUTPUT);
+ pinMode(tail, OUTPUT);
+ randomSeed(analogRead(0)); // Random initializer
+ Serial.begin(9600);
+ Serial.println("Initializing random sequence, please wait for results.");
+}
+
+// the loop routine runs over and over again forever:
+void loop() {
+ randomNumber = random();
+ digitalWrite(head, LOW);
+ digitalWrite(tail, LOW);
+ delay(REST); // wait a bit
+ if (randomNumber % 2 == 1) {
+ digitalWrite(head, HIGH);// turn the LED on ON
+ hCount++ ;
+ }
+ else {
+ digitalWrite(tail, HIGH);// turn the LED ON
+ tCount++ ;
+ }
+
+ // Serial output
+ if (runs % 10 == 0) { // Each 10 runs print a summary to serial
+ Serial.print("Results after more 10 runs, for a total of: ");
+ Serial.println(runs);
+ Serial.print("Tails: \t") ;
+ Serial.println(tCount);
+ Serial.print("Heads: \t");
+ Serial.println(hCount);
+ }
+
+ delay(PAUSE); // Long pause
+ runs++;
+
+}
+
+
+
+++ /dev/null
-/**
- * Program: find out is the user typed in a leap year. The code assumes
- * the user is not an idiot and only types in numbers that are a valid
- * year.
- * Author: Dr. Purdum, Aug. 7, 2012
- **/
-void setup()
-{
- Serial.begin(9600);
-}
-void loop()
-{
- if (Serial.available() > 0) {
- int bufferCount;
- int year;
- char myData[20];
- bufferCount = ReadLine(myData);
- year = atoi(myData);
- Serial.print("Year: ");
- Serial.print(year);
- if (IsLeapYear(year)) {
- Serial.print(" is ");
- }
- else {
- Serial.print(" is not ");
- }
- Serial.println("a leap year");
- }
-}
-// Convert to int
-/*****
- * Purpose: Determine if a given year is a leap year
- * Parameters:
- * int yr
- * The year to test
- * Return value:
- * int
- * 1 if the year is a leap year, 0 otherwise
- *****/
-int IsLeapYear(int yr)
-{
- if (yr % 4 == 0 && yr % 100 != 0 || yr % 400 == 0) {
- return 1; // It is a leap year
- }
- else {
- return 0;
- // not a leap year
- }
-}
-/*****
- * Purpose: Read data from serial port until a newline character is read ('\n')
- * Parameters:
- * char str[]
- * character array that will be treated as a nul-terminated string
- * Return value:
- * int
- * the number of characters read for the string
- * CAUTION: This method will sit here forever if no input is read from the serial
- * port and no newline character is entered.
- ****/
-int ReadLine(char str[])
-{
- char c;
- int index = 0;
- while (true) {
- if (Serial.available() > 0) {
- c = Serial.read();
- if (c != '\n') {
- str[index++] = c;
- }
- else {
- str[index] = '\0'; // null termination character
- break;
- }
- }
- }
- return index;
-}
-
-
+Esercizi sulle comunicazioni seriali
+--------------------------------------
+
- Simple serial communication 2 arduino: switch 1 and LED 2
- Serial Parser: Examples -> strings
- Serial to RGB LED
+++ /dev/null
-
-//**************************************************************//
-// Name : shiftOutCode, Hello World
-// Author : Carlyn Maw,Tom Igoe, David A. Mellis
-// Date : 25 Oct, 2006
-// Modified: 23 Mar 2010
-// Version : 2.0
-// Notes : Code for using a 74HC595 Shift Register //
-// : to count from 0 to 255
-//****************************************************************
-
-//Pin connected to ST_CP of 74HC595
-int latchPin = 8;
-//Pin connected to SH_CP of 74HC595
-int clockPin = 12;
-////Pin connected to DS of 74HC595
-int dataPin = 11;
-
-
-
-void setup() {
- //set pins to output so you can control the shift register
- pinMode(latchPin, OUTPUT);
- pinMode(clockPin, OUTPUT);
- pinMode(dataPin, OUTPUT);
- // Serial Debug
- Serial.begin(9600);
- Serial.print("Decimal");
- Serial.print("\t");
- Serial.println("Binary");
-}
-
-void loop() {
- // count from 0 to 255 and display the number
- // on the LEDs
- for (int numberToDisplay = 0; numberToDisplay < 256; numberToDisplay++) {
- // take the latchPin low so
- // the LEDs don't change while you're sending in bits:
- digitalWrite(latchPin, LOW);
-
- // shift out the bits:
- shiftOut(dataPin, clockPin, MSBFIRST, numberToDisplay);
-
- //take the latch pin high so the LEDs will light up:
- digitalWrite(latchPin, HIGH);
- // Serial Debug
- Serial.print(numberToDisplay);
- Serial.print("\t");
- Serial.println(numberToDisplay, BIN);
- // pause before next value:
- delay(200);
- }
-}
-
-
-
-
+++ /dev/null
-//**************************************************************//
-// Name : shiftOutCode, Hello World
-// Author : Carlyn Maw,Tom Igoe, David A. Mellis
-// Date : 25 Oct, 2006
-// Modified: 23 Mar 2010
-// Version : 2.0
-// Notes : Code for using a 74HC595 Shift Register
-//
-// : to count from 0 to 255
-//****************************************************************
-
-
-//Pin connected to ST_CP of 74HC595
-int latchPin = 8;
-//Pin connected to SH_CP of 74HC595
-int clockPin = 12;
-////Pin connected to DS of 74HC595
-int dataPin = 11;
-void setup() {
- //set pins to output so you can control the shift register
- pinMode(latchPin, OUTPUT);
-
-pinMode(clockPin, OUTPUT);
-pinMode(dataPin, OUTPUT);
-}
-void loop() {
- // count from 0 to 255 and display the number
- // on the LEDs
- for (int numberToDisplay = 0; numberToDisplay < 256; numberToDisplay++) {
- // take the latchPin low so
- // the LEDs don’t change while you’re sending in bits:
- digitalWrite(latchPin, LOW);
- // shift out the bits:
- shiftOut(dataPin, clockPin, MSBFIRST, numberToDisplay);
- //take the latch pin high so the LEDs will light up:
- digitalWrite(latchPin, HIGH);
- // pause before next value:
- delay(500);
- }
-}
-
-
-
+++ /dev/null
-
-/*
- Shift Register Example
- Turning on the outputs of a 74HC595 using an array
-
- Hardware:
- * 74HC595 shift register
- * LEDs attached to each of the outputs of the shift register
-
- */
-//Pin connected to ST_CP of 74HC595
-int latchPin = 8;
-//Pin connected to SH_CP of 74HC595
-int clockPin = 12;
-////Pin connected to DS of 74HC595
-int dataPin = 11;
-
-//holders for infromation you're going to pass to shifting function
-byte data;
-byte dataArray[10];
-
-void setup() {
- //set pins to output because they are addressed in the main loop
- pinMode(latchPin, OUTPUT);
- Serial.begin(9600);
-
- //Arduino doesn't seem to have a way to write binary straight into the code
- //so these values are in HEX. Decimal would have been fine, too.
- dataArray[0] = 0xFF; //11111111
- dataArray[1] = 0xFE; //11111110
- dataArray[2] = 0xFC; //11111100
- dataArray[3] = 0xF8; //11111000
- dataArray[4] = 0xF0; //11110000
- dataArray[5] = 0xE0; //11100000
- dataArray[6] = 0xC0; //11000000
- dataArray[7] = 0x80; //10000000
- dataArray[8] = 0x00; //00000000
- dataArray[9] = 0x38; //11100000
-
- //function that blinks all the LEDs
- //gets passed the number of blinks and the pause time
- blinkAll_2Bytes(2,500);
-}
-
-void loop() {
-
- for (int j = 0; j < 10; j++) {
- //load the light sequence you want from array
- data = dataArray[j];
- //ground latchPin and hold low for as long as you are transmitting
- digitalWrite(latchPin, 0);
- //move 'em out
- shiftOut(dataPin, clockPin, data);
- //return the latch pin high to signal chip that it
- //no longer needs to listen for information
- digitalWrite(latchPin, 1);
- delay(300);
- }
-}
-
-
-
-// the heart of the program
-void shiftOut(int myDataPin, int myClockPin, byte myDataOut) {
- // This shifts 8 bits out MSB first,
- //on the rising edge of the clock,
- //clock idles low
-
- //internal function setup
- int i=0;
- int pinState;
- pinMode(myClockPin, OUTPUT);
- pinMode(myDataPin, OUTPUT);
-
- //clear everything out just in case to
- //prepare shift register for bit shifting
- digitalWrite(myDataPin, 0);
- digitalWrite(myClockPin, 0);
-
- //for each bit in the byte myDataOut�
- //NOTICE THAT WE ARE COUNTING DOWN in our for loop
- //This means that %00000001 or "1" will go through such
- //that it will be pin Q0 that lights.
- for (i=7; i>=0; i--) {
- digitalWrite(myClockPin, 0);
-
- //if the value passed to myDataOut and a bitmask result
- // true then... so if we are at i=6 and our value is
- // %11010100 it would the code compares it to %01000000
- // and proceeds to set pinState to 1.
- if ( myDataOut & (1<<i) ) {
- pinState= 1;
- }
- else {
- pinState= 0;
- }
-
- //Sets the pin to HIGH or LOW depending on pinState
- digitalWrite(myDataPin, pinState);
- //register shifts bits on upstroke of clock pin
- digitalWrite(myClockPin, 1);
- //zero the data pin after shift to prevent bleed through
- digitalWrite(myDataPin, 0);
- }
-
- //stop shifting
- digitalWrite(myClockPin, 0);
-}
-
-
-//blinks the whole register based on the number of times you want to
-//blink "n" and the pause between them "d"
-//starts with a moment of darkness to make sure the first blink
-//has its full visual effect.
-void blinkAll_2Bytes(int n, int d) {
- digitalWrite(latchPin, 0);
- shiftOut(dataPin, clockPin, 0);
- shiftOut(dataPin, clockPin, 0);
- digitalWrite(latchPin, 1);
- delay(200);
- for (int x = 0; x < n; x++) {
- digitalWrite(latchPin, 0);
- shiftOut(dataPin, clockPin, 255);
- shiftOut(dataPin, clockPin, 255);
- digitalWrite(latchPin, 1);
- delay(d);
- digitalWrite(latchPin, 0);
- shiftOut(dataPin, clockPin, 0);
- shiftOut(dataPin, clockPin, 0);
- digitalWrite(latchPin, 1);
- delay(d);
- }
-}
-
-
-
+++ /dev/null
-/* SuperCar like pattern with a shift register.
- Note: first bit/LED is supposed to be 0 and not 7
- as in many arduino example sketches.
-
- Turning on the outputs of a 74HC595 using an array
-
- Hardware:
- * 74HC595 shift register
- * LEDs attached to each of the outputs of the shift register
- */
-
-int clockPin = 12; //IC Pin 11, Yellow Jumper
-int dataPin = 11; //IC Pin 14, Blue Jumper
-int latchPin = 8; //IC Pin 12, Green Jumper
-
-byte patterns[30] = {
- B00000001, 100,
- B00000010, 100,
- B00000100, 100,
- B00001000, 100,
- B00010000, 100,
- B00100000, 100,
- B01000000, 100,
- B10000000, 100,
- B01000000, 100,
- B00100000, 100,
- B00010000, 100,
- B00001000, 100,
- B00000100, 100,
- B00000010, 100
-};
-
-int index = 0;
-int count = sizeof(patterns) / 2;
-
-void setup() {
- pinMode(latchPin, OUTPUT);
- pinMode(clockPin, OUTPUT);
- pinMode(dataPin, OUTPUT);
-}
-
-void loop() {
- digitalWrite(latchPin, LOW);
- shiftOut(dataPin, clockPin, MSBFIRST, patterns[index * 2]);
- digitalWrite(latchPin, HIGH);
- delay(patterns[(index * 2) + 1]);
- index++;
- if (index >= count){
- index = 0;
- }
-}
-
+++ /dev/null
-
-/*
- Shift Register Example
- Turning on the outputs of a 74HC595 using an array
-
- Hardware:
- * 74HC595 shift register
- * LEDs attached to each of the outputs of the shift register
-
- */
-//Pin connected to ST_CP of 74HC595
-int latchPin = 8;
-//Pin connected to SH_CP of 74HC595
-int clockPin = 12;
-////Pin connected to DS of 74HC595
-int dataPin = 11;
-
-//holders for infromation you're going to pass to shifting function
-byte data;
-byte dataArray[10];
-
-void setup() {
- //set pins to output because they are addressed in the main loop
- pinMode(latchPin, OUTPUT);
- Serial.begin(9600);
-
- //Arduino doesn't seem to have a way to write binary straight into the code
- //so these values are in HEX. Decimal would have been fine, too.
- dataArray[0] = 0xFF; //11111111
- dataArray[1] = 0xFE; //11111110
- dataArray[2] = 0xFC; //11111100
- dataArray[3] = 0xF8; //11111000
- dataArray[4] = 0xF0; //11110000
- dataArray[5] = 0xE0; //11100000
- dataArray[6] = 0xC0; //11000000
- dataArray[7] = 0x80; //10000000
- dataArray[8] = 0x00; //00000000
- dataArray[9] = 0x38; //11100000
-
- //function that blinks all the LEDs
- //gets passed the number of blinks and the pause time
- blinkAll_2Bytes(2,500);
-}
-
-void loop() {
-
- for (int j = 0; j < 10; j++) {
- //load the light sequence you want from array
- data = dataArray[j];
- //ground latchPin and hold low for as long as you are transmitting
- digitalWrite(latchPin, 0);
- //move 'em out
- shiftOut(dataPin, clockPin, data);
- //return the latch pin high to signal chip that it
- //no longer needs to listen for information
- digitalWrite(latchPin, 1);
- delay(300);
- }
-}
-
-
-
-// the heart of the program
-void shiftOut(int myDataPin, int myClockPin, byte myDataOut) {
- // This shifts 8 bits out MSB first,
- //on the rising edge of the clock,
- //clock idles low
-
- //internal function setup
- int i=0;
- int pinState;
- pinMode(myClockPin, OUTPUT);
- pinMode(myDataPin, OUTPUT);
-
- //clear everything out just in case to
- //prepare shift register for bit shifting
- digitalWrite(myDataPin, 0);
- digitalWrite(myClockPin, 0);
-
- //for each bit in the byte myDataOut�
- //NOTICE THAT WE ARE COUNTING DOWN in our for loop
- //This means that %00000001 or "1" will go through such
- //that it will be pin Q0 that lights.
- for (i=7; i>=0; i--) {
- digitalWrite(myClockPin, 0);
-
- //if the value passed to myDataOut and a bitmask result
- // true then... so if we are at i=6 and our value is
- // %11010100 it would the code compares it to %01000000
- // and proceeds to set pinState to 1.
- if ( myDataOut & (1<<i) ) {
- pinState= 1;
- }
- else {
- pinState= 0;
- }
-
- //Sets the pin to HIGH or LOW depending on pinState
- digitalWrite(myDataPin, pinState);
- //register shifts bits on upstroke of clock pin
- digitalWrite(myClockPin, 1);
- //zero the data pin after shift to prevent bleed through
- digitalWrite(myDataPin, 0);
- }
-
- //stop shifting
- digitalWrite(myClockPin, 0);
-}
-
-
-//blinks the whole register based on the number of times you want to
-//blink "n" and the pause between them "d"
-//starts with a moment of darkness to make sure the first blink
-//has its full visual effect.
-void blinkAll_2Bytes(int n, int d) {
- digitalWrite(latchPin, 0);
- shiftOut(dataPin, clockPin, 0);
- shiftOut(dataPin, clockPin, 0);
- digitalWrite(latchPin, 1);
- delay(200);
- for (int x = 0; x < n; x++) {
- digitalWrite(latchPin, 0);
- shiftOut(dataPin, clockPin, 255);
- shiftOut(dataPin, clockPin, 255);
- digitalWrite(latchPin, 1);
- delay(d);
- digitalWrite(latchPin, 0);
- shiftOut(dataPin, clockPin, 0);
- shiftOut(dataPin, clockPin, 0);
- digitalWrite(latchPin, 1);
- delay(d);
- }
-}
-
-
-
+++ /dev/null
-/*
- Shift Register Example
- for 74HC595 shift register
-
- This sketch turns reads serial input and uses it to set the pins
- of a 74HC595 shift register.
-
- Hardware:
- * 74HC595 shift register attached to pins 8, 12, and 11 of the Arduino,
- as detailed below.
- * LEDs attached to each of the outputs of the shift register
-
- Created 22 May 2009
- Created 23 Mar 2010
- by Tom Igoe
-
- */
-
-//Pin connected to latch pin (ST_CP) of 74HC595
-const int latchPin = 8;
-//Pin connected to clock pin (SH_CP) of 74HC595
-const int clockPin = 12;
-////Pin connected to Data in (DS) of 74HC595
-const int dataPin = 11;
-
-void setup() {
- //set pins to output because they are addressed in the main loop
- pinMode(latchPin, OUTPUT);
- pinMode(dataPin, OUTPUT);
- pinMode(clockPin, OUTPUT);
- Serial.begin(9600);
- Serial.println("reset");
-}
-
-void loop() {
- if (Serial.available() > 0) {
- // ASCII '0' through '9' characters are
- // represented by the values 48 through 57.
- // so if the user types a number from 0 through 9 in ASCII,
- // you can subtract 48 to get the actual value:
- int bitToSet = Serial.read() - 48;
-
- // write to the shift register with the correct bit set high:
- registerWrite(bitToSet, HIGH);
- }
-}
-
-// This method sends bits to the shift register:
-
-void registerWrite(int whichPin, int whichState) {
-// the bits you want to send
- byte bitsToSend = 0;
-
- // turn off the output so the pins don't light up
- // while you're shifting bits:
- digitalWrite(latchPin, LOW);
-
- // turn on the next highest bit in bitsToSend:
- bitWrite(bitsToSend, whichPin, whichState);
-
- // shift the bits out:
- shiftOut(dataPin, clockPin, MSBFIRST, bitsToSend);
-
- // turn on the output so the LEDs can light up:
- digitalWrite(latchPin, HIGH);
- delay(300);
-
-}
-