Pages: [1] 2   Go Down
Author Topic: Digital imput via arduino to processing  (Read 2401 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 7
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
« Last Edit: April 30, 2011, 02:24:21 pm by Zenolin » Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 240
Posts: 24430
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Code:
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.
« Last Edit: April 30, 2011, 03:47:49 am by AWOL » Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 240
Posts: 24430
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Now I changed coments
Code:
pinMode(switchPin, INPUT);  // Set pin 0 as an input

Ok, right.
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Copenhagen, Denmark
Offline Offline
Edison Member
*
Karma: 25
Posts: 1147
Have you testrun your INO file today?
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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  smiley )

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.
Logged

Copenhagen, Denmark
Offline Offline
Edison Member
*
Karma: 25
Posts: 1147
Have you testrun your INO file today?
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I am fed up. Please make me happy.
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 240
Posts: 24430
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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.
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Copenhagen, Denmark
Offline Offline
Edison Member
*
Karma: 25
Posts: 1147
Have you testrun your INO file today?
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Zenolin, in reply to the message you sent me
Quote
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.
Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 452
Posts: 18694
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?

Code:
    Serial.print(0, BYTE);

(the last one, not the first one)


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

Code:
    Serial.print(2, BYTE);

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

Code:
00
01
10
11

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

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 452
Posts: 18694
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 240
Posts: 24430
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Code:
Serial.print(00, BYTE);           
Zero is zero whether you express it in octal or decimal.
« Last Edit: May 01, 2011, 04:26:52 am by AWOL » Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 452
Posts: 18694
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

You lost me there.
Logged

'round the world...
Offline Offline
Faraday Member
**
Karma: 41
Posts: 3113
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

  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).
Logged

Eu não sou o teu criado. Se respondo no fórum é para ajudar todos mediante a minha disponibilidade e disposição. Responder por mensagem pessoal iria contra o propósito do fórum e por isso evito-o.
Se realmente pretendes que eu te ajude por mensagem pessoal, então podemos chegar a um acordo e contrato onde me pagas pela ajuda que eu fornecer e poderás então definir os termos de confidencialidade do meu serviço. De forma contrária toda e qualquer ajuda que eu der tem de ser visível a todos os participantes do fórum (será boa ideia, veres o significado da palavra fórum).
Nota também que eu não me responsabilizo por parvoíces escritas neste espaço pelo que se vais seguir algo dito por mim, entende que o farás por tua conta e risco.

Dito isto, mensagens pessoais só se forem pessoais, ou seja, se já interagimos de alguma forma no passado ou se me pretendes convidar para uma churrascada com cerveja (paga por ti, obviamente).

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 452
Posts: 18694
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Oh, OK.

@Zenolin:

http://en.wikipedia.org/wiki/Number_base

Look, attention to detail is very, very important.

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

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 240
Posts: 24430
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Look, attention to detail is very, very important
Well, to be fair, you didn't specify that the combinations were written in binary  ;-)
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 452
Posts: 18694
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Let me put it like this:

你看,注重细节是非常,非常重要。
Logged

Pages: [1] 2   Go Up
Jump to: