Pages: [1]   Go Down
Author Topic: Increase a value, stupid question.  (Read 2010 times)
0 Members and 1 Guest are viewing this topic.
Baltic Sea
Offline Offline
Newbie
*
Karma: 0
Posts: 9
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi,
I'm new to Programming. After a hour of searching i didn't find a answer.
How can I raise a value on a key press?
I like to add (and on another button subtract) a 1 to the "steer"-value, to calibrate my steering for my RC-car which is attached via XBEE to Arduino.

I hope someone can help me.

btw: sorry for my bad english

Code:
/*  RC car project | Arduino UNO | zH32
   
*/
import processing.serial.*;
import controlP5.*;
import procontroll.*;
import net.java.games.input.*;
import java.io.*;

ControlP5 controlP5;
ControllIO controll;
ControllDevice device;
ControllStick stick;
ControllStick stick1;
ControllButton button1;
ControllButton button;

Serial myPort;
void setup()
{
  println(Serial.list());
 
  size(1024,600);
  myPort = new Serial(this, "COM32", 57600);
 

  frameRate(20);
 
  PFont myFont;
  myFont = createFont("Arial", 20);
  textFont(myFont);
  controlP5 = new ControlP5(this);
  controlP5.addSlider("Lenkung in %",-100,100,50,10,50,400,40);
  controlP5.addSlider("Power in %",-100,100,100,30,150,40,400);
  controlP5.addSlider("Licht",0,100,25,200,150,40,400);
  controll = ControllIO.getInstance(this);
  device = controll.getDevice("Logitech Cordless RumblePad 2");
 
  device.setTolerance(0.05f);
 
 
  ControllSlider sliderX = device.getSlider("Z-Achse");
  ControllSlider sliderY = device.getSlider("Z-Rotation");
  ControllSlider sliderX1 = device.getSlider("X-Achse");
  ControllSlider sliderY1 = device.getSlider("Y-Achse");
 
  stick = new ControllStick(sliderX,sliderY);
  stick1 = new ControllStick(sliderX1,sliderY1);

          // get right value for servo
  button = device.getButton("Taste 0");
  button1 = device.getButton("Taste 2");
 }


void draw()
{
  background(150);

  String n1;
  String n2;
  String sending;
  int speed = int(stick.getY()*-255+255); // get right value for motor controller
  int steer = int((stick1.getX()*45)+90);
  n1 = Integer.toString(steer);
  n2 = Integer.toString(speed);
  // if n1 or n2 is less than 4 digits add a zero to the front of them
  while (n1.length() < 4){
    n1 = '0' + n1;
  }
  while (n2.length() < 4){
    n2 = '0' + n2;
  }
  //combine the two 4 digit long strings into one 8 digit long one
  sending = n1 + n2;
  //write this to the serial port.
  myPort.write(sending);
  println(sending);
  controlP5.controller("Lenkung in %").setValue((steer-90)*2.5);
  controlP5.controller("Power in %").setValue(map(speed, 0, 510, -100, 100));
  fill(0);
  text("Sendestring: "+ sending, 800, 580);
  delay(20);

}
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 553
Posts: 46291
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
How can I raise a value on a key press?
There are callback methods for key press events. You need to override the callback method OnKeyPress() to check for the specific keys of interest.

steer++; // increment the value
steer--; // decrement the value
Logged

Baltic Sea
Offline Offline
Newbie
*
Karma: 0
Posts: 9
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

this solves the problem:

Code:
void keyPressed() {
  if (keyCode == RIGHT){
  if (value == 0) {
    value ++;
  } else {
    value ++;
  }}
  if (keyCode == LEFT){
  if (value == 0) {
    value --;
  } else {
    value --;
  }}
}
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 553
Posts: 46291
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
if (value == 0) {
    value ++;
  } else {
    value ++;
  }
What is the purpose of the if test when you do the same thing regardless of whether the condition is true or false?
Logged

Pages: [1]   Go Up
Jump to: