# Arduino random problem

Hello,

We can help us!

We have an Arduino program that works in a second screen, but how do you link an numer to a digital port??? Thats the problem!!!

int but[8];
long randNumber;
int x0 = 2;
int x1 = 3;
int x2 = 4;
int x3 = 5;
int x4 = 6;
int x5 = 7;
int x6 = 8;
int x7 = 9;

void setup(){
pinMode(2, OUTPUT);
pinMode(3, OUTPUT);
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
pinMode(6, OUTPUT);
pinMode(7, OUTPUT);
pinMode(8, OUTPUT);
pinMode(9, OUTPUT);
Serial.begin(9600);

void loop(){
for(byte x=0, y=x-1; x<8; x++){
but=random(8);
while(but==but[x-1]||but==but[x-2]||but==but[x-3]||but==but[x-4]||but==but[x-5]||but==but[x-6]||but==but[x-7]){
but=random(8);}}
for(byte x=0; x<8; x++){

}
randNumber = random(500, 4000);
Serial.print(randNumber);
delay(randNumber);

Serial.print(",");
Serial.print(but);
Serial.print(",but= ");

switch(but){

case 0:
Serial.println(“0”);
digitalWrite(2, HIGH);
break;

case 1:
Serial.println(“1”);
break;
case 2:
Serial.println(“2”);
break;
case 3:
Serial.println(“3”);
break;
case 4:
Serial.println(“4”);
break;

}

Serial.println("----------------------------------");
delay(1000);

}

Welcome to the Forum. Please read these two posts:

How to use this forum - please read.
and
Read this before posting a programming question …
You may also find useful information that would answer your question here:
Useful links - check here for reference posts / tutorials

You have posted code without using code tags. The code tags make the code look

``````like this
``````

when posting source code files. It makes it easier to read, and can be copied with a single mouse click. Also, if you don’t do it, some of the character sequences in the code can be misinterpred by the forum code as italics or funny emoticons. The “Code: [Select]” feature allows someone to select the entire sketch so it can be easily copied and pasted into the IDE for testing.
If you have already posted without using code tags, open your message and select “modify” from the pull down menu labelled, “More”, at the lower left corner of the message. Highlight your code by selecting it (it turns blue), and then click on the “</>” icon at the upper left hand corner. Click on the “Save” button. Code tags can also be inserted manually in the forum text using the code and /code metatags.

Unless the sketch is too large, it’s better if you post your code, rather than attach it. When it’s attached, we have to download it, create a folder then open your code in our IDE. And afterwards, the folder remains unless we navigate to the “Temp” folder and manually remove it. It’s much easier to just view the code in your post.

Many questions can be answered by reading the documentation which is provided with the IDE, available under the help tab, or online here.

There are many other things that programmers do to make their code understandable. Please do them, as a courtesy to the members who volunteer their time to help you here. One is to provide the entire code, not just a snippet. Use a standard indentation to clearly show the code blocks. Never put more than one statement per line. Place any brackets by themselves on a separate line. Use blank lines sparingly, no more than one at a time. Before posting the code, use Ctrl-T in the IDE to reformat the code in a standard format, which makes it easier for us to read. Another is to give things descriptive names. Use descriptive variable names, for example “temperature” instead of “t”. You can name numerical constants, pin numbers, variables and many other things in this way. For example, you can refer to a pin and an output level by number, like `digitalWrite(3,0)`. But such a statement doesn’t reveal anything about the purpose. `digitalWrite(hornRelayPin, LOW)` does. You can do that by declaring `const byte hornRelayPin = 3;` before setup() in your program. Many such names are already defined for you by the compiler and the Arduino IDE. Here are some:

``````#define HIGH 0x1
#define LOW  0x0
#define PI 3.1415926535897932384626433832795
``````

Use them. There are many more. Use compiler math to compute values so you can see where they came from (or at least document them). For example, if you see the number 73, you would be hard put to explain the significance of it. But if you see “daysPerYear/5”, it is obvious. One more thing. When you work on program continuously, you become familiar with it. So many things seem obvious even if they are not spelled out explicitly. But try looking at your own code six months later. It will be as if a stranger wrote it. So write for strangers, not yourself.