Processing and Analog Reading

Hi,

So I have been using processing for a while and I managed to come up with a code that allows you to set any pin on high when you press a certain "key" that its assigned to. The problem is I cannot get any code that uses the analog pins (variable values) to work with processing and arduino.

First- I am running firmata on arduino which allows me to communicate with processing.

Second- I have the following code used in processing:

void keyPressed() {
switch(key) {
case 'e':


}
};
This little portion of the code simply states when key 'e' is pressed, execute ***** code. I usually use arduino.digitalWrite(pin, Arduino.HIGH) as my code [this is what I want to change]

simple enough right.

Now I have a dimmer switched hooked up to the arduino using Analog 1 pin. Now what I want to happen is when I press 'e', I want it to read the value coming from my dimmer and then execute that value read to pin 12.

I have tested the hook up of the dimmer with the arduino sample code given for dimming a LED (just changing the pin values it was assigned too to fit my values) and it worked just fine. So I know my hookup is correct and should work.

Now for my code (or my **** in the above part) is the following:
int pinout= 12;
int sensorValue;
potPin= 1;

case 'e':
sensorValue= arduino.analogRead(potPin);
arduino.analogWrite(pinout, sensorValue);

when I run the code, I get nothing. If you see an error in my code or know a different way to doing this (maybe modifyineeg firmata) please let me know! Even if I need to type in the values to assign to it ( 0 - 255) instead of using my dimmer, I am fine with that. I just want to be able to adjust my values. thanks!

I also tried:
arduino.digitalWrite(pinout, sensorValue);
No luck.

I want it to read the value coming from my dimmer and then execute that value read to pin 12.

The first part should be easy. The second part doesn't make a lot of sense. Suppose you get a value of 890. How do you propose to "execute that value read to pin 12"?

If you are not using a Mega, then pin 12 is not a PWM pin, so analogWrite() won't do what you expect it to do to that pin.

The analogRead() and analogWrite() functions do not use the same range of values, either.

ok so if I get a value of 890, I will just divide it by 4 to stay in the range of 0-255. I am also using a mega.

i know that one ranges from 0-255 and the other is 0-1020.

So my code should look like this:

case 'e':
sensorValue= arduino.analogRead(potPin)/4;
arduino.analogWrite(pinout, sensorValue);

tried it and didn't work. arduinoRead output is 0-1023 and arduinoWrite only takes values 0-255. my code seems fine but I just can't get anything to work for it.

I am also using a mega.

Well, we all know how well Firmata works with the Mega. Which is to say not at all well.

First- I am running firmata on arduino which allows me to communicate with processing.

You don't have to use Firmata to communicate with processing you know. It is a very limiting option as it allows you little flexability at the arduino end.
As Paul says, Firmata is pants with a Mega.

oh I did not know that. All I'm trying to do is communicate wirelessly to my arduino by turning on or off pins. If I don't need firmata, then what exactly should I be programming on my arduino?

I know it has something to do with serial.begin (9600) but I honestly don't know for sure. I am also using xbee for my communication.

I know it has something to do with serial.begin (9600) but I honestly don't know for sure. I am also using xbee for my communication.

You need to define a protocol. A protocol is an agreed upon specification for how two applications will exchange data.

Start by listing all the things that Processing will want to ask the Arduino to do. Like, read digital pin n, write digital pin n, read analog pin n, write analog pin n (PWM actually).

Then, decide on a one letter code for the command. You might send "<R,8>" to read digital pin 8, "<S,6,1>" to set pin 6 to HIGH, and "<A,4>" to read analog pin 4.

Then, make the Processing code send the appropriate command, and make the Arduino read and parse the commands sent, and perform the required action, returning a value, is appropriate.

So the protocol is the agreed upon serial.

The part I would like more information on is the actual sending part and what should be performing all these actions (processing or arduino). I don't think I fully explained myself to you Paul. Like I said before I currently have firmata programmed on the arduino. Then the code I listed previously, works (I listed the main parts that really matters, I left out details such as void setup, void draw etc...).

Now the code that I have on processing is a bit long, but works (for the most part, I am having communication problems at times). Processing reads the key I press, then sends the information of what to do to the arduino (turn pin 8 High). My question is, should processing be only sending the 'key' I pressed and having the arduino interpret the 'key' and perform the action or have processing telling the arduino what to do? would this help with my communication problems that I am having?

Then coming back to the analog/ arduino firmata, I am still unsure of primarily what to write for the arduino. I am not a coding expert at all or how to get started. Obviously using firmata is a patch job, but thats not what I should be using for what I want to be doing. What I am saying is I don't know how to write code which tells the arduino what it receives or sends out. The only thing I know how to do is use processing to send to the arduino to perform an action

So the protocol is the agreed upon serial.

Serial is not a protocol, it is an interface technique.
A protocol is the meaning of the bytes you exchange with the serial mechanism.

