Go Down

Topic: Digital imput via arduino to processing (Read 3486 times) previous topic - next topic

Zenolin

Apr 30, 2011, 10:37 am Last Edit: Apr 30, 2011, 09:24 pm by Zenolin Reason: 1
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.

AWOL

#1
Apr 30, 2011, 10:46 am Last Edit: Apr 30, 2011, 10:47 am by AWOL Reason: 1
http://arduino.cc/forum/index.php/topic,59849.15.html

Code: [Select]
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.
"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.

AWOL

Quote
Now I changed coments

Code: [Select]
pinMode(switchPin, INPUT);  // Set pin 0 as an input

Ok, right.
"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.

Msquare

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

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.

Msquare


AWOL

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

Msquare

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.

Nick Gammon


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: [Select]
    Serial.print(0, BYTE);

(the last one, not the first one)


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

Code: [Select]
    Serial.print(2, BYTE);

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

Code: [Select]
00
01
10
11


Now look at your code and see what path will be taken for each of those.
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

Nick Gammon

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.
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

AWOL

#9
May 01, 2011, 11:23 am Last Edit: May 01, 2011, 11:26 am by AWOL Reason: 1
No, he meant using the # icon on the editor's toolbar to put the code in a proper code box.

Code: [Select]
Serial.print(00, BYTE);           
Zero is zero whether you express it in octal or decimal.
"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.

Nick Gammon


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


You lost me there.
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

bubulindo



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

Nick Gammon

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?
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

AWOL

Quote
Look, attention to detail is very, very important

Well, to be fair, you didn't specify that the combinations were written in binary  ;-)
"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.

Nick Gammon

Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

Go Up