simple password query

edited January 2014 in Arduino

Hi all,

I tryed the whole day finding a solution for this but i was not able :( Maybe someone could help me out here?

For a little Project I have two LED's attached to the Arduino and i try to send data from Processing to Arduino. The Arduino scetch is simple. It's actually not doing more than set the pin13 LED high / low if it receives a '1' or '0' -> This works fine.

The problem is the Processing code. I'm using cp5 for the interface. I can send a single char to Arduino but not a greater number like 1008. I even tried to parse the int to a string to compare the values but it's not working.

Any idea?

The processing code:

import processing.serial.*;
import controlP5.*;

ControlP5 cp5;

int ledPin = 13; // LED connected to digital pin 13

Serial myPort;  // Create object from Serial class

int pass = 1008;
String str = Integer.toString(pass);

void setup() {
  size(700, 400);
  String portName = Serial.list()[0]; //change the 0 to a 1 or 2 etc. to match your port
  myPort = new Serial (this, "/dev/tty.usbmodem1421", 9600);
  PFont font = createFont("arial", 20);

  cp5 = new ControlP5(this);

  cp5.addTextfield("input")
    .setPosition(20, 100)
      .setSize(200, 40)
        .setFont(font)
          .setFocus(true)
            .setColor(color(255, 0, 0))
              ;


  cp5.addBang("clear")
    .setPosition(240, 170)
      .setSize(80, 40)
        .getCaptionLabel().align(ControlP5.CENTER, ControlP5.CENTER)
          ;    

  cp5.addBang("enter")
    .setPosition(340, 170)
      .setSize(80, 40)
        .getCaptionLabel().align(ControlP5.CENTER, ControlP5.CENTER)
          ;   

  textFont(font);
}

void draw() {
  background(0);
  fill(255);
  text(cp5.get(Textfield.class, "input").getText(), 360, 130);
}

public void clear() {
  cp5.get(Textfield.class, "input").clear();
  myPort.write('0');
}

public void enter( String theValue) {
  cp5.get(Textfield.class, "enter");
  if (pass.equals("theValue")) {
    myPort.write('1');
  }
}

Answers

  • I'm not totally sure what you're asking. You also seem to be trying to call .equals() on an int variable (pass), which isn't going to work.

    Are you asking how to retrieve a value from somewhere? Are you asking how to convert an int to a String? Something else?

  • Oh, sorry for that.. I don't know if this is the right way to do it - i just ended up with this because i tried a lot of things.

    If you start the scetch the interface shows a textField in which you should type in your password. Therefore it should be a comparison between two values. One that is set as the right password (int pass) and the one which the user is tiping in (theValue).

    But something like if ( pass == 1234) {..} is not working only pass == 1 or H was working.

  • edited January 2014

    I don't know anything about arduino or even ControlP5, but I think what you're asking is more basic than that. In your enter() function, why do you have your theValue variable in quotation marks? I would guess you want to compare your password to the value of theValue, not the String "theValue", right?

    So if you're asking how to compare a password stored as an int and a user entry stored as a String, the answer is you have to convert one to the other. You can either convert the value entered by the user from a String to an int using the int() function, or you can convert the int password to a String using the str() function. Consult the API for more info: http://www.processing.org/reference/

    I also suggest starting with a much simpler program. Create a little test sketch that only contains a setup() function and tests the above conversions. Something like this:

    void setup(){
       int intPassword = 123;
       String userEntry = "456";
       int userEntryConverted = int(userEntry);
       if(userEntryConverted == intPassword){
          println("Passwords match!");
       }
       else{
          println("Passwords don't match!");
       }
    }
    

    If I'm wrong about what you're asking, try boiling your problem down into a smaller program like this one, and it'll be more obvious exactly where you're stuck.

  • Thank you for your help! Yes, i also have this feeling that the problem is more basic. Actually the entered value don't have to be a String. In fact, it will always be an int anyway.. I think that's a good start. As the 'userEntry' value is set by the user, i only will have to figure out how you can assign the value to the if loop.

Sign In or Register to comment.