Reading from Arduino In specified sketch

edited March 2014 in Arduino

Hi,

I've just begin to work with connections between Processing to arduino.

This to read the arduino of his on/off's that in the code (see below) and transform it in a basic visual. I've followed a tutorial to connect them to get an idea of how it has to go. I've get that somewhat. The only thing is that my tutorials was about floating point that processing reads. I'm still not sure actually what that means. But I think I don't need that for what I want to do.

Anyway hopefully somebody can help me.

This is the code from arduino. (What I want to be read)

  //const int switchPin = 2;    // switch input
  const int motor1Pin = 3;    // H-bridge leg 1 (pin 2, 1A)
  const int motor2Pin = 4;    // H-bridge leg 2 (pin 7, 2A)
  const int enablePin = 9;    // H-bridge enable pin


 void setup() {
    // set the switch as an input:
    //pinMode(switchPin, INPUT); 
    Serial.begin(9600);
    // set all the other pins you're using as outputs:
    pinMode(motor1Pin, OUTPUT); 
    pinMode(motor2Pin, OUTPUT); 
    pinMode(enablePin, OUTPUT);

    // set enablePin high so that motor can turn on
    digitalWrite(enablePin,   LOW); 
  }

void loop(){

      // go 
      digitalWrite(enablePin, HIGH);
      digitalWrite(motor1Pin, HIGH);  // set leg 1 of the H-bridge high
      digitalWrite(motor2Pin, LOW);   // set leg 2 of the H-bridge low
      delay(2000);
      Serial.println("1");
      // break1
      digitalWrite(enablePin, LOW); 
      digitalWrite(motor1Pin, LOW);   // set leg 1 of the H-bridge low
      digitalWrite(motor2Pin, LOW);  // set leg 2 of the H-bridge high
      Serial.println("2");
      delay(300);

      digitalWrite(enablePin, HIGH); 
      digitalWrite(motor1Pin, LOW);   // set leg 1 of the H-bridge low
      digitalWrite(motor2Pin, HIGH);  // set leg 2 of the H-bridge high
      Serial.println("3");
      delay(300);

      digitalWrite(enablePin, LOW); 
      digitalWrite(motor1Pin, LOW);   // set leg 1 of the H-bridge low
      digitalWrite(motor2Pin, LOW);  // set leg 2 of the H-bridge high
      delay(3000);

      digitalWrite(enablePin, HIGH); 
      digitalWrite(motor1Pin, LOW);   // set leg 1 of the H-bridge low
      digitalWrite(motor2Pin, HIGH);  // set leg 2 of the H-bridge high
      delay(300);

      digitalWrite(enablePin, LOW); 
      digitalWrite(motor1Pin, LOW);   // set leg 1 of the H-bridge low
      digitalWrite(motor2Pin, LOW);  // set leg 2 of the H-bridge high
      delay(2000);

      digitalWrite(enablePin, LOW); 
      digitalWrite(motor1Pin, LOW);   // set leg 1 of the H-bridge low
      digitalWrite(motor2Pin, LOW);  // set leg 2 of the H-bridge high
      delay(300);
      // back 
      digitalWrite(enablePin, HIGH);
      digitalWrite(motor1Pin, LOW);  // set leg 1 of the H-bridge high
      digitalWrite(motor2Pin, HIGH);   // set leg 2 of the H-bridge low
      delay(2000);
      // break2
      digitalWrite(enablePin, LOW); 
      digitalWrite(motor1Pin, LOW);   // set leg 1 of the H-bridge low
      digitalWrite(motor2Pin, LOW);  // set leg 2 of the H-bridge high
      delay(10000);

    }

This is the arduino code for the serial to Processing. (the floating point)

void setup(){
  Serial.begin(9600);
}

void loop(){
  float a = -4.56789;
  a /= random(10);
  Serial.println(a, 5);
  delay(500);
}

This the processing sketch to read it.

import processing.serial.*;
Serial myPort;
float value;

void setup()
{
  myPort = new Serial(this, Serial.list()[0], 9600);
}

void draw()
{
} 

void serialEvent(Serial p) {
  // get message till line break (ASCII > 13)
  String message = myPort.readStringUntil(13);
  if(message != null){
    value = float(message);
    println(value);
  }
}

How can I read the values of the first .ino sketch.

Hope someone can help me.

kind regards

Sign In or Register to comment.