Go Down

Topic: Project 11 first returned random value not random. Why? (Read 2111 times) previous topic - next topic

nass

Project 11 uses a tilt switch but the tiltswitch is broken because it didn't fit properly. My hunch is the copper arms got pushed in trying to make it fit so I've had to improvise. So instead I'm using a simple button instead in the same place, and the same code.

Works fine but erm it's actually not especially random. The first value generated by random() is always the highest value of the array. Is this because the button changes what happens or is this some flaw in random()? After the first value it's random but the first is never random with this sketch. Does this happen using the proper tilt switch as well?

Can anyone explain this?
Photography nut trying to make the flux capacitor of macro pho

nass

For what it's worth, I made a modification to the sketch that makes the thing properly random. Basically, instead of generating a random value just when switchState == LOW, I added reply = random(11); on every void loop. So it's just endlessly creating random reply values ad infinitum rather than just when the conditions are met. Seems to work, it'll be interesting to see if this is due to using a simple switch once I getb the tilt switch I ordered. All kind of weird.
Photography nut trying to make the flux capacitor of macro pho

nass

fyi, I just checked this using the proper parts and it still doesn't work properly. The first value I always get when switching it on is 'no', ie the highest value in the array. Sloppy that a badly written sketch is in the starter kit.
Photography nut trying to make the flux capacitor of macro pho

dean_1230

FWIW, I have the same problem with the tilt switch.  Not sure if the pins were pushed too far in or if they're just not long enough, but they don't like to stay engaged in the board.  They make contact with the metal in the breadboard, but it feels like it's spring loaded.  If I don't keep pressure on the sensor, the thing just pops out. 


Go Up