USA, FL
Offline
God Member
Karma: 11
Posts: 577
A life? Where can I download one of those?
|
 |
« on: September 27, 2012, 04:41:20 pm » |
I would like to pick a random pin, but with the pins named. Does anybody know how to do this? int ThisPin = 2; int ThatPin = 3; int AnotherPin = 4; int FinalPin = 5; I know you can get random numbers. But I would like to get random pin names. This is how I would like to use them: PinMode(RandomPin,OUTPUT); delay(20) PinMode(RandomPin,INPUT); More information is located here: http://arduino.cc/forum/index.php/topic,123841.msg930983.htmlLast reply is where I have to use PinMode for the triggering. Thanks, Link
|
|
|
|
|
Logged
|
//LiNK
|
|
|
|
UK
Offline
Tesla Member
Karma: 89
Posts: 6313
-
|
 |
« Reply #1 on: September 27, 2012, 04:44:50 pm » |
Don't use variable names; stick your pin numbers in an array and use your random number as an index into the array.
|
|
|
|
|
Logged
|
|
|
|
|
USA, FL
Offline
God Member
Karma: 11
Posts: 577
A life? Where can I download one of those?
|
 |
« Reply #2 on: September 27, 2012, 05:04:37 pm » |
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?
|
|
|
|
|
Logged
|
//LiNK
|
|
|
|
Leeds, UK
Offline
God Member
Karma: 35
Posts: 982
Once the magic blue smoke is released, it won't go back in!
|
 |
« Reply #3 on: September 27, 2012, 05:10:03 pm » |
If you use an array, you can still use the pins in areas where they are not random. The array doesn't change, hence the const keyword. Instead you pick a random element from the array. const byte pins[] = {2,3,4,5};
void setup(){ ... }
void loop(){ ... byte randomNumber = ... ; pinMode(pins[randomNumber],OUTPUT); //do something with a random pin delay(20) pinMode(pins[randomNumber],INPUT); ...
... digitalWrite(pins[0],HIGH); //Just an example. Element 0 is always element 0, so there is no random factor here... }
|
|
|
|
|
Logged
|
~Tom~
|
|
|
|
USA, FL
Offline
God Member
Karma: 11
Posts: 577
A life? Where can I download one of those?
|
 |
« Reply #4 on: September 27, 2012, 05:13:18 pm » |
Aha... Thanks Tom and Peter.
|
|
|
|
|
Logged
|
//LiNK
|
|
|
|
Left Coast, CA (USA)
Offline
Brattain Member
Karma: 279
Posts: 15312
Measurement changes behavior
|
 |
« Reply #5 on: September 27, 2012, 05:13:34 pm » |
How about 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
|
|
|
|
|
Logged
|
|
|
|
|
USA, FL
Offline
God Member
Karma: 11
Posts: 577
A life? Where can I download one of those?
|
 |
« Reply #6 on: September 27, 2012, 05:17:42 pm » |
How about 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.
|
|
|
|
|
Logged
|
//LiNK
|
|
|
|
UK
Offline
Tesla Member
Karma: 89
Posts: 6313
-
|
 |
« Reply #7 on: September 27, 2012, 05:23:05 pm » |
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].
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Edison Member
Karma: 15
Posts: 1002
Arduino rocks
|
 |
« Reply #8 on: September 27, 2012, 05:58:53 pm » |
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);
|
|
|
|
|
Logged
|
|
|
|
|
USA, FL
Offline
God Member
Karma: 11
Posts: 577
A life? Where can I download one of those?
|
 |
« Reply #9 on: September 27, 2012, 07:15:13 pm » |
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 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.
|
|
|
|
|
Logged
|
//LiNK
|
|
|
|
UK
Offline
Tesla Member
Karma: 89
Posts: 6313
-
|
 |
« Reply #10 on: September 28, 2012, 10:33:24 am » |
So I don't think
...
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.
So set it back to an input afterwards, if that's what it needs.
|
|
|
|
|
Logged
|
|
|
|
|
USA, FL
Offline
God Member
Karma: 11
Posts: 577
A life? Where can I download one of those?
|
 |
« Reply #11 on: September 28, 2012, 11:29:22 am » |
If I use "RandPin" to set it back to INPUT, how is it going to know what the first RandPin was?
|
|
|
|
|
Logged
|
//LiNK
|
|
|
|
Leeds, UK
Offline
God Member
Karma: 35
Posts: 982
Once the magic blue smoke is released, it won't go back in!
|
 |
« Reply #12 on: September 28, 2012, 12:06:36 pm » |
You just have to set it up so randpin doesn't change between becoming an output and an input. int randpin = pins[random(4)];
pinMode(randpin, OUTPUT); delay(20); PinMode(randpin,INPUT);
|
|
|
|
|
Logged
|
~Tom~
|
|
|
|
USA, FL
Offline
God Member
Karma: 11
Posts: 577
A life? Where can I download one of those?
|
 |
« Reply #13 on: September 28, 2012, 02:02:14 pm » |
Ok, so that is what that does! Thanks Tom.
|
|
|
|
|
Logged
|
//LiNK
|
|
|
|
|