Go Down

Topic: Trigger Random Pin Names (Read 1 time) previous topic - next topic

retrolefty

How about


Code: [Select]
RandomPin = random(2, 14);  // assuming you don't want to step on serial comm
PinMode(RandomPin,OUTPUT);
delay(20)
RandomPin = random(2, 14);  // assuming you don't want to step on serial comm
PinMode(RandomPin,INPUT);



Lefty

codlink


How about

Code: [Select]
RandomPin = random(2, 14);  // assuming you don't want to step on serial comm
PinMode(RandomPin,OUTPUT);
delay(20)
RandomPin = random(2, 14);  // assuming you don't want to step on serial comm
PinMode(RandomPin,INPUT);


Lefty


That was the way I was going to do it, but really wanted to keep the names of the pins so I would know what the trigger is.  This sketch is for a robot that will talk back to you.
//LiNK

PeterH


I didn't really want to do it that way as I am going to use those same pins on other triggers, but they will not be random...

Is this the only way though?


If you want to use named access to those pins too, you can either hold the pin numbers in ordinary named variables too, or defined an enumerated type that assigned named values to the array indices. So instead of putting thisPin and thatPin you'd use pins[ThisOne] or pins[ThatOne].
I only provide help via the forum - please do not contact me for private consultancy.

WizenedEE

Code: [Select]

enum pins_t  {ThisPin, ThatPin, AnotherPin, FinalPin};
int pins[] = {  2,       3,       4,          5};

int randpin = pins[random(4)];

pinMode(randpin, OUTPUT);
digitalWrite(pins[ThisPin], HIGH);
digitalWrite(pins[FinalPin], LOW);

codlink

hmm..  I may not be able to randomize this as I want to.

The way the MP3 Trigger works in "real life,"  I have to use pinMode(ThisPin,OUTPUT); to turn the pin to ground to trigger that pin on the MP3 Trigger.  Since the MP3 Trigger will not simply rely on digitalWrite(ThisPin,LOW);/digitalWrite(ThisPin,HIGH); since it will not trigger another pin if one pin is HIGH (it supposed to but doesn't).  

So I have to use pinMode(ThisPin,OUPUT); to trigger and pinMode(ThisPin,INPUT); so I can trigger other pins on the MP3 Trigger.

So I don't think

Code: [Select]
enum pins_t  {ThisPin, ThatPin, AnotherPin, FinalPin};
int pins[] = {  2,       3,       4,          5};

int randpin = pins[random(4)];

pinMode(randpin, OUTPUT);
digitalWrite(pins[ThisPin], HIGH);
digitalWrite(pins[FinalPin], LOW);


will work if that randompin gets set to OUTPUT then that same pin (after the delay) needs to get set back to INPUT so it will not loop the sound from the MP3 Trigger.
//LiNK

Go Up