user28496: I need to create something for a school assignment, but I'm stuck at the moment.
I have one code, which makes the Arduino Leonardo press space button on my laptop, which works.
I have another code, that turns on the LED when 2 wires are put in water.
Now I want to combine them, but I have no success.
Hope someone will help me with this.
These are the codes: 
The keyboard code leonardo:
bool eenkeergeweest = false;

#include <Keyboard.h>

void setup()
{
    // make pin 2 an input and turn on the
    // pullup resistor so it goes high unless
    // connected to ground:
    pinMode(2, INPUT_PULLUP);
    // initialize control over the keyboard:
    Keyboard.begin();
}

void loop()
{
    // while (digitalRead(2) == HIGH) {
    //   // do nothing until pin 2 goes low
    //   delay(500);
    //  }
    delay(1000);
    // new document:
    if (!eenkeergeweest) {
        eenkeergeweest = true;
        Keyboard.print(' ');
        delay(100);
        //Keyboard.releaseAll();
    }

    // wait for new window to open:
    delay(1000);
}

The code for the water LED:
int out = 12;
int in = 4;

int rd;

void setup()
{
    pinMode(out, OUTPUT);
    pinMode(in, INPUT);
}

void loop()
{
    rd = digitalRead(in);
    if (rd == HIGH)
    {
        digitalWrite(out, HIGH);
        delay(100);
    }
    else
    {
        digitalWrite(out, LOW);
        delay(100);
    }
}


user10394: How do you want them to work ? Do you want the spacebar pressed when there is a high signal read (when the light goes on)?
If you want the space bar press when it goes high this might work:
#include <Keyboard.h>

bool eenkeergeweest = false;
int out = 12;
int in = 4;

void toggleKeyPress (const char& keypress)
{
    if (!eenkeergeweest) 
    {
        eenkeergeweest = true;
        Keyboard.print(' ');
        delay(100);
    }
}

void setup()
{
    pinMode(out, OUTPUT);
    pinMode(in, INPUT);

    // initialize control over the keyboard:
    Keyboard.begin();

}


void loop()
{
    const int rd = digitalRead(in);
    if (rd == HIGH)
    {
        toggleKeyPress(' ');
        digitalWrite(out, HIGH);
        // wait for new window to open:
        delay(1000);   
    }
    else
    {
        digitalWrite(out, LOW);
    }
}


28496: <p>I need to create something for a school assignment, but I'm stuck at the moment.<br>
I have one code, which makes the Arduino Leonardo press space button on my laptop, which works.</p>

<p>I have another code, that turns on the LED when 2 wires are put in water.</p>

<p>Now I want to combine them, but I have no success.</p>

<p>Hope someone will help me with this.</p>

<p>These are the codes: </p>

<p><strong>The keyboard code leonardo:</strong></p>

<pre class="lang-cpp prettyprint-override"><code>bool eenkeergeweest = false;

#include &lt;Keyboard.h&gt;

void setup()
{
    // make pin 2 an input and turn on the
    // pullup resistor so it goes high unless
    // connected to ground:
    pinMode(2, INPUT_PULLUP);
    // initialize control over the keyboard:
    Keyboard.begin();
}

void loop()
{
    // while (digitalRead(2) == HIGH) {
    //   // do nothing until pin 2 goes low
    //   delay(500);
    //  }
    delay(1000);
    // new document:
    if (!eenkeergeweest) {
        eenkeergeweest = true;
        Keyboard.print(' ');
        delay(100);
        //Keyboard.releaseAll();
    }

    // wait for new window to open:
    delay(1000);
}
</code></pre>

<p><strong>The code for the water LED:</strong></p>

<pre class="lang-cpp prettyprint-override"><code>int out = 12;
int in = 4;

int rd;

void setup()
{
    pinMode(out, OUTPUT);
    pinMode(in, INPUT);
}

void loop()
{
    rd = digitalRead(in);
    if (rd == HIGH)
    {
        digitalWrite(out, HIGH);
        delay(100);
    }
    else
    {
        digitalWrite(out, LOW);
        delay(100);
    }
}
</code></pre>
 10394: <p>How do you want them to work ? Do you want the spacebar pressed when there is a high signal read (when the light goes on)?</p>

<p>If you want the space bar press when it goes high this might work:</p>

<pre><code>#include &lt;Keyboard.h&gt;

bool eenkeergeweest = false;
int out = 12;
int in = 4;

void toggleKeyPress (const char&amp; keypress)
{
    if (!eenkeergeweest) 
    {
        eenkeergeweest = true;
        Keyboard.print(' ');
        delay(100);
    }
}

void setup()
{
    pinMode(out, OUTPUT);
    pinMode(in, INPUT);

    // initialize control over the keyboard:
    Keyboard.begin();

}


void loop()
{
    const int rd = digitalRead(in);
    if (rd == HIGH)
    {
        toggleKeyPress(' ');
        digitalWrite(out, HIGH);
        // wait for new window to open:
        delay(1000);   
    }
    else
    {
        digitalWrite(out, LOW);
    }
}
</code></pre>
