Digital imput via arduino to processing

Processing code:

import processing.serial.*;

Serial myPort; // Create object from Serial class
int val; // Data received from the serial port

void setup()
{
size(200, 200);
// I know that the first port in the serial list on my mac
// is always my FTDI adaptor, so I open Serial.list()[0].
// On Windows machines, this generally opens COM1.
// Open whatever port is the one you're using.
String portName = Serial.list()[0];
myPort = new Serial(this, portName, 9600);
}

void draw()
{
if ( myPort.available() > 0) { // If data is available,
val = myPort.read(); // read it and store it in val
}
background(255); // Set background to white
if (val == 2) { // If the serial value is 2,
fill(0); // set fill to black

} else if (val == 1) {
fill (204);
} else {
fill (204, 102, 0);

} rect(50, 50, 100, 100);
}

//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Arduino code:

// Wiring / Arduino Code
// Code for sensing a switch status and writing the value to the serial port.

int switchPin = 4; // Switch connected to pin 4
int ZZZZ = 7; // Switch connected to pin 7

void setup() {
pinMode(switchPin, INPUT);
pinMode(ZZZZ, INPUT);
Serial.begin(9600); // Start serial communication at 9600 bps
}

void loop() {

if (digitalRead(switchPin) == HIGH) { // If switch is ON,
Serial.print(1, BYTE); // send 1 to Processing
} else { // If the switch is not ON,
Serial.print(0, BYTE); // send 0 to Processing
}

if (digitalRead(ZZZZ) == HIGH) { // If switch is ON,
Serial.print(2, BYTE); // send 2 to Processing
} else { // If the switch is not ON,
Serial.print(0, BYTE); // send 0 to Processing
}
delay(100); // Wait 100 milliseconds
}

Do not workink good. I did mistake in arduino code , but i do not now there. Please help.

http://arduino.cc/forum/index.php/topic,59849.15.html

int ZZZZ = 7;  // Switch connected to pin 4

pinMode(ZZZZ, INPUT);  // Set pin 0 as an input

Comments like that are simply noise. They should not be there.

Now I changed coments

 pinMode(switchPin, INPUT);  // Set pin 0 as an input

Ok, right.

It is always instructive to see the other posts made by a person asking... So, I gather you finally have gotten the Processing/Arduino distiction sorted out (earlier thread)

Lets see what you are doing. The Arduino code is sending two bytes roughly ten times a second. It can be sending 00, 10, 02 or 12 depending on the state of the switches.

The Processing colours a rectangle : IF "2" then white, if "1" then gray, else brown.

So.... if you only press one button on the Arduino then the other button's "0" signal will colour the rectangle ... (that is a hint)

Please please, when asking question make it clear which bit is not working. You may find - I have a few times - that explaining precisly what goes wrong, makes you find the error yourself (usually while writing the post, which then does not have to be sent :slight_smile: )

EACH time you ask include three things:
**1: What did you do?**That is the code, the circuit diagram, what commands, button pushes you do.
2: What did you want to happen? (f.ex: The LED blinks 5 times at left button push; the LED turns Green on temperature exceeding 20C)
3: What actually did happen? (f.ex: The LED blinked once; The RGB-LED turn Yellow instead of Green at 20C and turned Green at 30C)

BTW, there is a Processing forum, too.

I am fed up. Please make me happy.

I am fed up. Please make me happy.

Two men meet in the street.
The first one says "How are you - I haven't seen you for ages"
"Oh, I'm OK, but my wife has left me. She said she was going out to buy a pint of milk and never came back", says the second.
"That's awful!", said the first, "How are you managing?"
"I'm just having to use the powdered stuff", says the second.

Zenolin, in reply to the message you sent me

plese help me.
Here not mistake, bet do not work good.

void loop() {

if (digitalRead(switchPin) == HIGH) { // If switch is ON,
Serial.print(1, BYTE); // send 1 to Processing
} else { // If the switch is not ON,
Serial.print(0, BYTE); // send 0 to Processing
}
delay(100);
}

