본문 바로가기

만들기 / making/sensing workshop

multi sensor arduino / processing

multi sensor arduino / processing

Needs to be added or corrected!!
숙제로 밑에 링크를 보고 코드를 변환해보세요
어떻게 하면 여러 센서 데이터를 받아 각 센서 값에 따라 영상을 변화할 수 있을까요?
//Arduino code 
//ADC + Serial Communication

int reading0,reading1,reading2,reading3,reading4,reading5;                // the readings from the analog input


void setup()
{
  Serial.begin(9600);                     // initialize serial communication with computer
 
}

void loop()
{
  
  reading0 = analogRead(0); // read from the sensor
  reading1 = analogRead(1); // read from the sensor
  reading2 = analogRead(2); // read from the sensor
  reading3 = analogRead(3); // read from the sensor
  reading4 = analogRead(4); // read from the sensor
  reading5 = analogRead(5); // read from the sensor
  
  if ( Serial.available() > 0 )
  {
    if (Serial.read() == 'a')
    {
      
    
    Serial.println(reading0);    // send it to the computer (as ASCII digits)
    Serial.println(reading1);    // send it to the computer (as ASCII digits)
    Serial.println(reading2);    // send it to the computer (as ASCII digits)
    Serial.println(reading3);    // send it to the computer (as ASCII digits)
    Serial.println(reading4);    // send it to the computer (as ASCII digits)
    Serial.println(reading5);    // send it to the computer (as ASCII digits)
    }
  }
}




//Corrected Final multi arduino
//Arduino code 
//ADC + Serial Communication

int reading0,reading1,reading2,reading3,reading4,reading5;                // the readings from the analog input


void setup()
{
  Serial.begin(9600);                     // initialize serial communication with computer
 
}

void loop()
{
  
  reading0 = analogRead(0); // read from the sensor
  reading1 = analogRead(1); // read from the sensor
  reading2 = analogRead(2); // read from the sensor
  reading3 = analogRead(3); // read from the sensor
  reading4 = analogRead(4); // read from the sensor
  reading5 = analogRead(5); // read from the sensor
  
  if ( Serial.available() > 0 )
  {
    if (Serial.read() == 'a')
    {
      
    
    Serial.println(reading0);    // send it to the computer (as ASCII digits)
    Serial.println(reading1);    // send it to the computer (as ASCII digits)
    Serial.println(reading2);    // send it to the computer (as ASCII digits)
    Serial.println(reading3);    // send it to the computer (as ASCII digits)
    Serial.println(reading4);    // send it to the computer (as ASCII digits)
    Serial.println(reading5);    // send it to the computer (as ASCII digits)
    }
  }
}
import processing.opengl.*;

import processing.serial.*;

int lf = 10;    // Linefeed in ASCII
//String myString = null;
Serial myPort;  // The serial port
int i;
String myString;

void setup() {
  
  // List all the available serial ports
  println(Serial.list());
  size (800, 450, OPENGL);
  // I know that the first port in the serial list on my mac
  // is always my  Keyspan adaptor, so I open Serial.list()[0].
  // Open whatever port is the one you're using.
  myPort = new Serial(this, Serial.list()[0], 9600);
  myPort.clear();
  // Throw out the first reading, in case we started reading 
  // in the middle of a string from the sender.
  //myString = myPort.readStringUntil(lf);
  //myString = null;
  myPort.bufferUntil(lf);
  frameRate(10);
}

void draw() {
  myPort.write('a');
  background (255);

  fill(255,0,0);
  ellipse (100, height/2, 100, i);
  ellipse (220, height/2, 100, i);
  ellipse (340, height/2, 100, i);
  ellipse (460, height/2, 100, i);
  ellipse (580, height/2, 100, i);
  ellipse (700, height/2, 100, i);
  
}

void serialEvent(Serial p) { 
  myString = myPort.readString();
  myString = trim(myString);
  i = int(myString);
  println(i);
}





//Corrected Final multi processing
import processing.opengl.*;

import processing.serial.*;

int lf = 10;    // Linefeed in ASCII
//String myString = null;
Serial myPort;  // The serial port
int i;
String myString;
int[] value = new int[6];
int cnt = 0;

void setup() {
  
  int [] value = {0,0,0,0,0,0};
  cnt=0;
  
  // List all the available serial ports
  println(Serial.list());
  size (800, 450, OPENGL);
  
  myPort = new Serial(this, Serial.list()[0], 9600);
  myPort.clear();
  
  
  myPort.bufferUntil(lf);
  frameRate(30);
}

void draw() {
  myPort.write('a');
  background (255);

  fill(255,0,0);
  ellipse (100, height/2, 100, value[0]);
  ellipse (220, height/2, 100, value[1]);
  ellipse (340, height/2, 100, value[2]);
  ellipse (460, height/2, 100, value[3]);
  ellipse (580, height/2, 100, value[4]);
  ellipse (700, height/2, 100, value[5]);
  
}

void serialEvent(Serial p) { 
  myString = myPort.readString();
  myString = trim(myString);
  value[cnt] = int(myString);
  //println(value[cnt-1]);
  
  
  if (cnt < 5)
  {
      cnt=cnt+1;
  }
  else 
  {
    cnt = 0;
  }
  
}

'만들기 / making > sensing workshop' 카테고리의 다른 글

Day 8: Touch Sensor  (0) 2009.08.31
serial communication  (0) 2009.08.31
arduino + processing code  (0) 2009.08.31
Audio sensor module on PCB  (0) 2009.08.31
Audio sensor module on Breadboard  (0) 2009.08.31