]> git.piffa.net Git - scratch/commitdiff
S4a Esercizi per Scratch for Arduino
authoreaman <eaman@conny.andreamanni.com>
Fri, 9 Oct 2015 13:29:57 +0000 (15:29 +0200)
committereaman <eaman@conny.andreamanni.com>
Fri, 9 Oct 2015 13:29:57 +0000 (15:29 +0200)
30 files changed:
.README.txt.swp [new file with mode: 0644]
README.html [new file with mode: 0644]
README.txt [new file with mode: 0644]
S4AFirmware16.ino [new file with mode: 0644]
esercizi/analog_input_6.sb [new file with mode: 0644]
esercizi/analog_input_output_7.sb [new file with mode: 0644]
esercizi/blink_1.sb [new file with mode: 0644]
esercizi/blink_led_esterno_2.sb [new file with mode: 0644]
esercizi/bottone_ciclo_condizionele_4.sb [new file with mode: 0644]
esercizi/bottone_effetto.sb [new file with mode: 0644]
esercizi/ciclo_iterattivo_5.sb [new file with mode: 0644]
esercizi/input_condizionale_3.sb [new file with mode: 0644]
esercizi/input_condizionale_suono.sb [new file with mode: 0644]
schemi_breadboard/.directory [new file with mode: 0644]
schemi_breadboard/blink_led_esterno_2.png [new file with mode: 0644]
schemi_breadboard/bottone_ciclo_condizionele_4.png [new file with mode: 0644]
schemi_breadboard/circuito_led_bb.png [new file with mode: 0644]
schemi_breadboard/circuito_led_button_bb.png [new file with mode: 0644]
schemi_breadboard/photoresistor_led.png [new file with mode: 0644]
schemi_breadboard/potenziometro.png [new file with mode: 0644]
screenshots/.directory [new file with mode: 0644]
screenshots/analog_input_6.png [new file with mode: 0644]
screenshots/analog_input_output_7.png [new file with mode: 0644]
screenshots/blink.png [new file with mode: 0644]
screenshots/bottone_ciclo_condizionele.png [new file with mode: 0644]
screenshots/ciclo_condizionale_suono.png [new file with mode: 0644]
screenshots/ciclo_iterativo.png [new file with mode: 0644]
screenshots/ciclo_iterativo_5.png [new file with mode: 0644]
screenshots/input_condizionale_suono.png [new file with mode: 0644]
screenshots/movimento_sprite_potenziometro.png [new file with mode: 0644]

