Little transformation :)

I need to transform this lines of arduino to processing !

bitWrite(commande,0,a);
  bitWrite(commande,1,b);
  bitWrite(commande,2,c);
  bitWrite(commande,3,d);
  bitWrite(commande,4,e);
  bitWrite(commande,5,f);
  bitWrite(commande,6,g);
  bitWrite(commande,7,h);
  
  Serial.write(0xAA);  
  Serial.write(commande); 
  Serial.write(0x55);

Thx :slight_smile:

I don't understand that code. Maybe if you explain what it is doing, or what you want to do, we can help.

My guess is that you have 8 booleans (a-h) and you want to combine them into a single byte?

byte val = a;
val |= b << 1;
val |= c << 2;
val |= d << 3;
val |= e << 4;
val |= f << 5;
val |= g << 6;
val |= h << 7;

Absolutly yes ! I'll try this code =) .

This older thread may be of help.
http://forum.arduino.cc/index.php/topic,42318.0.html

Arduino bitWrite() sets or clears a bit.
http://arduino.cc/en/Reference/BitWrite

Syntax

bitWrite(x, n, b)

Parameters

x: the numeric variable to which to write

n: which bit of the number to write, starting at 0 for the least-significant (rightmost) bit

b: the value to write to the bit (0 or 1)

That's a convenience function you could make for yourself in Processing.

Processing has the standard bit operations though I don't think they are all shown here:

Bitwise Operators
& (bitwise AND)
<< (left shift)

(right shift)
| (bitwise OR)

If I want to set bit 3 (where the bits are 7 6 5 4 3 2 1 0 ) in a byte (unsigned 8 bit) then I would

byte mybyte = 0;
mybyte = mybyte | ( 1 << 3 );

If I want to clear bit 3 using only those operators shown (I don't want to dig) then I would

byte mybyte = 0;
mybyte = mybyte & ( 255 - ( 1 << 3 ));

and yes I'm sure there's a better way but I'm not a Processing veteran and it is Java....

You have to include the Serial library in Processing to use it. Do the examples to learn, it's very much like Arduino Serial which is no surprise since Arduino was derived from Processing.
http://www.processing.org/reference/libraries/serial/index.html

This is some explications : I'm controlling a wirless car via proceesing (keyboard and mouse) , everything work fine with arduino , i tried this code , work fine but i have a little problem with "key" .

byte a,b,c,d,e,f,g,h;
if (key == 'z' )       {a=1;}   else{a=0;}   
  if (key == 'd' )       {b=1;}   else{b=0;}    
  if (key == 'q' )       {c=1;}   else{c=0;}
  if (key == 's' )       {d=1;}   else{d=0;}
  if (keyCode == LEFT )    {e=1;}  else{e=0;}
  if (keyCode == RIGHT )  {f=1;}  else{f=0;}
  if (keyCode == UP ) {g=1;}  else{g=0;}
  if (keyCode == DOWN )  {h=1;} else{h=0;}  
  
  byte val = a;
val |= b << 1;
val |= c << 2;
val |= d << 3;
val |= e << 4;
val |= f << 5;
val |= g << 6;
val |= h << 7;

  myPort.write(0xAA);
  myPort.write(val);
  myPort.write(0x55);
  delay(100);
  val=0; 
  key='O';

Some help ?

I need a fonction like key which allow me to touch all keys and send the command because i can't send z,q,s,d,UP,DOWN,LEFT,RIGHT simultaneously :s

I have no idea how you'd do it in the over-abstracted processing environment, but in Java I'd listen to the KeyEvents KeyEvent (Java Platform SE 6)

You would probably do better wiring an old non-USB game controller to the Arduino, or making your own control box.

You could play around with keypressed in the same way that Arduino has Serial.available() to see what you can get away with for pressing multiple keyboard keys "at the same time", just to see if they all get through and you can catch each in turn. It's close to impossible to trigger keys -at exactly the same moment- in terms of computer speed so you might be able to catch them all.
You might look into keyreleased() as well. Also consider what auto-repeat will do.

Or you can wire controls to the Arduino and quit screwing with the keyboard as unsuitable for your purposes.

Hi , i'm using processing and i want find a solution for my problem :
I'm sending a byte wirelessly to control a car , i wana control it via keyBoard but i have a problem with "key" , when i touch 'z' it send 'z' every time until i touch another key , another problem , i can't send two or more keys instantly :confused:
This is the code :

byte a,b,c,d,e,f,g,h;
if (key == 'z' )       {a=1;}   else{a=0;}         // i need modifications over here :s
  if (key == 'd' )       {b=1;}   else{b=0;}    
  if (key == 'q' )       {c=1;}   else{c=0;}
  if (key == 's' )       {d=1;}   else{d=0;}
  if (keyCode == LEFT )    {e=1;}  else{e=0;}
  if (keyCode == RIGHT )  {f=1;}  else{f=0;}
  if (keyCode == UP ) {g=1;}  else{g=0;}
  if (keyCode == DOWN )  {h=1;} else{h=0;}  
  
  byte val = a;   
val |= b << 1;
val |= c << 2;
val |= d << 3;
val |= e << 4;
val |= f << 5;
val |= g << 6;
val |= h << 7;

  myPort.write(0xAA);  // sending first package
  myPort.write(val);     // sending the command
  myPort.write(0x55);  // sending the final package
  delay(100);
  val=0;           // initializing val
  key='O';        // i wana change it , i wrote it to initialize key , but it don't work very well

I hope you'll help me and sorry for bad english :s

This is the same question as your other post!

Mark

Please do not cross-post. This wastes time and resources as people attempt to answer your question on multiple threads.

Threads merged.

  • Moderator

I note you also posted this question in the French section. That is still cross-posting. Make up your mind what language you want to ask in. French post deleted.

Sorry :s
I'm a little hasty :confused:

If you don't press keys within perhaps 10 milliseconds of each other then you can probably do it using the keypress and key commands.

so what is the apropriate code according to you ? =)

You want to use a pc, running windows, and hit 5 or 6 keys on the keyboard at the same time and have all those keys sent to the arduino?
Well try a text editor on your pc. Hit 5 keys at the same time and see if the pc can show all those keys. No, it can't. So the first problem is with your pc. Arduino could probably compute it, if the pc could provide it.

stribuda:
so what is the apropriate code according to you ? =)

Something that will take a good bit of trial, error and work. And even then there will be things you can't do that if you didn't go through all that you would not understand and tell the person who did to try again but do better. And I know better than to be the person at that end from experience, thank you.

The smart move would be to wire an old game controller to the Arduino and use that instead of the unsuitable keyboard. Or make something, there are examples plenty of how.

The mouse, you can use. The keyboard is good for 1 key at a time. For a game controller that can be many things at once pressed, people make things that plug into the PC.

If you need to, you can run more than one Arduino (you can build your own) at the same time but for your need, one should do for the car and the controller.

Or you can get a new style USB game controller, plug that into the PC and find out how and IF you can read those inputs through Processing. That may or may not be harder and more expensive but this is about what You want and You are willing to do to get that.

Thank you all for help , the code works very well , no more problems of sending data , i'm working now on camera and the display part ! =)