How can i translate the arduino line to processing? if(sum == 0b1101 || sum == 0b0100 || sum == 0b0

edited January 2016 in Arduino

I based myself to build this circuit http://bildr.org/2012/08/rotary-encoder-arduino/ and started working it in processing 3.0 i'm "translating" the lines but the most importan ones, the ones that give me the values for the rotary encoder, keeps sending me the same error "Syntax error, maybe a missing right parenthesis?"

if(sum == 0b1101 || sum == 0b0100 || sum == 0b0010 || sum == 0b1011) encoderValue ++; if(sum == 0b1110 || sum == 0b0111 || sum == 0b0001 || sum == 0b1000) encoderValue --;

These are the lines i have problems with

`import processing.serial.*; import cc.arduino.*; PImage img;

Arduino arduino;

int encoderPin1 = 2; int encoderPin2 = 3; int encoderSwitchPin = 4; //push button switch

int buttonPin = 7 ; int ledPin = 13;

volatile int lastEncoded = 0; volatile long encoderValue = 0;

long lastencoderValue = 0;

int lastMSB = 0; int lastLSB = 0;

boolean buttonPushed = false; int buttonState = 0;

void setup() { size(600, 600);

arduino = new Arduino(this, Arduino.list()[0], 57600);

arduino.pinMode(encoderPin1, Arduino.INPUT); arduino.pinMode(encoderPin2, Arduino.INPUT);

arduino.pinMode(ledPin, Arduino.OUTPUT); arduino.pinMode(buttonPin, Arduino.INPUT);

arduino.pinMode(encoderSwitchPin, Arduino.INPUT);

arduino.digitalWrite(encoderPin1, Arduino.HIGH); arduino.digitalWrite(encoderPin2, Arduino.HIGH); arduino.digitalWrite(encoderSwitchPin, Arduino.HIGH);

attachInterrupt(0, updateEncoder, Arduino.CHANGE); attachInterrupt(1, updateEncoder, Arduino.CHANGE);

}

void draw(){ buttonState = arduino.digitalRead(buttonPin); img = loadImage ("robotina.jpg"); if(arduino.digitalRead(encoderSwitchPin) == Arduino.HIGH){ buttonPushed= false;
} else{ println("button pushed");

println (encoderValue);

}

if (buttonState == Arduino.HIGH) { println("back button"); arduino.digitalWrite(ledPin, Arduino.HIGH); image (img,0,0); } else{ arduino.digitalWrite(ledPin, Arduino.LOW); }

//println(encoderValue);
delay(100); }

void updateEncoder(){ int MSB = arduino.digitalRead(encoderPin1); //MSB = most significant bit int LSB = arduino.digitalRead(encoderPin2); //LSB = least significant bit

int encoded = (MSB << 1) |LSB; //converting the 2 pin value to single number int sum = (lastEncoded << 2) | encoded; //adding it to the previous encoded value

//this is where i keep getting the error

if(sum == 0b1101 || sum == 0b0100 || sum == 0b0010 || sum == 0b1011) encoderValue ++; if(sum == 0b1110 || sum == 0b0111 || sum == 0b0001 || sum == 0b1000) encoderValue --;

lastEncoded = encoded; //store this value for next time }`

Answers

  • edited January 2016 Answer ✓

    https://forum.Processing.org/two/discussion/8045/how-to-format-code-and-text

    Although the Java language itself got support for binary literals using prefix 0b since version 7,
    due to Processing's IDE (PDE) pre-processor, we can't! For this and for many other features!!! X(

    You can grab some calculator w/ "Programmer Mode" (Windows comes bundled w/ 1).
    Select "Bin", type in the 1s & 0s. Then switch to "Dec"; or even "Hex" or "Oct".
    Then you can type in the now converted binary literal into the PDE.

    For example 0b1101 becomes 13 in decimal, 0xD in hexadecimal, and 015 in octal. *-:)
    All of those last 3 base literal values are accepted by PDE's pre-processor. ~O)

    http://docs.Oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html

  • edited January 2016 Answer ✓

    for the binary challenged:

    0b0000 = 0x0 = 0
    0b0001 = 0x1 = 1
    0b0010 = 0x2 = 2
    0b0011 = 0x3 = 3
    0b0100 = 0x4 = 4
    0b0101 = 0x5 = 5
    0b0110 = 0x6 = 6
    0b0111 = 0x7 = 7
    0b1000 = 0x8 = 8
    0b1001 = 0x9 = 9
    0b1010 = 0xA = 10
    0b1011 = 0xB = 11
    0b1100 = 0xC = 12
    0b1101 = 0xD = 13
    0b1110 = 0xE = 14
    0b1111 = 0xF = 15
    
  • Now there is no error in that part thanks! the next thing i noticed i can't get it to work is the function "attach interrupt" but because it says that the variable desn't exist.

    `import processing.serial.*; import cc.arduino.*; PImage img;

    Arduino arduino;

    int encoderPin1 = 2; int encoderPin2 = 3; int encoderSwitchPin = 4; //push button switch

    int buttonPin = 7 ; int ledPin = 13;

    volatile int lastEncoded = 0; volatile long encoderValue = 0;

    long lastencoderValue = 0;

    int lastMSB = 0; int lastLSB = 0;

    boolean buttonPushed = false; int buttonState = 0;

    void setup() { size(600, 600);

    arduino = new Arduino(this, Arduino.list()[0], 57600);

    arduino.pinMode(encoderPin1, Arduino.INPUT); arduino.pinMode(encoderPin2, Arduino.INPUT);

    arduino.pinMode(ledPin, Arduino.OUTPUT); arduino.pinMode(buttonPin, Arduino.INPUT);

    arduino.pinMode(encoderSwitchPin, Arduino.INPUT);

    arduino.digitalWrite(encoderPin1, Arduino.HIGH); arduino.digitalWrite(encoderPin2, Arduino.HIGH); arduino.digitalWrite(encoderSwitchPin, Arduino.HIGH);

    ****//------------ "THE VARIABLE "UpdateEncoder" DOESN'T EXIST

    arduino.attachInterrupt(0, updateEncoder, Arduino.CHANGE); arduino.attachInterrupt(1, updateEncoder, Arduino.CHANGE);****

    //attachInterrupt(0, updateEncoder, Arduino.CHANGE); //attachInterrupt(1, updateEncoder, Arduino.CHANGE); }

    void draw() { buttonState = arduino.digitalRead(buttonPin); img = loadImage ("robotina.jpg");

    if (arduino.digitalRead(encoderSwitchPin) == Arduino.HIGH) { buttonPushed= false; } else { println("button pushed");

    //println (encoderValue);
    

    }

    if (buttonState == Arduino.HIGH) { println("back button"); arduino.digitalWrite(ledPin, Arduino.HIGH); image (img, 0, 0); } else { arduino.digitalWrite(ledPin, Arduino.LOW); }

    //println(encoderValue); delay(100); }

    void updateEncoder() { int MSB = arduino.digitalRead(encoderPin1); //MSB = most significant bit int LSB = arduino.digitalRead(encoderPin2); //LSB = least significant bit int LSB = arduino.digitalRead(encoderPin2);

    int encoded = (MSB << 1) |LSB; //converting the 2 pin value to single number int sum = (lastEncoded << 2) | encoded; //adding it to the previous encoded value int sum = (lastEncoded << 2) | encoded; //this is where i keep getting the error

    if (sum == 13 || sum == 4 || sum == 2 || sum == 11) encoderValue ++; if (sum == 14 || sum == 7 || sum == 1 || sum == 8) encoderValue --;

    lastEncoded = encoded; //store this value for next time } `

    I think is the only thing missing for the code to work.

  • select code, hit ctrl-o...

Sign In or Register to comment.