if (digitalRead(ZZZZ) == HIGH) { // If switch is ON,
Serial.print(2, BYTE); // send 2 to Processing
} else { // If the switch is not ON,
Serial.print(0, BYTE); // send 0 to Processing
}
// delay(100); // Wait 100 milliseconds

Bad English is excusable, I can understand despite numerous spelling mistakes, wrong grammer and worse.

It is that you still have not said what it is that does not work. You expect me (or other Forum users), from the program and your cryptic "bet do not work good" to guess what you are expecting to happen, from what I can derive that the program actually does (and that is not always easy). In this case, is it showing the wrong colurs on your Processing or too briefly? Are your buttons something that is held down a long time or something that happens very quickly?

(1) What are you doing (2) What are wanting to happen (3) what is actually happening

I can't be bothered with so little info. Sorry.

Zenolin:
blinkink screen with it :
void loop() {
if ((digitalRead(switchPin) == LOW) || (digitalRead(switchPin3) == LOW)) { // If switch is ON,
Serial.print(0, BYTE);
} else if (digitalRead(switchPin) == HIGH) {
Serial.print(1, BYTE);
} else if (digitalRead(switchPin3) == HIGH){ // If switch is ON,
Serial.print(2, BYTE);
} else {
Serial.print(0, BYTE);
}

You want help enough to make scrolling messages in all caps, right?

Well can you answer this ...

You have two switches, switchPin and switchPin3. Now can you explain under what circumstances you conceive that his line of code will be executed?

    Serial.print(0, BYTE);

(the last one, not the first one)

Also can you explain when you think that it will execute this:

    Serial.print(2, BYTE);

Let me give you a hint. Do a truth table. There are four possible combinations:

00
01
10
11

Now look at your code and see what path will be taken for each of those.

Also, if you want help that badly, how about learning how to post your code in "code" blocks like I did above? It is part of the forum, and makes your posts more readable, and thus more likely to be answered.

No, he meant using the # icon on the editor's toolbar to put the code in a proper code box.

Serial.print(00, BYTE);

Zero is zero whether you express it in octal or decimal.

Zenolin:
if (val == 10) { // If the serial value is 2,

You lost me there.

0b10 = 2 = 0x02. That's what I think he's trying.

This is the dark side of Arduino... people get empowered by the little device, but there are some things you can't do if you don't put an effort into it. (althgouh in this case it might be lack of information in whatever language Zenolin speaks).

Oh, OK.

@Zenolin:

Look, attention to detail is very, very important.

If I said to you go buy 10 apples, would you return with 2 of them?

Look, attention to detail is very, very important

Well, to be fair, you didn't specify that the combinations were written in binary :wink:

Let me put it like this:

????????????????

Quite so
Mein Luftkissenfahrzeug ist mit Aalen gefüllt

How about answering my questions in reply #14.

AWOL:
Mein Luftkissenfahrzeug ist mit Aalen gefüllt

Sie haben schöne Oberschenkel.

Zenolin:
Sorry, was bad coment, now I changed. How now ?

So far I haven't seen a single line of code that you have posted with the [ code ] tags as we have repeatedly requested. You want people to help you but are not prepared to take a couple of minutes to work out how to do that.

Your comments do not agree with the code you post. That shows a lack of attention to detail. Detail is what makes programs work. The computer cannot read your mind, nor can we. Code like this (bearing in mind the comments) show a total confusion on your part:

int switchPin = 4;                       // Switch connected to pin 4

...
  pinMode(switchPin, INPUT);      // Set pin 0 as an input

Meanwhile comments like this, while correct:

  if (val == 2) {              // If the serial value is 2,

... simply do not add anything at all to the code.

Imagine if I said this:

if (a == 2)   // if a is 2

What has the comment done? Has it helped in any way?

Or this:

a = 42;   // make a have the value 42

Try to think clearly. Programming is not about writing "stuff that looks like programs". It is about understanding what you are doing.