Bonjour à tous,
Je n'arrive pas à transmettre depuis Arduino ou recevoir dans Processing d'informations PWM.
Pourtant, j'envoie des données avec analogWrite(,) à partir d'Arduino et les retrouver dans Processing avec arduino.analogRead().
J'ai testé l'exemple ArduinoInput dans Processing et j' obtiens parfaitement les informations de l'entrée analogique Arduino et des sorties numériques Arduino. Je me suis le basé sur le skecth standard Firmata (auquel j'ai rejouter la lecture de l'entrée analogique A0 et controler des sorties digitales et PWM).
Par contre, je n'arrive pas à obtenir les informations PWM(en l'occurrence de la sortie Arduino 9). Je recois seulement des 0.
Je mets seulement la fin du programme Arduino car j'ai copier-coller le programme Standard Firmata (l'exemple de la bibliothèque), mais c'est seulement la fin, après les //********* où j'ai dû faire une erreur!
/*
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.
/*
//*************************************
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
//****************************************
void setup()
{
pinMode (3, OUTPUT);
pinMode (2, OUTPUT);
pinMode (9, OUTPUT);
pinMode (12, OUTPUT);
pinMode (11, OUTPUT);
pinMode(ledPin, OUTPUT);
pinMode (ledPin2, OUTPUT);
pinMode (16, OUTPUT);
pinMode (15, OUTPUT);
pinMode (13, OUTPUT);
pinMode (14, OUTPUT);
Firmata.setFirmwareVersion(FIRMATA_FIRMWARE_MAJOR_VERSION, FIRMATA_FIRMWARE_MINOR_VERSION);
Firmata.attach(ANALOG_MESSAGE, analogWriteCallback);
Firmata.attach(DIGITAL_MESSAGE, digitalWriteCallback);
Firmata.attach(REPORT_ANALOG, reportAnalogCallback);
Firmata.attach(REPORT_DIGITAL, reportDigitalCallback);
Firmata.attach(SET_PIN_MODE, setPinModeCallback);
Firmata.attach(SET_DIGITAL_PIN_VALUE, setPinValueCallback);
Firmata.attach(START_SYSEX, sysexCallback);
Firmata.attach(SYSTEM_RESET, systemResetCallback);
// to use a port other than Serial, such as Serial1 on an Arduino Leonardo or Mega,
// Call begin(baud) on the alternate serial port and pass it to Firmata to begin like this:
// Serial1.begin(57600);
// Firmata.begin(Serial1);
// However do not do this if you are using SERIAL_MESSAGE
Firmata.begin(57600);
while (!Serial) {
; // wait for serial port to connect. Needed for ATmega32u4-based boards and Arduino 101
}
systemResetCallback(); // reset to default config
}
byte analogPin = 0;
/*==============================================================================
* LOOP()
*============================================================================*/
void loop()
{
byte pin, analogPin;
/* DIGITALREAD - as fast as possible, check for changes and output them to the
* FTDI buffer using Serial.print() */
checkDigitalInputs();
/* STREAMREAD - processing incoming messagse as soon as possible, while still
* checking digital inputs. */
while (Firmata.available())
Firmata.processInput();
// TODO - ensure that Stream buffer doesn't go over 60 bytes
currentMillis = millis();
if (currentMillis - previousMillis > samplingInterval) {
previousMillis += samplingInterval;
/* ANALOGREAD - do all analogReads() at the configured sampling interval */
for (pin = 0; pin < TOTAL_PINS; pin++) {
if (IS_PIN_ANALOG(pin) && Firmata.getPinMode(pin) == PIN_MODE_ANALOG) {
analogPin = PIN_TO_ANALOG(pin);
if (analogInputsToReport & (1 << analogPin)) {
Firmata.sendAnalog(analogPin, analogRead(analogPin));
}
}
}
// report i2c data for all device with read continuous mode enabled
if (queryIndex > -1) {
for (byte i = 0; i < queryIndex + 1; i++) {
readAndReportData(query[i].addr, query[i].reg, query[i].bytes, query[i].stopTX);
}
}
}
#ifdef FIRMATA_SERIAL_FEATURE
serialFeature.update();
#endif
//**********************************************************************************
//**********************************************************************************
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);
digitalWrite(LED_BUILTIN, HIGH); // turn the LED on (HIGH is the voltage level)
digitalWrite(2, HIGH);
digitalWrite (3, HIGH);
digitalWrite (8, HIGH);
int COS = abs (100*cos (2*PI*b/10));
analogWrite (ledPin, COS);
analogWrite (ledPin2,abs (COS-phi));
Serial.println (b);
Serial.println (phi);
Serial.print ("fonction cos "); Serial.println (COS);
Serial.print ("fonction lag "); Serial.println (COS-phi);
}
Le programme Processing où je me suis peut-être tromper de manière pour lire les données de la sortie 9 PWM
/*
arduino_input
*/
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()[4], 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.usbmodem621", 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 <= 5; i++) {
ellipse(280 + i * 30, 240, arduino.analogRead(i) / 16, arduino.analogRead(i) / 16);
ellipse(50, 240, arduino.analogRead(9) / 16, arduino.analogRead(9) / 16);
println(arduino.analogRead(9));
}
}
Merci pour vos éclairages!