There are lots and lots of examples. Of arduino code that sends and receives stuff throught the serial port. To send just do a Serial.print or Serial.write, to recieve check that data is Avaliable then if it is read it, then act on it.

Firmata implements one protocol. It hides that implementation from you, in the form of methods that have similar names to what the Arduino uses - analogWrite(), analogRead(), pinMode(), digitalRead(), and digitalWrite().

When you call one of those methods in Processing, the method sends serial data to the Arduino. The Firmata sketch on the Arduino reads the serial data, collects it into an array, and, when the packet is complete, parses it and performs the required action. If the action generated any data, such as analogRead() does, the Arduino sends the result back to Processing.

The Serial.print(), Serial,read(), and Serial.available() functions in Processing and Arduino bear looking into, as Grumpy points out.

ok! thank you paul and grumpy.

So from what you told me, I need to scratch firmata and start with a new code for arduino using analog write(), read(), serial.print(), .read(), etc..

and to do this I need to write some form a code that says it received what I was sent it to ensure that its performing the action correctly. Does this seem right?

Also lets say if I do make this code and try it out wirelessly using my xbees, but it has glitches such that sometimes it will receive it but sometimes it will not. Is that a code error? or something going on with the xbees? or possibly arduino settings?

Is that a code error? or something going on with the xbees? or possibly arduino settings?

Yes, yes, and no.

Paul and Grumpy,

ok so after some research and following this website:

I came up with the following code:

Arduino

int ledPin;
int ledPin13 = 13;
int ledPin12 = 12;
int ledPin11 = 11;
int sensorPin = A0;
int sensorValue = 0;
int analogPin = 0;
char c = 0;

void setup()
{
    pinMode( ledPin, OUTPUT );
    Serial.begin( 9600 );
}

void loop()
{
    // Wait for a character to arrive at the serial port.
    if( Serial.available() > 0 )
    {
        // Read one byte (character).
        c = Serial.read();
        switch( c )
        {
            case 'e':
                Serial.println("case e received");
                digitalWrite( ledPin13, HIGH );
                Serial.println("Pin 13 HIGH");
                break;
            case 'i':
                Serial.println("case i received");
                digitalWrite( ledPin12, HIGH );
                Serial.println("Pin 12 HIGH");
                break;
            case 'q':
                Serial.println("case q received");
                sensorValue = analogRead( sensorPin )/4;
                analogWrite(ledPin11, sensorValue);
                Serial.println("Pin 11 HIGH");
                break;
            case 'z':
                Serial.println("case q received");
                digitalWrite( ledPin13, LOW );
                Serial.println("Pin 13 LOW");
                break;
            case 'p':
                Serial.println("case p received");
                digitalWrite( ledPin12, LOW );
                Serial.println("Pin 12 LOW");
                break;
            case 'l':
                Serial.println("case l received");
                digitalWrite( ledPin11, LOW );
                Serial.println("Pin 11 LOW");
                break;
        }
    }
      
}

Processing:

import processing.serial.*; //Import the serial library into your sketch
import cc.arduino.*;        //Import the Arduino-Firmata library into your sketch
Arduino arduino;      //Create an instance of Arduino named arduino (can be any name) 

int ledPin;
char serial;

void setup() {
  size(200, 200);
print(arduino.list());  
  arduino = new Arduino(this, Arduino.list()[0], 9600); //defines arduino our board and sets the communication rate
  arduino.pinMode(ledPin, Arduino.OUTPUT);
  }

void keyPressed() {
    print(key);
    delay(10);
    switch (key) {
      case 'e':
      case 'E':
        serial.write( 'e' );
      break;
      case 'i':
      case 'I':
        serial.write( 'i' );
      break;
      case 'q':
      case 'Q':
       serial.write( 'q' );
      break;
      
    }
  };
  


void keyReleased() {
    delay(10);
    switch (key) {
      case 'e':
      case 'E':
        serial.write( 'z' );
      break;
      case 'i':
      case 'I':
        serial.write( 'p');
      break;
      case 'q':
      case 'Q':
        serial.write( 'l');
      break;
    }
  };

Unfortunately, the serial.write is wrong for the processing code. I am unsure of what my error is. Also regarding the arduino code, I don't quite understand the following portion of the code:

c = Serial.read();
        switch( c )

Please let me know if you find the problem out or have a better solution to it!

No need for this

import cc.arduino.*;        //Import the Arduino-Firmata library into your sketch
Arduino arduino;      //Create an instance of Arduino named arduino (can be any name)

Also you have no need to send stuff on key up and key down.

You are also sending debug prints into processing and doing nothing with it.

I don't quite understand what you mean by key up and key down and sending debug prints into processing.

the void keyPressed and keyReleased is used to notify processing that I am pressing or releasing a key. I essentially want to control pins on the arduino by pressing various keys.

ketPress sends stuf out when the key is first held down. keyRelease also sends stuff when you take your finger off the key. You do not need to send stuff to the arduino twice.