diff --git a/.README.txt.swp b/.README.txt.swp
new file mode 100644 (file)
index 0000000..4dd5da6
Binary files /dev/null and b/.README.txt.swp differ
diff --git a/README.html b/README.html
new file mode 100644 (file)
index 0000000..71ee5a1
--- /dev/null
@@ -0,0 +1,416 @@
+<?xml version="1.0" encoding="utf-8" ?>
+<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
+<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
+<head>
+<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
+<meta name="generator" content="Docutils 0.11: http://docutils.sourceforge.net/" />
+<title></title>
+<style type="text/css">
+
+/*
+:Author: David Goodger (goodger@python.org)
+:Id: $Id: html4css1.css 7614 2013-02-21 15:55:51Z milde $
+:Copyright: This stylesheet has been placed in the public domain.
+
+Default cascading style sheet for the HTML output of Docutils.
+
+See http://docutils.sf.net/docs/howto/html-stylesheets.html for how to
+customize this style sheet.
+*/
+
+/* used to remove borders from tables and images */
+.borderless, table.borderless td, table.borderless th {
+  border: 0 }
+
+table.borderless td, table.borderless th {
+  /* Override padding for "table.docutils td" with "! important".
+     The right padding separates the table cells. */
+  padding: 0 0.5em 0 0 ! important }
+
+.first {
+  /* Override more specific margin styles with "! important". */
+  margin-top: 0 ! important }
+
+.last, .with-subtitle {
+  margin-bottom: 0 ! important }
+
+.hidden {
+  display: none }
+
+a.toc-backref {
+  text-decoration: none ;
+  color: black }
+
+blockquote.epigraph {
+  margin: 2em 5em ; }
+
+dl.docutils dd {
+  margin-bottom: 0.5em }
+
+object[type="image/svg+xml"], object[type="application/x-shockwave-flash"] {
+  overflow: hidden;
+}
+
+/* Uncomment (and remove this text!) to get bold-faced definition list terms
+dl.docutils dt {
+  font-weight: bold }
+*/
+
+div.abstract {
+  margin: 2em 5em }
+
+div.abstract p.topic-title {
+  font-weight: bold ;
+  text-align: center }
+
+div.admonition, div.attention, div.caution, div.danger, div.error,
+div.hint, div.important, div.note, div.tip, div.warning {
+  margin: 2em ;
+  border: medium outset ;
+  padding: 1em }
+
+div.admonition p.admonition-title, div.hint p.admonition-title,
+div.important p.admonition-title, div.note p.admonition-title,
+div.tip p.admonition-title {
+  font-weight: bold ;
+  font-family: sans-serif }
+
+div.attention p.admonition-title, div.caution p.admonition-title,
+div.danger p.admonition-title, div.error p.admonition-title,
+div.warning p.admonition-title, .code .error {
+  color: red ;
+  font-weight: bold ;
+  font-family: sans-serif }
+
+/* Uncomment (and remove this text!) to get reduced vertical space in
+   compound paragraphs.
+div.compound .compound-first, div.compound .compound-middle {
+  margin-bottom: 0.5em }
+
+div.compound .compound-last, div.compound .compound-middle {
+  margin-top: 0.5em }
+*/
+
+div.dedication {
+  margin: 2em 5em ;
+  text-align: center ;
+  font-style: italic }
+
+div.dedication p.topic-title {
+  font-weight: bold ;
+  font-style: normal }
+
+div.figure {
+  margin-left: 2em ;
+  margin-right: 2em }
+
+div.footer, div.header {
+  clear: both;
+  font-size: smaller }
+
+div.line-block {
+  display: block ;
+  margin-top: 1em ;
+  margin-bottom: 1em }
+
+div.line-block div.line-block {
+  margin-top: 0 ;
+  margin-bottom: 0 ;
+  margin-left: 1.5em }
+
+div.sidebar {
+  margin: 0 0 0.5em 1em ;
+  border: medium outset ;
+  padding: 1em ;
+  background-color: #ffffee ;
+  width: 40% ;
+  float: right ;
+  clear: right }
+
+div.sidebar p.rubric {
+  font-family: sans-serif ;
+  font-size: medium }
+
+div.system-messages {
+  margin: 5em }
+
+div.system-messages h1 {
+  color: red }
+
+div.system-message {
+  border: medium outset ;
+  padding: 1em }
+
+div.system-message p.system-message-title {
+  color: red ;
+  font-weight: bold }
+
+div.topic {
+  margin: 2em }
+
+h1.section-subtitle, h2.section-subtitle, h3.section-subtitle,
+h4.section-subtitle, h5.section-subtitle, h6.section-subtitle {
+  margin-top: 0.4em }
+
+h1.title {
+  text-align: center }
+
+h2.subtitle {
+  text-align: center }
+
+hr.docutils {
+  width: 75% }
+
+img.align-left, .figure.align-left, object.align-left {
+  clear: left ;
+  float: left ;
+  margin-right: 1em }
+
+img.align-right, .figure.align-right, object.align-right {
+  clear: right ;
+  float: right ;
+  margin-left: 1em }
+
+img.align-center, .figure.align-center, object.align-center {
+  display: block;
+  margin-left: auto;
+  margin-right: auto;
+}
+
+.align-left {
+  text-align: left }
+
+.align-center {
+  clear: both ;
+  text-align: center }
+
+.align-right {
+  text-align: right }
+
+/* reset inner alignment in figures */
+div.align-right {
+  text-align: inherit }
+
+/* div.align-center * { */
+/*   text-align: left } */
+
+ol.simple, ul.simple {
+  margin-bottom: 1em }
+
+ol.arabic {
+  list-style: decimal }
+
+ol.loweralpha {
+  list-style: lower-alpha }
+
+ol.upperalpha {
+  list-style: upper-alpha }
+
+ol.lowerroman {
+  list-style: lower-roman }
+
+ol.upperroman {
+  list-style: upper-roman }
+
+p.attribution {
+  text-align: right ;
+  margin-left: 50% }
+
+p.caption {
+  font-style: italic }
+
+p.credits {
+  font-style: italic ;
+  font-size: smaller }
+
+p.label {
+  white-space: nowrap }
+
+p.rubric {
+  font-weight: bold ;
+  font-size: larger ;
+  color: maroon ;
+  text-align: center }
+
+p.sidebar-title {
+  font-family: sans-serif ;
+  font-weight: bold ;
+  font-size: larger }
+
+p.sidebar-subtitle {
+  font-family: sans-serif ;
+  font-weight: bold }
+
+p.topic-title {
+  font-weight: bold }
+
+pre.address {
+  margin-bottom: 0 ;
+  margin-top: 0 ;
+  font: inherit }
+
+pre.literal-block, pre.doctest-block, pre.math, pre.code {
+  margin-left: 2em ;
+  margin-right: 2em }
+
+pre.code .ln { color: grey; } /* line numbers */
+pre.code, code { background-color: #eeeeee }
+pre.code .comment, code .comment { color: #5C6576 }
+pre.code .keyword, code .keyword { color: #3B0D06; font-weight: bold }
+pre.code .literal.string, code .literal.string { color: #0C5404 }
+pre.code .name.builtin, code .name.builtin { color: #352B84 }
+pre.code .deleted, code .deleted { background-color: #DEB0A1}
+pre.code .inserted, code .inserted { background-color: #A3D289}
+
+span.classifier {
+  font-family: sans-serif ;
+  font-style: oblique }
+
+span.classifier-delimiter {
+  font-family: sans-serif ;
+  font-weight: bold }
+
+span.interpreted {
+  font-family: sans-serif }
+
+span.option {
+  white-space: nowrap }
+
+span.pre {
+  white-space: pre }
+
+span.problematic {
+  color: red }
+
+span.section-subtitle {
+  /* font-size relative to parent (h1..h6 element) */
+  font-size: 80% }
+
+table.citation {
+  border-left: solid 1px gray;
+  margin-left: 1px }
+
+table.docinfo {
+  margin: 2em 4em }
+
+table.docutils {
+  margin-top: 0.5em ;
+  margin-bottom: 0.5em }
+
+table.footnote {
+  border-left: solid 1px black;
+  margin-left: 1px }
+
+table.docutils td, table.docutils th,
+table.docinfo td, table.docinfo th {
+  padding-left: 0.5em ;
+  padding-right: 0.5em ;
+  vertical-align: top }
+
+table.docutils th.field-name, table.docinfo th.docinfo-name {
+  font-weight: bold ;
+  text-align: left ;
+  white-space: nowrap ;
+  padding-left: 0 }
+
+/* "booktabs" style (no vertical lines) */
+table.docutils.booktabs {
+  border: 0px;
+  border-top: 2px solid;
+  border-bottom: 2px solid;
+  border-collapse: collapse;
+}
+table.docutils.booktabs * {
+  border: 0px;
+}
+table.docutils.booktabs th {
+  border-bottom: thin solid;
+  text-align: left;
+}
+
+h1 tt.docutils, h2 tt.docutils, h3 tt.docutils,
+h4 tt.docutils, h5 tt.docutils, h6 tt.docutils {
+  font-size: 100% }
+
+ul.auto-toc {
+  list-style-type: none }
+
+</style>
+</head>
+<body>
+<div class="document">
+
+
+<div class="section" id="s4a-scaratch-for-arduino">
+<h1>S4a: Scaratch for Arduino</h1>
+<p>S4a e' un ambiende di sviluppo educativo ottimizzato per connettersi con
+i microcontroller Arduino.</p>
+<ul class="simple">
+<li><a class="reference external" href="http://s4a.cat/">http://s4a.cat/</a></li>
+<li><a class="reference external" href="https://scratch.mit.edu/about/">https://scratch.mit.edu/about/</a></li>
+</ul>
+<p>Per poter connettere il microcontroller Arduino a s4a si dovra' caricare il
+firmware S4AFirmware16.ino sulla scheda <strong>prima di lanciare s4a</strong>.</p>
+<ul class="simple">
+<li>Download: <a class="reference external" href="http://vps34736.ovh.net/S4A/S4AFirmware16.ino">http://vps34736.ovh.net/S4A/S4AFirmware16.ino</a></li>
+<li><a class="reference external" href="http://s4a.cat/index.html#downloads">http://s4a.cat/index.html#downloads</a></li>
+</ul>
+</div>
+<div class="section" id="link-a-risorse-utili">
+<h1>Link a risorse utili:</h1>
+<p>Documentazione e guide</p>
+<div class="section" id="italiano">
+<h2>Italiano</h2>
+<ul class="simple">
+<li><a class="reference external" href="http://www.maffucci.it/2014/03/29/micro-corso-di-scratch-per-incominciare-ad-imparare-a-programmare/">http://www.maffucci.it/2014/03/29/micro-corso-di-scratch-per-incominciare-ad-imparare-a-programmare/</a></li>
+<li><a class="reference external" href="http://www.maffucci.it/2015/03/27/corso-di-scratch-lezione-1/">http://www.maffucci.it/2015/03/27/corso-di-scratch-lezione-1/</a></li>
+<li><a class="reference external" href="http://www.maffucci.it/2015/04/09/corso-di-scratch-lezione-2/">http://www.maffucci.it/2015/04/09/corso-di-scratch-lezione-2/</a></li>
+<li><a class="reference external" href="http://www.prometheusnow.com/blog/s4a-programmare-arduino-scratch/">http://www.prometheusnow.com/blog/s4a-programmare-arduino-scratch/</a></li>
+<li><a class="reference external" href="http://gzuliani.bitbucket.org/scratch/scratch-arduino.html">http://gzuliani.bitbucket.org/scratch/scratch-arduino.html</a></li>
+<li><a class="reference external" href="http://www.mastropaolo.net/scratch">http://www.mastropaolo.net/scratch</a></li>
+</ul>
+</div>
+<div class="section" id="inglese">
+<h2>Inglese</h2>
+<ul class="simple">
+<li><a class="reference external" href="https://djanesblog.wordpress.com/category/circuit-3/">https://djanesblog.wordpress.com/category/circuit-3/</a></li>
+<li><a class="reference external" href="http://s4a.cat/index.html#docs">http://s4a.cat/index.html#docs</a></li>
+</ul>
+</div>
+</div>
+<div class="section" id="risorse">
+<h1>Risorse</h1>
+<p>Questo archivio e' disponibile su:</p>
+<ul class="simple">
+<li><a class="reference external" href="http://lab.piffa.net/scratch">http://lab.piffa.net/scratch</a></li>
+<li><a class="reference external" href="http://git.andreamanni.com/">http://git.andreamanni.com/</a> <a class="reference external" href="http://git.andreamanni.com/web">http://git.andreamanni.com/web</a></li>
+</ul>
+<p>In questo archivio troverete:</p>
+<div class="section" id="esercizi">
+<h2>Esercizi</h2>
+<p>Nella cartella <tt class="docutils literal">esercizi</tt> sono contenuti i primi esempi relativi a fondamenti
+di programmazione, sono ordinati per numero e coprono:</p>
+<ul class="simple">
+<li>istruzioni</li>
+<li>sequenze di istruzioni</li>
+<li>cicli condizionali (input da tastiera e bottone fisico)</li>
+<li>cicli iterativi (ripetizione di movimenti)</li>
+<li>input analogico (da un potenziometro o fotoresistenza)</li>
+<li>output analogico PWM (relativo a un input analogico)</li>
+</ul>
+<p>Qesti esercizi nascono come introduzione alle versioni classiche in C++ che
+possono essere affrontate successivamente:
+- <a class="reference external" href="http://lab.piffa.net/sketchbook_andrea/">http://lab.piffa.net/sketchbook_andrea/</a></p>
+</div>
+<div class="section" id="screenshot">
+<h2>Screenshot</h2>
+<p>Diagrammi di flusso degli sketch in formato png.</p>
+</div>
+<div class="section" id="schemi">
+<h2>Schemi</h2>
+<p>Schemi dei circuiti elettrici sulla breadboard.</p>
+</div>
+</div>
+</div>
+</body>
+</html>
diff --git a/README.txt b/README.txt
new file mode 100644 (file)
index 0000000..6d7af01
--- /dev/null
@@ -0,0 +1,79 @@
+S4a: Scaratch for Arduino
+=========================
+
+S4a e' un ambiende di sviluppo educativo ottimizzato per connettersi con 
+i microcontroller Arduino.
+
+- http://s4a.cat/
+- https://scratch.mit.edu/about/
+
+
+Per poter connettere il microcontroller Arduino a s4a si dovra' caricare il
+firmware S4AFirmware16.ino sulla scheda **prima di lanciare s4a**.
+
+- Download: http://vps34736.ovh.net/S4A/S4AFirmware16.ino
+- http://s4a.cat/index.html#downloads
+
+
+Link a risorse utili:
+=======================
+
+Documentazione e guide
+
+Italiano
+-----------
+
+- http://www.maffucci.it/2014/03/29/micro-corso-di-scratch-per-incominciare-ad-imparare-a-programmare/
+- http://www.maffucci.it/2015/03/27/corso-di-scratch-lezione-1/
+- http://www.maffucci.it/2015/04/09/corso-di-scratch-lezione-2/
+- http://www.prometheusnow.com/blog/s4a-programmare-arduino-scratch/
+- http://gzuliani.bitbucket.org/scratch/scratch-arduino.html
+- http://www.mastropaolo.net/scratch
+
+Inglese
+-----------
+
+- https://djanesblog.wordpress.com/category/circuit-3/
+- http://s4a.cat/index.html#docs
+
+
+Risorse
+========
+
+Questo archivio e' disponibile su:
+
+- http://lab.piffa.net/scratch
+- http://git.andreamanni.com/ http://git.andreamanni.com/web
+
+
+In questo archivio troverete:
+
+Esercizi
+---------
+Nella cartella ``esercizi`` sono contenuti i primi esempi relativi a fondamenti
+di programmazione, sono ordinati per numero e coprono:
+
+- istruzioni
+- sequenze di istruzioni
+- cicli condizionali (input da tastiera e bottone fisico)
+- cicli iterativi (ripetizione di movimenti)
+- input analogico (da un potenziometro o fotoresistenza)
+- output analogico PWM (relativo a un input analogico)
+
+
+Qesti esercizi nascono come introduzione alle versioni classiche in C++ che
+possono essere affrontate successivamente:
+- http://lab.piffa.net/sketchbook_andrea/
+
+
+Screenshot
+-----------
+Diagrammi di flusso degli sketch in formato png.
+
+
+
+Schemi
+--------
+Schemi dei circuiti elettrici sulla breadboard.
+
+
diff --git a/S4AFirmware16.ino b/S4AFirmware16.ino
new file mode 100644 (file)
index 0000000..1423c20
--- /dev/null
@@ -0,0 +1,237 @@
+// NEW IN VERSION 1.6c (by Jorge Gomez):
+// Fixed variable type in pin structure: pin.state should be int, not byte
+// Optimized speed of execution while receiving data from computer in readSerialPort()
+
+// NEW IN VERSION 1.6b (by Jorge Gomez):
+// Added new structure arduinoPins to hold the pins information:
+//  - This makes the code easier to read and modify (IMHO)
+//  - Allows to change the type of pin more easily to meet non standard use of S4A
+//  - Eliminates the need of having to deal with different kind of index access (ie: states[pin-4])
+//  - By using an enum to hold all the possible output pin states the code is now more readable
+// Changed all functions using old style pin access: configurePins(), resetPins(), readSerialPort(), updateActuator() and sendUpdateActuator()
+// Fixed possible overflow every 70 minutes (2e32 us) in pulse() while using micros(). Changed for delayMicroseconds()
+// Some minor coding style fixes
+
+// NEW IN VERSION 1.6a  (by Jorge Gomez):
+// Fixed compatibility with Arduino Leonardo by avoiding the use of timers
+// readSerialPort() optimized:
+//  - created state machine for reading the two bytes of the S4A message
+//  - updateActuator() is only called if the state is changed 
+// Memory use optimization
+// Cleaning some parts of code 
+// Avoid using some global variables
+
+// NEW IN VERSION 1.6:
+// Refactored reset pins
+// Merged code for standard and CR servos
+// Merged patch for Leonardo from Peter Mueller (many thanks for this!)
+
+// NEW IN VERSION 1.5:
+// Changed pin 8 from standard servo to normal digital output
+
+// NEW IN VERSION 1.4:
+// Changed Serial.print() for Serial.write() in ScratchBoardSensorReport function to make it compatible with latest Arduino IDE (1.0)
+
+// NEW IN VERSION 1.3:
+// Now it works on GNU/Linux. Also tested with MacOS and Windows 7.
+// timer2 set to 20ms, fixing a glitch that made this period unstable in previous versions.
+// readSerialport() function optimized.
+// pulse() modified so that it receives pulse width as a parameter instead using a global variable.
+// updateServoMotors changes its name as a global variable had the same name.
+// Some minor fixes.
+
+typedef enum { 
+  input, servomotor, pwm, digital } 
+pinType;
+
+typedef struct pin {
+  pinType type;       //Type of pin
+  int state;         //State of an output
+  //byte value;       //Value of an input. Not used by now. TODO
+};
+
+pin arduinoPins[14];  //Array of struct holding 0-13 pins information
+
+unsigned long lastDataReceivedTime = millis();
+
+void setup()
+{
+  Serial.begin(38400);
+  Serial.flush();
+  configurePins();
+  resetPins();
+}
+
+void loop()
+{
+  static unsigned long timerCheckUpdate = millis();
+
+  if (millis()-timerCheckUpdate>=20)
+  {
+    sendUpdateServomotors();
+    sendSensorValues();
+    timerCheckUpdate=millis();
+  }
+
+  readSerialPort();
+}
+
+void configurePins()
+{
+  arduinoPins[0].type=input;
+  arduinoPins[1].type=input;
+  arduinoPins[2].type=input;
+  arduinoPins[3].type=input;
+  arduinoPins[4].type=servomotor;
+  arduinoPins[5].type=pwm;
+  arduinoPins[6].type=pwm;
+  arduinoPins[7].type=servomotor;
+  arduinoPins[8].type=servomotor;
+  arduinoPins[9].type=pwm;
+  arduinoPins[10].type=digital;
+  arduinoPins[11].type=digital;
+  arduinoPins[12].type=digital;
+  arduinoPins[13].type=digital;
+}
+
+void resetPins() {
+  for (byte index=0; index <=13; index++) 
+  {
+    if (arduinoPins[index].type!=input)
+    {
+      pinMode(index, OUTPUT);
+      if (arduinoPins[index].type==servomotor)
+      {
+        arduinoPins[index].state = 255;
+        servo (index, 255);
+      }
+      else
+      {
+        arduinoPins[index].state=0;
+        digitalWrite(index,LOW);
+      }
+    }
+  }
+}
+
+void sendSensorValues()
+{
+  unsigned int sensorValues[6], readings[5];
+  byte sensorIndex;
+
+  for (sensorIndex = 0; sensorIndex < 6; sensorIndex++) //for analog sensors, calculate the median of 5 sensor readings in order to avoid variability and power surges
+  {
+    for (byte p = 0; p < 5; p++)
+      readings[p] = analogRead(sensorIndex);
+    insertionSort(readings, 5); //sort readings
+    sensorValues[sensorIndex] = readings[2]; //select median reading
+  }
+
+  //send analog sensor values
+  for (sensorIndex = 0; sensorIndex < 6; sensorIndex++)
+    ScratchBoardSensorReport(sensorIndex, sensorValues[sensorIndex]);
+
+  //send digital sensor values
+  ScratchBoardSensorReport(6, digitalRead(2)?1023:0);
+  ScratchBoardSensorReport(7, digitalRead(3)?1023:0);
+}
+
+void insertionSort(unsigned int* array, unsigned int n)
+{
+  for (int i = 1; i < n; i++)
+    for (int j = i; (j > 0) && ( array[j] < array[j-1] ); j--)
+      swap( array, j, j-1 );
+}
+
+void swap(unsigned int* array, unsigned int a, unsigned int b)
+{
+  unsigned int temp = array[a];
+  array[a] = array[b];
+  array[b] = temp;
+}
+
+void ScratchBoardSensorReport(byte sensor, int value) //PicoBoard protocol, 2 bytes per sensor
+{
+  Serial.write( B10000000
+    | ((sensor & B1111)<<3)
+    | ((value>>7) & B111));
+  Serial.write( value & B1111111);
+}
+
+void readSerialPort()
+{
+  byte pin;
+  int newVal;
+  static byte actuatorHighByte, actuatorLowByte;
+  static byte readingSM = 0;
+
+  if (Serial.available())
+  {
+    if (readingSM == 0)
+    {
+      actuatorHighByte = Serial.read();
+      if (actuatorHighByte >= 128) readingSM = 1;
+    }
+    else if (readingSM == 1)
+    {
+      actuatorLowByte = Serial.read();
+      if (actuatorLowByte < 128) readingSM = 2;
+      else readingSM = 0;
+    }
+
+    if (readingSM == 2)
+    {
+      lastDataReceivedTime = millis();    
+      pin = ((actuatorHighByte >> 3) & 0x0F);
+      newVal = ((actuatorHighByte & 0x07) << 7) | (actuatorLowByte & 0x7F); 
+
+      if(arduinoPins[pin].state != newVal)
+      {
+        arduinoPins[pin].state = newVal;
+        updateActuator(pin);
+      }
+      readingSM = 0;
+    }
+  }
+  else checkScratchDisconnection();
+}
+
+void reset() //with xbee module, we need to simulate the setup execution that occurs when a usb connection is opened or closed without this module
+{
+  resetPins();        // reset pins
+  sendSensorValues(); // protocol handshaking
+  lastDataReceivedTime = millis();
+}
+
+void updateActuator(byte pinNumber)
+{
+  if (arduinoPins[pinNumber].type==digital) digitalWrite(pinNumber, arduinoPins[pinNumber].state);
+  else if (arduinoPins[pinNumber].type==pwm) analogWrite(pinNumber, arduinoPins[pinNumber].state);
+}
+
+void sendUpdateServomotors()
+{
+  for (byte p = 0; p < 10; p++)
+    if (arduinoPins[p].type == servomotor) servo(p, arduinoPins[p].state);
+}
+
+void servo (byte pinNumber, byte angle)
+{
+  if (angle != 255)
+    pulse(pinNumber, (angle * 10) + 600);
+}
+
+void pulse (byte pinNumber, unsigned int pulseWidth)
+{
+  digitalWrite(pinNumber, HIGH);
+  delayMicroseconds(pulseWidth);
+  digitalWrite(pinNumber, LOW);
+}
+
+void checkScratchDisconnection() //the reset is necessary when using an wireless arduino board (because we need to ensure that arduino isn't waiting the actuators state from Scratch) or when scratch isn't sending information (because is how serial port close is detected)
+{
+  if (millis() - lastDataReceivedTime > 1000) reset(); //reset state if actuators reception timeout = one second
+}
+
+
+
diff --git a/esercizi/analog_input_6.sb b/esercizi/analog_input_6.sb
new file mode 100644 (file)
index 0000000..ad878d7
Binary files /dev/null and b/esercizi/analog_input_6.sb differ
diff --git a/esercizi/analog_input_output_7.sb b/esercizi/analog_input_output_7.sb
new file mode 100644 (file)
index 0000000..2ea72ac
Binary files /dev/null and b/esercizi/analog_input_output_7.sb differ
diff --git a/esercizi/blink_1.sb b/esercizi/blink_1.sb
new file mode 100644 (file)
index 0000000..0bbe491
Binary files /dev/null and b/esercizi/blink_1.sb differ
diff --git a/esercizi/blink_led_esterno_2.sb b/esercizi/blink_led_esterno_2.sb
new file mode 100644 (file)
index 0000000..7e88ba0
Binary files /dev/null and b/esercizi/blink_led_esterno_2.sb differ
diff --git a/esercizi/bottone_ciclo_condizionele_4.sb b/esercizi/bottone_ciclo_condizionele_4.sb
new file mode 100644 (file)
index 0000000..8fea0ae
Binary files /dev/null and b/esercizi/bottone_ciclo_condizionele_4.sb differ
diff --git a/esercizi/bottone_effetto.sb b/esercizi/bottone_effetto.sb
new file mode 100644 (file)
index 0000000..1918adf
Binary files /dev/null and b/esercizi/bottone_effetto.sb differ
diff --git a/esercizi/ciclo_iterattivo_5.sb b/esercizi/ciclo_iterattivo_5.sb
new file mode 100644 (file)
index 0000000..a687beb
Binary files /dev/null and b/esercizi/ciclo_iterattivo_5.sb differ
diff --git a/esercizi/input_condizionale_3.sb b/esercizi/input_condizionale_3.sb
new file mode 100644 (file)
index 0000000..a879037
Binary files /dev/null and b/esercizi/input_condizionale_3.sb differ
diff --git a/esercizi/input_condizionale_suono.sb b/esercizi/input_condizionale_suono.sb
new file mode 100644 (file)
index 0000000..abdc275
Binary files /dev/null and b/esercizi/input_condizionale_suono.sb differ
diff --git a/schemi_breadboard/.directory b/schemi_breadboard/.directory
new file mode 100644 (file)
index 0000000..62a9ca0
--- /dev/null
@@ -0,0 +1,4 @@
+[Dolphin]
+PreviewsShown=true
+Timestamp=2015,10,9,14,38,22
+Version=3
diff --git a/schemi_breadboard/blink_led_esterno_2.png b/schemi_breadboard/blink_led_esterno_2.png
new file mode 100644 (file)
index 0000000..590c09d
Binary files /dev/null and b/schemi_breadboard/blink_led_esterno_2.png differ
diff --git a/schemi_breadboard/bottone_ciclo_condizionele_4.png b/schemi_breadboard/bottone_ciclo_condizionele_4.png
new file mode 100644 (file)
index 0000000..02ca967
Binary files /dev/null and b/schemi_breadboard/bottone_ciclo_condizionele_4.png differ
diff --git a/schemi_breadboard/circuito_led_bb.png b/schemi_breadboard/circuito_led_bb.png
new file mode 100644 (file)
index 0000000..7213d7c
Binary files /dev/null and b/schemi_breadboard/circuito_led_bb.png differ
diff --git a/schemi_breadboard/circuito_led_button_bb.png b/schemi_breadboard/circuito_led_button_bb.png
new file mode 100644 (file)
index 0000000..f1915d1
Binary files /dev/null and b/schemi_breadboard/circuito_led_button_bb.png differ
diff --git a/schemi_breadboard/photoresistor_led.png b/schemi_breadboard/photoresistor_led.png
new file mode 100644 (file)
index 0000000..c9e1626
Binary files /dev/null and b/schemi_breadboard/photoresistor_led.png differ
diff --git a/schemi_breadboard/potenziometro.png b/schemi_breadboard/potenziometro.png
new file mode 100644 (file)
index 0000000..2aa5fa6
Binary files /dev/null and b/schemi_breadboard/potenziometro.png differ
diff --git a/screenshots/.directory b/screenshots/.directory
new file mode 100644 (file)
index 0000000..fbffe6a
--- /dev/null
@@ -0,0 +1,4 @@
+[Dolphin]
+PreviewsShown=true
+Timestamp=2015,10,9,14,33,56
+Version=3
diff --git a/screenshots/analog_input_6.png b/screenshots/analog_input_6.png
new file mode 100644 (file)
index 0000000..295b2af
Binary files /dev/null and b/screenshots/analog_input_6.png differ
diff --git a/screenshots/analog_input_output_7.png b/screenshots/analog_input_output_7.png
new file mode 100644 (file)
index 0000000..95e1390
Binary files /dev/null and b/screenshots/analog_input_output_7.png differ
diff --git a/screenshots/blink.png b/screenshots/blink.png
new file mode 100644 (file)
index 0000000..e549761
Binary files /dev/null and b/screenshots/blink.png differ
diff --git a/screenshots/bottone_ciclo_condizionele.png b/screenshots/bottone_ciclo_condizionele.png
new file mode 100644 (file)
index 0000000..723d544
Binary files /dev/null and b/screenshots/bottone_ciclo_condizionele.png differ
diff --git a/screenshots/ciclo_condizionale_suono.png b/screenshots/ciclo_condizionale_suono.png
new file mode 100644 (file)
index 0000000..9a14335
Binary files /dev/null and b/screenshots/ciclo_condizionale_suono.png differ
diff --git a/screenshots/ciclo_iterativo.png b/screenshots/ciclo_iterativo.png
new file mode 100644 (file)
index 0000000..201daf7
Binary files /dev/null and b/screenshots/ciclo_iterativo.png differ
diff --git a/screenshots/ciclo_iterativo_5.png b/screenshots/ciclo_iterativo_5.png
new file mode 100644 (file)
index 0000000..dbd8319
Binary files /dev/null and b/screenshots/ciclo_iterativo_5.png differ
diff --git a/screenshots/input_condizionale_suono.png b/screenshots/input_condizionale_suono.png
new file mode 100644 (file)
index 0000000..f0eeecd
Binary files /dev/null and b/screenshots/input_condizionale_suono.png differ
diff --git a/screenshots/movimento_sprite_potenziometro.png b/screenshots/movimento_sprite_potenziometro.png
new file mode 100644 (file)
index 0000000..72b2f97
Binary files /dev/null and b/screenshots/movimento_sprite_potenziometro.png differ