Need help by generating a random number in an while loop for Arduino | Arduino | Forum

The ThingSpeak community site has been upgraded to a new site. This site is currently in read-only mode. You can ask questions or post and read discussions on the new site.

Please consider registering

sp_LogInOut Log In sp_Registration Register

Register | Lost password?
Advanced Search

— Forum Scope —

— Match —

— Forum Options —

Minimum search word length is 3 characters - maximum search word length is 84 characters

sp_Feed sp_TopicIcon
Need help by generating a random number in an while loop for Arduino
No permission to create posts
August 28, 2019
1:01 pm
New Member
Forum Posts: 1
Member Since:
August 28, 2019
sp_UserOfflineSmall Offline

the program should generate a random color and stay that way. I know that it is not that hard but i really want to know why this isn't working.

I don't know why, but when i set a fix number like 100 the connected LED is bright. But when i try it with random numbers it is dark every time i restart the Arduino.

her is the code.

int mode;
const int led_red = 9;
const int led_green = 10;
const int led_blue = 11;
void setup() {
// put your setup code here, to run once:
mode = 0;

void loop() {
// put your main code here, to run repeatedly:
int random_red;
int random_green;
int random_blue;
random_red = random(256); //Here is the problem. by a fix number like 100 it works but not a random number
random_green = random(256);
random_blue = random(255);
mode = 1;
analogWrite(led_red, random_red);
analogWrite(led_green, random_green);
analogWrite(led_blue, random_blue);

thx for your help.

August 29, 2019
8:08 am

Forum Posts: 885
Member Since:
March 7, 2017
sp_UserOfflineSmall Offline

Can you consider posting this to an Arduino forum? It doesn't really seem ThingSpeak related.
also have a look at
(try random(50,200))?

Forum Timezone: America/New_York

Most Users Ever Online: 166

Currently Online:
27 Guest(s)

Currently Browsing this Page:
1 Guest(s)

Top Posters:

rw950431: 272

Vinod: 240

piajola: 95

turgo: 70

vespapierre: 63

Adarsh_Murthy: 62

Member Stats:

Guest Posters: 1

Members: 8665

Moderators: 1

Admins: 2

Forum Stats:

Groups: 3

Forums: 14

Topics: 1600

Posts: 5760

Newest Members:

estebanmateo93, mikewrite, onlinebusiness, winzy99, Zark_zeugan, StefanWi

Moderators: cstapels: 460

Administrators: Hans: 405, lee: 457