You are not setting up the baud rate in processing,

You are not doing anything with data you recieve from the arduino, so how do you know what is happening?

Mike,

For the keyPress/release, it needs to be there. If I don't communicate with the arduino that I no longer want key 'e' to be pressed, it will remain on when I want it off.

the following code:

void setup() {
  size(200, 200);
print(arduino.list());  
  arduino = new Arduino(this, Arduino.list()[0], 9600); //defines arduino our board and sets the communication rate
  arduino.pinMode(ledPin, Arduino.OUTPUT);
  }

should define the baud rate in processing.

and the data I am giving the arduino is turing pins on High or Low. I have motor drivers hooked up to these pins. So if I turn a pin on High, I am activating that motor driver, so then I know that the arduino is receiving it. So I don't necessarily need feedback to myself wirelessly to my computer. Plus wouldn't this cause more interference with receiving and sending wireless signals through my xbees?

Plus isn't my code

  Serial.println("case e received");
                digitalWrite( ledPin13, HIGH );
                Serial.println("Pin 13 HIGH");
                break;

the serial.println part sending me that its performing that action?

This is what I had for my older code (with firmata programmed on arduino):

import processing.serial.*; //Import the serial library into your sketch

import cc.arduino.*;        //Import the Arduino-Firmata library into your sketch

Arduino arduino;      //Create an instance of Arduino named arduino (can be any name) 

int ledPin;
int ledPinHIGH = 53;  //Create a varibale ledPin and assigns a value of 13 to it.
int ledPinHIGH1= 23;
int Leftmotorforward= 2;
int Leftmotorreverse= 3;
int Rightmotorforward= 4;
int Rightmotorreverse= 5;
int Rollerarmenable= 34;
int Rollerarmmotor= 35;
int Rollerarmforward= 43;
int Rollerarmbackwards= 42;

void setup() {
  size(200, 200);    
  arduino = new Arduino(this, Arduino.list()[0], 57600); //defines arduino our board and sets the communication rate
  arduino.pinMode(ledPin, Arduino.OUTPUT);
  }

void draw()
{
  arduino.digitalWrite(ledPinHIGH, Arduino.HIGH);
  arduino.digitalWrite(ledPinHIGH1, Arduino.HIGH);
};

void keyPressed() {
    print(key);
    switch (key) {
      case 'e':
      case 'E':
      arduino.digitalWrite(Leftmotorforward, Arduino.HIGH);
      break;
      case 'd':
      case 'D':
      arduino.digitalWrite(Leftmotorreverse, Arduino.HIGH);
      break;
      case 'i':
      case 'I':
      arduino.digitalWrite(Rightmotorforward, Arduino.HIGH);
      break;
      case 'k':
      case 'K':
      arduino.digitalWrite(Rightmotorreverse, Arduino.HIGH);
      break;
      case 'f':
      case 'F':
      arduino.digitalWrite(Rollerarmenable, Arduino.HIGH);
      arduino.digitalWrite(Rollerarmmotor, Arduino.LOW);
      break;
      case 'r':
      case 'R':
      arduino.digitalWrite(Rollerarmenable, Arduino.HIGH);
      arduino.digitalWrite(Rollerarmmotor, Arduino.HIGH);
      break;
      case 'u':
      case 'U':
      arduino.digitalWrite(Rollerarmforward, Arduino.HIGH);
      break;
      case 'j':
      case 'J':
      arduino.digitalWrite(Rollerarmbackwards, Arduino.HIGH);
      break;
    }
 
  };

void keyReleased() {
    switch (key) {
      case 'e':
      case 'E':
      arduino.digitalWrite(Leftmotorforward, Arduino.LOW);
      break;
      case 'd':
      case 'D':
      arduino.digitalWrite(Leftmotorreverse, Arduino.LOW);
      break;
      case 'i':
      case 'I':
      arduino.digitalWrite(Rightmotorforward, Arduino.LOW);
      break;
      case 'k':
      case 'K':
      arduino.digitalWrite(Rightmotorreverse, Arduino.LOW);
      break;
      case 'f':
      case 'F':
      arduino.digitalWrite(Rollerarmenable, Arduino.LOW);
      arduino.digitalWrite(Rollerarmmotor, Arduino.LOW);
      break;
      case 'r':
      case 'R':
      arduino.digitalWrite(Rollerarmenable, Arduino.LOW);
      arduino.digitalWrite(Rollerarmmotor, Arduino.LOW);
      break;
      case 'u':
      case 'U':
      arduino.digitalWrite(Rollerarmforward, Arduino.LOW);
      break;
      case 'j':
      case 'J':
      arduino.digitalWrite(Rollerarmbackwards, Arduino.LOW);
      break;
    }
};

it worked pretty good except had difficulties with it sometimes not communicating wirelessly (problems receiving my keyReleased or Pressed). Plus from researching, firmata doesn't allow you to use analog pins, hence why I am trying to re-write the code so that it works.