ScratchRobot

Da raspibo.
Versione del 25 lug 2013 alle 16:43 di Eineki (discussione | contributi) (Script per la comunicazione tra scratch e arduino via python)
(diff) ← Versione meno recente | Versione attuale (diff) | Versione più recente → (diff)
Jump to navigation Jump to search

Due righe e qualche pezzo di codice da riorganizzare:

La parte arduino sta semplicemente in attesa di un comando sulla seriale e, in caso lo riconosca esegue le istruzioni associate.

/*
  Python interface via usb serial
  Language: Wiring/Arduino

  Il programma si mette in attesa di un input sulla seriale
  ed agisce a seconda dell'input ricevuto.

  This example code is in the public domain.
 */

int led = 13;    // first analog sensor
int inByte = 0;         // incoming serial byte

void setup()
{
  // initialize the digital pin as an output.
  pinMode(led, OUTPUT);
  // start serial port at 9600 bps:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }

//  establishContact();  // send a byte to establish contact until receiver responds
}

void loop()
{
  // if we get a valid byte, read analog ins:
  if (Serial.available() > 0) {
    // get incoming byte:
    inByte = Serial.read();
    if ((char) inByte=='n') { // oN
        digitalWrite(led, HIGH);   // turn the LED on (HIGH is the voltage level)
    } else if ((char) inByte=='f') { // ofF
        digitalWrite(led, LOW);   // turn the LED off
    } else { //      default:
        Serial.print(inByte); // send back the unrecognized command to the sender
    }
  }
}

La controparte python invece si attacca ad una istanza scratch e fa da ponte/interprete dei comandi verso arduino. 'Nota: ' Se arduino è collegato via usb alla macchina host quando Scratch va in modalità server scratch sembra inviare direttamente i comandi anche a lui (penso che in realta cerchi un handshake senza successo) e python non riesce più ad interfacciardi ad arduino.