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

Avatar

Please consider registering
Guest

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
Avatar
qwer12129
New Member
Members
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:
pinMode(led_red,OUTPUT);
pinMode(led_green,OUTPUT);
pinMode(led_blue,OUTPUT);
randomSeed(analogRead(A0));
mode = 0;
}

void loop() {
// put your main code here, to run repeatedly:
int random_red;
int random_green;
int random_blue;
while(mode=0){
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;
}
while(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
Avatar
cstapels
Moderator
Members


Moderators
Forum Posts: 771
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
https://www.arduino.cc/reference/en/language/functions/random-numbers/random/
(try random(50,200))?

Forum Timezone: America/New_York

Most Users Ever Online: 166

Currently Online:
18 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:

Hema, askask, Rakwireless, budiardi basuki, oliver009, shelby1234

Moderators: cstapels: 460

Administrators: Hans: 405, lee: 457