Controlling Arduino from Raspberry Pi

I am trying to control an Arduino Leonardo R3 to run some code when "triggered" from a Raspberry Pi, this would be in a loop so that it can be ran multiple times. I have setup the Raspberry Pi and tested it with an LED and the output is working fine.

I then connected this up to the Arduino and checked for a HIGH input from the RPi using an if statement.

void setup()
{
  pinMode(7,INPUT);
  
    
  if(digitalRead(7)==HIGH)
  {
  CODE IN HERE
  }
}

I found that this did not work as the if statement was always true because of pull up resistors not being used. I would greatly appreciate some guidance on this.

Post a circuit diagram and all your code.

 if(digitalRead(7)==HIGH)
  {
  CODE IN HERE
  }

this needs to be in void loop() yo.

The circuit for what I had tried was just connecting the Rpi GPIO to pin 7 of the Arduino. My code is basically as shown above but for a keyboard event (for saving a load of space, this is replaced with "Hello World"):

void setup()
{
 pinMode(2,INPUT);
 pinMode(7,INPUT);
 digitalWrite(2,HIGH);

 Keyboard.begin();

 while(digitalRead(2))
 {
 }

 if(digitalRead(7)==HIGH)
 {
 Keyboard.println("Hello World");
 }
}

Pin 2 is used as a manual trigger to check the code. When I tested this, I had set a LOW output from the RPi GPIO and when I connected pin 2 to ground on the Arduino (causing the program to execute) the mouse event still ran even though Pin 7 should have been low, I read that this was because of pull up resistors (same as what I have used on Pin 2) but I am not to sure how to set this up for Pin 7.

Also, the reason I did not put it inside the loop was because I was scared if I made a mistake the RPi may get stuck in an infinite loop so decided to test 1 run first in setup.

Also, if you're sending digital signals, shouldn't it be grounded to the arduino?

You can also have the PI communicate with the Arduino via Serial.

The code in setup gets run once as the Arduino is powered up, so it will miss any subsequent changes.

Put the test in loop() so it keeps checking.

Behind the scenes in the Arduino this is what gets run:

void main ()
{
  setup () ;
  while (1)
    loop () ;
}

(more or less). You just have to provide the setup() and loop(), they will be run like this.

Also, the reason I did not put it inside the loop was because I was scared if I made a mistake the RPi may get stuck in an infinite loop so decided to test 1 run first in setup.

That makes no sense - the Pi is not running loop()!!

Isn't the PI a 3.3v device? Will it be damaged if the Arduino puts 5v on an output pin?

The Arduino should be able to read 3.3v signals OK.

...R

I think I may be going the wrong way about this as there may be a simpler solution. The overall task is basically to control the Arduino Leo to type something when triggered by some Java code. I have controlled my RPi in the past through Java so thought I could use this as an input.

Is there any simpler solution to this? I was thinking maybe looking for a keyPress in a IF statement on the Arduino loop and activating this by making Java operate this key? I know your probably thinking "why make the arduino do it when Java can type" but this is part of a bigger project which would take a lot of writing to explain.

Thanks

sur4j: The overall task is basically to control the Arduino Leo to type something when triggered by some Java code.

Seems like that would require two lines of communcation between the Leonardo and the PI - the USB connection to send the keystrokes to the PI and something else to tell the Leonardo to send the keystrokes. ? ?

...R

seriously cereal

Robin2:
Isn’t the PI a 3.3v device? Will it be damaged if the Arduino puts 5v on an output pin?

The Arduino should be able to read 3.3v signals OK.

…R

I thought in that too.