Go Down

Topic: 5V Arduino (Leonardo/Pro Micro) acting as a push button for a raspberry pi zero (Read 3303 times) previous topic - next topic

Grumpy_Mike

Quote
I only have experience in Java and some C++, as well as the combination of languages the Arduino IDE uses, which I would prefer to use. Is that a possibility?
While you can program the Pi in C it does not have all the pre built function definitions that sometimes make the Arduino look like another language. You could also use "Processing" which is Java like.

I do not use C on the Pi because the "market" is for Python code.

I wrote this in a style as close to your original Arduino code only in Python. Open up a command line box and type
gksudo idle
You will then get into the idle development environment where you can copy and paste this code:-
Note in Python there are no braces defining sections of code it is all done by indentation so this is crucial to get it right, and # is the comment symbol.
Code: [Select]

#!/usr/bin/env python
# Run this in supervisor mode

import time, os
import RPi.GPIO as GPIO

loPin = 4   # pins for triggering
hiPin = 17  # change to suite your setup

def main():
  init()
  print"Ctrl C to quit"
  count = 0
  time.sleep(0.3)           # let the other processes run a bit
  if not GPIO.input(loPin): # if (analogRead(A1) < 100) {
    time.sleep(5.0)         # delay(5000);
   
    if GPIO.input(hiPin):   # if (analogRead(A0) > 960) count++;
        count += 1
    else:
        count = 0
  else:
       count = 0;
 
  if count == 2 :           # trigger command on pi
      os.system(" insert your OS call here in quotes ")
 
  if count >= 3:
      count = 0;            # if it keeps hit state rerun the command


def init():
   GPIO.setmode(GPIO.BCM)    # Use the real GPIO pin numbers
   GPIO.setwarnings(False)
   GPIO.setup(hiPin,GPIO.IN) # pin mode call
   GPIO.setup(loPin,GPIO.IN) # pin mode call
   
# Main program logic:
if __name__ == '__main__':   
    main()


Phoenixxl

When all else fails stick a single relay on your arduino. /shrug

You won't have to worry about voltages then, just pullup resistors , pulldown resistors, internal pullup, adding 100 ohm to the data pin or not, mechanical contact bounce, millis overload proof loops and sacrificing chickens to papa legba.

It should all work in the end.

tinp

I wrote this in a style as close to your original Arduino code only in Python. Open up a command line box and type
gksudo idle
You will then get into the idle development environment where you can copy and paste this code:-
Note in Python there are no braces defining sections of code it is all done by indentation so this is crucial to get it right, and # is the comment symbol.
Wow, thank you for putting in the effort to write this code.
I can only ssh to my pi because the connection is too slow for vnc, so I just opened a cli text editor and pasted the code after editing it on my PC. Problem is, when I run the script it outputs "Ctrl C to quit" and then it ends. So I figure having the script run forever after starting it once is not a possibility in python? Should I have cron run it every 5 seconds then? Or is there a better solution?

Grumpy_Mike

Quote
So I figure having the script run forever after starting it once is not a possibility in python?
It is, something else must be wrong.

On my Pi this program runs forever. I have not checked the operation with the GPIO because I did not have access to them as it was bolted into a system that physically blocked GPIO access.
Dis you run it in supervisor / root mode? Put sudo in front of the run command. I used "idle" to launch it and loaded "ilde" with:-  "sudo idle"

tinp

I just added a loop by putting a
Code: [Select]
While True: in front. Now it works fine. It is run as root anyway because /etc/rc.local launches it on startup using "python /root/pcshutdown.py"

Thank you for your help, it works just as intended. It also motivated me to learn python which i really should have done by now ^^

Grumpy_Mike


Go Up