XRAD'S randomSeed teensy 4.1 question..RGBW screen saver

XRAD

Well-known member
I know there is an entropy lib, but I want to use randomSeed() as the necessary randomness is minimal. The basic arduino sketch reading teensy A2 does generate different numbers on boot. But when I apply the arduino sketch to my screen saver, the randomness goes away (and the boot sequence repeats).....It's probably something simple, but I have spent a few hours trying different things and no success....

thx for any pointers....

Arduino sketch off the arduino site, works...:
Code:
long randNumber;

void setup() {
  Serial.begin(9600);
  randomSeed(analogRead(A2));
}

void loop() {
  randNumber = random(1,300);
  Serial.println(randNumber);
  delay(50);
}

my sketch applying randomSeed(), repeats...:
Code:
/*
  Created by Tom Igoe December 2012
  Modified 15 April 2013 by Scott Fitzgerald
  Modified by XRAD 4/28/2022
*/

#include "Adafruit_GFX.h"
#include "Adafruit_HX8357.h"

// These are my teensy connections, along w/native SPI hardware
#define TFT_CS 2
#define TFT_DC 3
#define TFT_RST 5

#define width 480
#define height 320

Adafruit_HX8357 tft = Adafruit_HX8357(TFT_CS, TFT_DC, TFT_RST);//3.5" tft touch

int ballDirectionX = 1;
int ballDirectionY = 1;

int ballSpeed = 10; // lower numbers are faster
int randomSpeed;

int ballX , ballY, oldBallX, oldBallY;

uint32_t colour[] = {HX8357_RED, HX8357_GREEN, HX8357_BLUE, HX8357_WHITE};

int color = 0;

void setup() {
  Serial.begin(9600);
  randomSeed(analogRead(A2));
  tft.begin();
  tft.setRotation(3);
  tft.fillScreen(HX8357_BLACK);
  randomSpeed = random(1,10);//need a speed > 0 to begin motion
  Serial.println(randomSpeed);
}

void loop() {
  if (millis() % ballSpeed < randomSpeed) {
    moveBall();
  }
}

void moveBall() {
  if (ballX > width || ballX < 0) {
    color = color + 1;
    if (color > 3) {
      color = 0;
    }
    ballDirectionX = -ballDirectionX;
    randomSpeed = random(1,10);
    Serial.println(randomSpeed);
  }

  if (ballY > height || ballY < 0) {
    color = color + 1;
    if (color > 3) {
      color = 0;
    }
    ballDirectionY = -ballDirectionY;
    randomSpeed = random(1,10);
    Serial.println(randomSpeed);
  }

  // update the ball's position
  ballX += ballDirectionX;
  ballY += ballDirectionY;

  if (oldBallX != ballX || oldBallY != ballY) {
    tft.drawCircle(oldBallX, oldBallY, 25, HX8357_BLACK);
  }

  tft.drawCircle(ballX, ballY, 25, colour[color]);

  oldBallX = ballX;
  oldBallY = ballY;
}
 
nevermind, I (EDIT: DID NOT) figured it out. It was the line of code containing the modulo operator (0-9). I eliminated this and randomSeed() works fine now.... :)

randomSeed still not 'random".... :(
 
nevermind, I (EDIT: DID NOT) figured it out. It was the line of code containing the modulo operator (0-9). I eliminated this and randomSeed() works fine now.... :)

randomSeed still not 'random".... :(

You could make it more random by feeding the seed from a counter.
How about using something with millis().
possibly (uint32_t) millis()
 
TY for reply, but why does randomSeed 'work' in the basic program, but not with my screen saver....That is my confusion...
 
Back
Top