Outils d'utilisateurs

Outils du Site


projets:arduino_et_processing

Arduino et Processing

Firmware Arduino

Le firmware

#define DIGITAL 0
#define ANALOG 1
#define DIGITALPIN 14
#define ANALOGPIN  6
#define ANALOGDELTA 1
 
char digitalListen[DIGITALPIN];
char digitalPrevious[DIGITALPIN];
char analogListen[ANALOGPIN];
int analogPrevious[ANALOGPIN];
 
char command[8];
char key[8];
//char value[8];
int pos = 0;
char c;
 
int strtoint(char *c) {
  int pos = 0;
  int val = 0;
  while (c[pos] != 0) {
    val = 10*val + c[pos++] - '0';
  }
  return val;
}
 
void initPin() {
  for(int i = 0; i<DIGITALPIN;i++) {
    digitalListen[i] = 0;
  }
  for(int i = 0; i<ANALOGPIN;i++) {
    analogListen[i] = 0;
  }
}
 
void exec(char *command) {
  int pos = 0;
  int posKey = 0;
  int valKey;
//  int posValue = 0;
  int type = 0; 
  int val;
 
  switch (command[pos]) {
    case 'C': //clear command C clear all or C A5
      if (command[1] != '\n') {
        pos+=2;
        if (command[pos] =='A') {
          type = ANALOG;
          pos++;
        } else {
          type = DIGITAL;
        }
        while (command[pos] != '\n') {
          key[posKey++] = command[pos++];
        }
        key[posKey] = 0;
 
        if (type == ANALOG) {
          analogListen[strtoint(key)] = 0;
        } else {
          digitalListen[strtoint(key)] = 0;
        }
      } else {
        initPin();
      }
      break;
    case 'M': //pinMode command ex: M 12,I
      pos+=2;
      while (command[pos] != ',') {
        key[posKey++] = command[pos++];
      }
      key[posKey] = 0;
      pos++;
      if (command[pos] == 'I') {
        pinMode(strtoint(key),INPUT);
      } else {
        pinMode(strtoint(key),OUTPUT);
      }
      break;
 
    case 'W': //write command ex: W 12,0
      pos+=2;
      while (command[pos] != ',') {
        key[posKey++] = command[pos++];
      }
      key[posKey] = 0;
      pos++;
      if (command[pos] == '0') {
        digitalWrite(strtoint(key),LOW);
      } else {
        digitalWrite(strtoint(key),HIGH);
      }
      break;
 
    case 'R': //write command ex: R A0
      pos+=2;
      if (command[pos] =='A') {
        type = ANALOG;
        pos++;
      } else {
        type = DIGITAL;
      }
      while (command[pos] != '\n') {
        key[posKey++] = command[pos++];
      }
      key[posKey] = 0;
 
      if (type == ANALOG) {
       val = analogRead(strtoint(key));
        Serial.print("V A");
        Serial.print(key);
        Serial.print(",");
        Serial.println(val);
      } else {
        val = digitalRead(strtoint(key));
        Serial.print("V ");
        Serial.print(key);
        Serial.print(",");
        Serial.println(val);
      }
      break;
 
    case 'L': //write command ex: L A0
      pos+=2;
      if (command[pos] =='A') {
        type = ANALOG;
        pos++;
      } else {
        type = DIGITAL;
      }
      while (command[pos] != '\n') {
        key[posKey++] = command[pos++];
      }
      key[posKey] = 0;
 
      if (type == ANALOG) {
        valKey = strtoint(key);
        analogListen[valKey] = 1;
        val = analogRead(valKey);
        analogPrevious[valKey] = val;
        Serial.print("V A");
        Serial.print(key);
        Serial.print(",");
        Serial.println(val);
      } else {
        valKey = strtoint(key);
        digitalListen[valKey] = 1;
        val = digitalRead(valKey);
        digitalPrevious[valKey] = val;
        Serial.print("V ");
        Serial.print(key);
        Serial.print(",");
        Serial.println(val);
      }
      break;
  }
}
 
