본문 바로가기
임베디드/Arduino

RGB LED 랜덤함수

by sj0020 2020. 9. 18.


   pinMode(2, OUTPUT); 

아날로그에 연결 했을때는 핀모드 입력 해주나 안해주나 똑같이 동작한다

 

int BLUE = 11;
int GREEN = 10;
int RED = 9;
int i;
long randNumber;
void setup() {
  //pinMode(BLUE, HIGH);//BLUE
  //pinMode(GREEN, HIGH);//GREEN
  //pinMode(RED, HIGH);//RED
  randomSeed(analogRead(0));
  Serial.begin(9600);
  
}

void loop() {
  i = random(1,256);
  Serial.println(i);
  analogWrite(BLUE, i);
  i = random(1,256);
  Serial.println(i);
  analogWrite(GREEN, i);
  i = random(1,256);
  Serial.println(i);
  analogWrite( RED, i);
  delay(30);
}

 


랜덤함수

 

Syntax

random(max)
random(min, max)

Parameters

min: lower bound of the random value, inclusive (optional).
max: upper bound of the random value, exclusive.

Returns

A random number between min and max-1. Data type: long.

long randNumber;

void setup() {
  Serial.begin(9600);

  // if analog input pin 0 is unconnected, random analog
  // noise will cause the call to randomSeed() to generate
  // different seed numbers each time the sketch runs.
  // randomSeed() will then shuffle the random function.
  randomSeed(analogRead(0));
}

void loop() {
  // print a random number from 0 to 299
  randNumber = random(300);
  Serial.println(randNumber);

  // print a random number from 10 to 19
  randNumber = random(10, 20);
  Serial.println(randNumber);

  delay(50);
}

https://www.arduino.cc/reference/en/language/functions/random-numbers/random/

 

random() - Arduino Reference

Example Code The code generates random numbers and displays them. long randNumber; void setup() { Serial.begin(9600); // if analog input pin 0 is unconnected, random analog // noise will cause the call to randomSeed() to generate // different seed numbers

www.arduino.cc

 

'임베디드 > Arduino' 카테고리의 다른 글

dht22  (0) 2020.10.21
analogWrite() 함수  (0) 2020.09.18
JMOD-BT-01 블루투스 모듈 / dht11 / MIT App Inventor  (0) 2020.09.16
blynk dht11  (0) 2020.09.16
esp8266 blynk  (0) 2020.09.16