Button Press Code

Guys,

Need help.

I have an Arduino. Pins 2 & 3 are set to off (low), connected to pins 2 & 3 are two lines that send a On (high) signal. So on High, I want it to send keystrokes.

Something is off in my code and when I connect the Arduino to USB it just keeps sending keystrokes.

Requirements: pin 2 & pin 3 have to be on low/off position. The two wires are sending the high (on) voltage signal.

The code is below.

Thank you,

Super Duper Noob

#define buttonPin1 2 
#define buttonPin2 3

int state = 0; 

void setup()
{
  Serial.begin(9600);

  pinMode(buttonPin1, INPUT);
  pinMode(buttonPin2, INPUT);

  digitalWrite(buttonPin1, 0);
  digitalWrite(buttonPin2, 0);


  delay(200);
}

void loop()
{

  // ButtonPin1
  state = digitalRead(buttonPin1);
  if (state != 1) {
    buf[0] = 0;
    buf[2] = 27; 
    Serial.write(buf, 8);
    releaseKey();
    delay(900);
  }

  // ButtonPin2
  state = digitalRead(buttonPin2);
  if (state != 1) {
    buf[0] = 0;
    buf[2] = 23;    
    Serial.write(buf, 8);
    releaseKey();
    delay(200);
  }

}

void releaseKey()
{
  buf[0] = 0;
  buf[2] = 0;
  Serial.write(buf, 8); // Release key
  delay(500);
}

i'm surprised it uploaded it at all. or your not showing all the code.

int state = 0;

state can be a "byte" instead of a "int" it ony needs to hold 1 or 0. you will never exceed 255.

pinMode(buttonPin1, INPUT);
pinMode(buttonPin2, INPUT);

digitalWrite(buttonPin1, 0);
digitalWrite(buttonPin2, 0);

first you put them as a INPUT. then you tell it to OUTPUT a value. :confused:
dont do this. use real buttons.

for testing just use a dummy variable and change that variable. to 1 or 0.

if (state != 1) {
  //Code.....
  }

oke lets read this.
IF STATE "is not equeal to" 1 lets run code in between curly brackets.
i think input 1 & 2 are both "0" so not equel to 1 therefore the result is positive or true and will run the keystroke.

One does not make inputs low. The only thing that that will do is switch off the internal pull-up and in your case is not needed. You need to add pull- down resistors on the inputs so they will not pick up random noise.

Alternatively, if you can swap the logic around, you can use INPUT_PULLUP instead of INPUT.