Hi I'm new to the forum so I'm sorry if this is not the right place to ask the question but;
I have some code I'm writing which I want to use to inflate and deflate air rams under a platform to create the feeling of a ship rolling on the sea, the interface is from a tablet, over WiFi using Node Red on a Raspberry Pi to update the perimeters via mqtt - so far so good.
I want to include a random factor to the timing, so it doesn't have too mechanical a feel to the motion - but I am having trouble generating random numbers.
I tried the Arduino random() function, but that returned only very low numbers, so I installed the ESP8266TrueRandom library but have the same issue.
Using ESP8266TrueRandom.random(10000) I rarely get a number generated above 250, and most of the results are sub 100. I have searched online but can't find any information, is anyone able to shed some light on why this is happening / how to fix it?
Here is the part of the code where I'm generating and using the random number:
currentMillis = millis(); //syncs the currentMillis value to the current time
duration = ((durationRAW * (platformspeed/100))); //Sets the duration value to a percentage of platform speed
RandFactor = ESP8266TrueRandom.random(RandomRAW); //Creates a random number upto the random amount set via mqtt
// Alternates between the two halves of the cycle and turns on rams 1 and 2. Timing includes a random value
if (currentMillis - FlipTime >= platformspeed + RandFactor)
{
{ if (flipflop == HIGH)
{
{
flipflop = LOW;
Ram1OnTime = currentMillis;
Ram1State = HIGH;
Serial.print(RandFactor);
Serial.println();
}
}
else
{
{
flipflop = HIGH;
Ram2OnTime = currentMillis;
Ram2State = HIGH;
}
}
FlipTime = currentMillis;
}
}
// Write the values to the pins
digitalWrite(ram1, Ram1State);
digitalWrite(ram2, Ram2State);
Everything else works perfectly, the sequence and mqtt communication is doing what I want - I just can't get the randomisation part to work.
Thanks in advance for any help or insight with this problem.