Mazza: A certain amount of discouragement and fruatration is normal. There is a lot to learn, and as you say it is not always set out in the easiest way. Don't give up.
I took the time to go through your code and produce something closer to what you want. Let me offer some feedback about what I found while editing.
1. The construct 'if (x=HIGH)' should be 'if (x==HIGH)'. Note carefully the difference. The first one assigns HIGH to X. The second one tests whether x is equal to HIGH. If you use the first where you think you are using the second one you get an interesting debugging experience.
2. The construct 'if (x==HIGH');' should not have the semicolon after it. Semicolon is a statement. This construct says "if X is HIGH do nothing", and the following statement is executed without regard for the if. Another interesting debugging experience. Use if(x==HIGH) {stmt;stmt;stmt;} instead.
3. The code wasn't consistently indented to reflect the nesting structure. This will trip up your eyes when you try to find bugs. Adopt a consistent style, preferably by reading and imitating code from people you like. Fortune favors those who keep their code neat as a pin.
4. Thinking carefully about the algorithm pays off. Look how much simpler the loop() is below.
byte whichpin = 3; // set to 3 so turning it off is harmless
void setup() {
Serial.begin(9600);
randomSeed(analogRead(0));
pinMode(2,INPUT);
pinMode(3, OUTPUT);
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
pinMode(6, OUTPUT);
pinMode(7, OUTPUT);
pinMode(8, OUTPUT);
pinMode(9, OUTPUT);
pinMode(10, OUTPUT);
}
void loop() {
if (digitalRead(2)) {
digitalWrite(whichpin, LOW);
whichpin = random(3,10);
digitalWrite(whichpin, HIGH);
}
}
There is a lot of fun to be had here when things are working correctly, but a certain amount of frustration is part of the bargain. Stick with it, you'll figure it out.
-br