Go Down

Topic: FIRMATA : Communication between Arduino & Processing (Read 285 times) previous topic - next topic

bvking

Hi everyones,

I would like just to know the way to send the PWM value (not only HIGH or LOW) from Arduino to Processing?

For now I can use analog input  Arduino to control Processing with FIRMATA library.

With this way, I can manage the interface in Processing where I can see ellipse move with data of analog input (programmed as analogRead) but I can't manage to control an other ellipse with the PWM value (programmed as analogWrite).

How can I say to the program, that analogWrite value can be like an analogRead value?

Thanks in advance.

I put //************************************* to see where I added code

First the Arduino Program:

Code: [Select]


/*
 * Firmata is a generic protocol for communicating with microcontrollers
 * from software on a host computer. It is intended to work with
 * any host computer software package.
 *
 * To download a host software package, please click on the following link
 * to open the download page in your default browser.
 *
 * http://firmata.org/wiki/Download
 */

/* Supports as many analog inputs and analog PWM outputs as possible.
 *
 * This example code is in the public domain.
 */
#include <Firmata.h>

//*************************************
int a=0;
int sensorPin = A0;    // select the input pin for the potentiometer
int ledPin = 9;      // pin 9 with PWM
int sensorValue = 0;  // variable to store the value coming from the sensor
int ledPin2 = 11; //  pin 11 with PWM
//****************************************

byte analogPin = 0;

void analogWriteCallback(byte pin, int value)
{
  if (IS_PIN_PWM(pin)) {
    pinMode(PIN_TO_DIGITAL(pin), OUTPUT);
    analogWrite(PIN_TO_PWM(pin), value);
 
  }
}

void setup()
{
  Firmata.setFirmwareVersion(FIRMATA_FIRMWARE_MAJOR_VERSION, FIRMATA_FIRMWARE_MINOR_VERSION);
  Firmata.attach(ANALOG_MESSAGE, analogWriteCallback);
  Firmata.begin(57600);
 
  pinMode(ledPin, OUTPUT);
  pinMode (ledPin2, OUTPUT);
}

void loop()
{
  while (Firmata.available()) {
    Firmata.processInput();
  }
  // do one analogRead per loop, so if PC is sending a lot of
  // analog write messages, we will only delay 1 analogRead
  Firmata.sendAnalog(analogPin, analogRead(analogPin));
  analogPin = analogPin + 1;
  if (analogPin >= TOTAL_ANALOG_PINS) analogPin = 0;

//*************************************
   a=a+1;
  float b = a/100;
   int sensorValue = analogRead(A0);
  // Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 5V):
  float voltage = sensorValue * (5/ 1023.0);
  // print out the value you read:
  //int phi= sensorValue *

  int phi= map (voltage, 0, 5, 0, 180);
   
  analogWrite (ledPin, abs (100*cos (2*PI*b/10)));
  analogWrite (ledPin2, abs (100*cos (2*PI*b/10- phi)));

  Serial.println (b);
  Serial.println (phi);
  Serial.print ("fonction cos "); Serial.println(abs (100*cos (2*PI*b/100)));
  Serial.print ("fonction lag "); Serial.println(abs (100*cos (2*PI*b/100- phi)));
//****************************************
}


Second: Processing program

Code: [Select]

/*
arduino_input

Demonstrates the reading of digital and analog pins of an Arduino board
running the StandardFirmata firmware.

To use:
* Using the Arduino software, upload the StandardFirmata example (located
  in Examples > Firmata > StandardFirmata) to your Arduino board.
* Run this sketch and look at the list of serial ports printed in the
  message area below. Note the index of the port corresponding to your
  Arduino board (the numbering starts at 0).  (Unless your Arduino board
  happens to be at index 0 in the list, the sketch probably won't work.
  Stop it and proceed with the instructions.)
* Modify the "arduino = new Arduino(...)" line below, changing the number
  in Arduino.list()[0] to the number corresponding to the serial port of
  your Arduino board.  Alternatively, you can replace Arduino.list()[0]
  with the name of the serial port, in double quotes, e.g. "COM5" on Windows
  or "/dev/tty.usbmodem621" on Mac.
* Run this sketch. The squares show the values of the digital inputs (HIGH
  pins are filled, LOW pins are not). The circles show the values of the
  analog inputs (the bigger the circle, the higher the reading on the
  corresponding analog input pin). The pins are laid out as if the Arduino
  were held with the logo upright (i.e. pin 13 is at the upper left). Note
  that the readings from unconnected pins will fluctuate randomly.
 
For more information, see: http://playground.arduino.cc/Interfacing/Processing
*/

import processing.serial.*;

import cc.arduino.*;

Arduino arduino;

color off = color(4, 79, 111);
color on = color(84, 145, 158);

void setup() {
  size(470, 280);

  // Prints out the available serial ports.
  println(Arduino.list());
 
  // Modify this line, by changing the "0" to the index of the serial
  // port corresponding to your Arduino board (as it appears in the list
  // printed by the line above).
  //arduino = new Arduino(this, Arduino.list()[0], 57600);
 
  // Alternatively, use the name of the serial port corresponding to your
  // Arduino (in double-quotes), as in the following line.
  //arduino = new Arduino(this, "dev/tty.usbmodem1421", 57600);
 
  arduino = new Arduino(this, Serial.list()[4], 57600);
  ///dev/tty.usbmodem1421, 57600
 
  // Set the Arduino digital pins as inputs.
  for (int i = 0; i <= 13; i++)
    arduino.pinMode(i, Arduino.INPUT);
}

void draw() {
  background(off);
  stroke(on);
 
  // Draw a filled box for each digital pin that's HIGH (5 volts).
  for (int i = 0; i <= 13; i++) {
    if (arduino.digitalRead(i) == Arduino.HIGH)
      fill(on);
    else
      fill(off);
     
    rect(420 - i * 30, 30, 20, 20);
  }

  // Draw a circle whose size corresponds to the value of an analog input.
  noFill();
//********
  for (int i = 0; i <= 13; i++) { //****before i <= 5, because the 5 analog input
    ellipse( i * 30, 240, arduino.analogRead(i) / 16, arduino.analogRead(i) / 16);
  }
}




Go Up