 # Logic question...

Hi Guys,

I came across this stupid logic problem.
Maybe it is not a big deal, yet, it works in a software environment and it is not in the Arduino.

The code snippet :

``````int comparator = 0;
comparator = random(100);
int density = map(analogRead(0), 0, 1023, 0, 100);
if (comparator < density) {
"do something"
``````

The little Nano should generate a random number between 0 and 100 and should compare it to the value read from A0.
In theory, if A0 has 5V, so, 1023 INT, mapped to a 100, it should “do something” every single time.
Am I right?
It works in Max MSP, I do not know what the problem is here…

Please post a complete sketch so that the problem can be seen in context. for instance, have you by any chance got a global variable named density as well as the local one ?

The little Nano should generate a random number between 0 and 100

Actually no it won't. The random number will be between 0 and 99

Have you tried printing the values being compared just before the comparison ? Are they what you expect ?

Full code :

``````const int clock_in = 2;
const int per1 = 13;
const int tock = 3;

const int per2 = 4;
const int per3 = 5;
const int per4 = 6;
const int per6 = 7;
const int per8 = 8;
const int per12 = 9;
const int per16 = 10;
const int per24 = 11;

const int trigger_length = 7;

int count1 = 0;
int state = 0;
int last_state = 0;

void setup() {

pinMode(clock_in, INPUT);
pinMode(tock, OUTPUT);
pinMode(per1, OUTPUT);
pinMode(per2, OUTPUT);
pinMode(per3, OUTPUT);
pinMode(per4, OUTPUT);
pinMode(per6, OUTPUT);
pinMode(per8, OUTPUT);
pinMode(per12, OUTPUT);
pinMode(per16, OUTPUT);
pinMode(per24, OUTPUT);

Serial.begin(9600);
}

void loop() {
if (state != last_state) {
if (state == HIGH) {
int comparator = 0;
comparator = random(90);
int density = map(analogRead(0), 0, 1023, 0, 100);
if (comparator < density) {
digitalWrite(tock, HIGH);
delay(trigger_length);
digitalWrite(tock, LOW);
} else {
digitalWrite(tock, LOW);}
{
digitalWrite(per1, HIGH);
delay(trigger_length);
digitalWrite(per1, LOW);
count1++;
if (count1 > 48) {
count1 = 1;
} else { count1 = count1; }

if((count1==2) || (count1==4) || (count1==6) || (count1==8) || (count1==10) || (count1==12) || (count1==14) || (count1==16) || (count1==18) || (count1==20) || (count1==22) || (count1==24) || (count1==26) || (count1==28) || (count1==30) || (count1==32) || (count1==34) || (count1==36) || (count1==38) || (count1==40) || (count1==42) || (count1==44) || (count1==46) || (count1==48))
{
digitalWrite(per2, HIGH); delay(trigger_length); digitalWrite(per2, LOW);
} else {
digitalWrite(per2, LOW);
}
if((count1==3) || (count1==6) || (count1==9) || (count1==12) || (count1==15) || (count1==18) || (count1==21) || (count1==24) || (count1==27) || (count1==30) || (count1==33) || (count1==36) || (count1==39) || (count1==42) || (count1==45) || (count1==48))
{
digitalWrite(per3, HIGH); delay(trigger_length); digitalWrite(per3, LOW);
} else {
digitalWrite(per3, LOW);
}
if((count1==4) || (count1==8) || (count1==12) || (count1==16) || (count1==20) || (count1==24) || (count1==28) || (count1==32) || (count1==36) || (count1==40) || (count1==44) || (count1==48))
{
digitalWrite(per4, HIGH); delay(trigger_length); digitalWrite(per4, LOW);
} else {
digitalWrite(per4, LOW);
}
if((count1==6) || (count1==12) || (count1==18) || (count1==24) || (count1==30) || (count1==36) || (count1==42) || (count1==48))
{
digitalWrite(per6, HIGH); delay(trigger_length); digitalWrite(per6, LOW);
} else {
digitalWrite(per6, LOW);
}
if((count1==8) || (count1==16) || (count1==24) || (count1==32) || (count1==40) || (count1==48))
{
digitalWrite(per8, HIGH); delay(trigger_length); digitalWrite(per8, LOW);
} else {
digitalWrite(per8, LOW);
}
if((count1==12) || (count1==24) || (count1==36) || (count1==48))
{
digitalWrite(per12, HIGH); delay(trigger_length); digitalWrite(per12, LOW);
} else {
digitalWrite(per12, LOW);
}
if((count1==16) || (count1==32) || (count1==48))
{
digitalWrite(per16, HIGH); delay(trigger_length); digitalWrite(per16, LOW);
} else {
digitalWrite(per16, LOW);
}
if((count1==24) || (count1==48))
{
digitalWrite(per24, HIGH); delay(trigger_length); digitalWrite(per24, LOW);
} else {
digitalWrite(per24, LOW);
}
}
delay(1);
}
last_state = state;
}
}
``````

Have you tried printing the values being compared just before the comparison ? Are they what you expect ?

Are you sure you intended a close brace on the end of this line?

``````digitalWrite(tock, LOW);}
``````

If so, then why the pointless { } code block that follows it?

``````       {
digitalWrite(per1, HIGH);
delay(trigger_length);
....
}
``````

You’d do well to stick to a consistent and standard brace style. Either open braces always on the end of their respective lines...

``````if (....) {
....
} else {
....
}
``````

Or open and close braces on lines by themselves...

``````if (....)
{
....
}
else
{
....
}
``````

When you chop and change styles it makes your code very hard (at least for others) to read.

The outputs are 7 ms long "triggers" for modular synths, that is the reason of the digital write LOW lines.
I'll print the values asap.

Also, thank you very for the comments on the style, I'll fix them as well!

random(100) generates numbers ranging from 0 to 99.
The map function you use will map the analog reading to values ranging from 0 to 100.

jjhdnd:
random(100) generates numbers ranging from 0 to 99.
The map function you use will map the analog reading to values ranging from 0 to 100.

That's right and using the correct scaling factor will fix that.

``````int density = map(analogRead(0), 0, 1024, 0, 100);
``````