Code translation help

Hello,

I wrote some code for a Raspberry Pi a while back and it works exactly how it is mean to, i have now decided to actually get the project up and running and have realised an Arduino is less over kill than the Pi would be. I havent written in C for a very long time (uni .... about 12 years ago and only dabbled in coding in general) what i am wandering is, are there equivelent libraries to import. I have had a look but cant find anything that will make the random.choice work properly. I have put the python code at the bottom, but a bit of pointing in the right direction would be amazing.

import RPi.GPIO as GPIO
import time
import random

def main():

    GPIO.setmode(GPIO.BCM)

    pinlist = [17,27,22,10,9,13,5,6]

    GPIO.setup(pinlist, GPIO.OUT)
    GPIO.output(pinlist, GPIO.LOW)

    SleepA = 0.2
    SleepB = 0.5

    def Rounds ():
        print ("Rounds")
# code turning relays on and off in sequence

    def Queens ():
        print ("Queens")
# code turning relays on and off in sequence

    def Kings ():
        print ("Kings")
# code turning relays on and off in sequence

    def Tittums ():
        print ("Tittums")
# code turning relays on and off in sequence


    Tune = [Rounds,Queens,Kings,Tittums]
    count = 0

    while count < 2:
        random.choice(Tune)()
        time.sleep(SleepB)
        count +=1

        if count >= 2:
            GPIO.output(pinlist, GPIO.LOW)
            print ("complete")

GPIO.setmode (GPIO.BCM)
GPIO.setup (26, GPIO.IN, pull_up_down=GPIO.PUD_UP)

while True:
    input_state = GPIO.input (26)
    if input_state == False:
        print ("Button Press")
        main()

My Python isn't great, but is main recursive?

...yes?

once the programe finishes it will skip back to the top.

The project is a doorbell, when the button is pressed a random two of the four 'tunes' will play via solenoids hitting bells controlled by relays. if that helps?

Scrap the code you have. There's nothing about the application you described that requires an recursive algorithm. Nor is it very long. Just write it from scratch in the "Arduino way".

This should be pretty close... You will have to adjust your pin numbers depending on which Arduino board you have and how to have things wired up. I also don't know what GPIO.setmode (GPIO.BCM) is supposed to do so it is just commented out

//import RPi.GPIO as GPIO
//import time
//import random

const byte pinlist[] = {17, 27, 22, 10, 9, 13, 5, 6};
const byte pinCount = sizeof(pinlist) / sizeof(pinlist[0]);

const unsigned long SleepA = 200; // msec
const unsigned long SleepB = 500;

void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);

  //    GPIO.setmode(GPIO.BCM)
  // ?????

  for (int i = 0; i < pinCount; ++i ) {
    pinMode( pinlist[i], OUTPUT );
    digitalWrite( pinlist[i], LOW );
  }

  //GPIO.setup (26, GPIO.IN, pull_up_down = GPIO.PUD_UP)
  pinMode( 26, INPUT_PULLUP );
}

void Rounds() {
  Serial.println("Rounds");
  //# code turning relays on and off in sequence
}

void Queens() {
  Serial.println("Queens");
  //# code turning relays on and off in sequence
}

void Kings() {
  Serial.println("Kings");
  //# code turning relays on and off in sequence
}

void Tittums() {
  Serial.println("Tittums");
  //# code turning relays on and off in sequence
}

void (*Tune[])() = {Rounds, Queens, Kings, Tittums};
const byte tuneCount = sizeof(Tune) / sizeof(Tune[0]);


void loop() {
  // put your code here to run repeatedly
  
  for ( int count = 0; count < 2; count++ ) {
    int choice = random(tuneCount);
    (*Tune[choice])();
    delay(SleepB);
  }
  for (int i = 0; i < pinCount; ++i) {
    digitalWrite(pinlist[i], LOW);
  }
  Serial.println("complete");

  //GPIO.setmode (GPIO.BCM)
  // ???

  while (digitalRead(26) == HIGH ) {
    // wait for button press
  }
  Serial.println("Button Press");
}

Jellyboo:
...yes?

Is unconstrained recursion common in Python?

blh64 - Thank you! This is a great start and so much closer than i have been in days to getting anywhere close! The GPIO.BCM is just telling the Pi which set of pin numbers you want to use, either Board (1-40) or BCM (how the Pi knows them)

if you are really interested :slight_smile:

TheMemberFormerlyKnownAsAWOL - Its just an easy way of resetting the program. the button press runs the code, the program then resets ready to run again when the button is pressed.

Thank you everyone for your help, this will make tomorrows furloughed day slightly less frustrating!

Don't rely on recursion to get you out of situations, when simpler and less ass-biting solutions are available.