void setup() {
  Serial.begin(57600);
  initPin();
}
 
void loop() {
  int val;
 
  if (Serial.available()){
    c = Serial.read();
    command[pos++] = c;
    if (c =='\n') {
      exec(command);
      pos = 0;
    }
//  }
  } else {
    for(int i = 0; i<DIGITALPIN;i++) {
      if (digitalListen[i]) {
        val = digitalRead(i);
        if (val != digitalPrevious[i]) {
          digitalPrevious[i] = val;
          Serial.print("V ");
          Serial.print(i);
          Serial.print(",");
          Serial.println(val);
        }
      }
    }
    for(int i = 0; i<ANALOGPIN;i++) {
      if (analogListen[i]) {
        val = analogRead(i);
        if (abs(analogPrevious[i]-val) > ANALOGDELTA) {
          analogPrevious[i] = val;
          Serial.print("V A");
          Serial.print(i);
          Serial.print(",");
          Serial.println(val);
 
        }
      }    
    }
  }
}

Exemples d'utilisation

R A0 L A0 C A0 C

M 8,O W 8,1 W 8,0

M 6,I R 6 L 6

+ montage

Applications avec Processing

Lire un potentiometre en A0

import processing.serial.*;
 
Serial port;
int potentiometre;
 
void setup() {
  size(800,600);
  fill(0);
  String portName = Serial.list()[0];
  port = new Serial(this,portName,57600);
}
 
void draw() {
 port.write("R A0\n");
    while (port.available() > 0) {
      int c = port.read();
      print((char)c);
    }
  delay(500);
}
import processing.serial.*;
 
Serial port;
int potentiometre;
 
void setup() {
  size(800,600);
  fill(0);
  String portName = Serial.list()[0];
  port = new Serial(this,portName,57600);
  delay(2000);
  port.write("M 8,O\n"); 
}
 
void draw() {
 
 delay(100);
 port.write("W 8,1\n");
 delay(100);
 port.write("W 8,0\n");
 delay(100);
}

Led et souris

import processing.serial.*;
 
Serial port;
int potentiometre;
 
void setup() {
  size(800,600);
  fill(0);
  String portName = Serial.list()[0];
  port = new Serial(this,portName,57600);
  delay(2000);
  port.write("M 8,O\n"); 
}
 
void draw() {
 
  if (mousePressed) {
    port.write("W 8,1\n");
  } else {
    port.write("W 8,0\n");
  }
}

Background

import processing.serial.*;
 
Serial port;
int potentiometre;
String command = "";
int value = 0;
 
void setup() {
  size(800,600);
  String portName = Serial.list()[0];
  port = new Serial(this,portName,57600);
  delay(2000);
}
 
void draw() {
  background(0,value,value);
 port.write("R A0\n");
  while (port.available() > 0) {
    int c = port.read();
    command = command + (char)c;
    if (c == '\n') {
      value = Integer.valueOf(command.split(",")[1].trim());
      value = value >> 2;
      println(value);
      command = "";
    }
  }
}

Line

import processing.serial.*;
 
Serial port;
int potentiometre;
String command = "";
int value;
 
void setup() {
  size(800,600);
  fill(0);
  String portName = Serial.list()[0];
  port = new Serial(this,portName,57600);
  delay(2000);
  port.write("L A0\n"); 
  strokeWeight(2);
  stroke(204, 102, 0);
}
 
void draw() {
  background(255);
  while (port.available() > 0) {
    int c = port.read();
    command = command + (char)c;
    if (c == '\n') {
      value = Integer.valueOf(command.split(",")[1].trim());
      line(100,value+50,700,value+50);
      command = "";
    }
  }
}

Et pour la prochaine fois :

une ardoise magique avec deux potentiomètres et un bouton poussoir

projets/arduino_et_processing.txt · Dernière modification: 2017/09/16 15:41 par thierry_dasse