I just started using the Arduino IDE a day or two ago, I understand quite a lot already since I have some small experience with other programming languages. I was doing a small project where I have a potentiometer, three LED's (green, yellow and red) and a buzzer connected to my UNO. The idea was to write some code so that I would get more knowledgeable of the Arduino specific functions. I want the three LED's to light up but never at the same time using the potentiometer, making it random somehow I stumbled accross the random function. But when I reset the code the random number stays the same no matter how many times i reset it. Here is my code:
const int led_yellow = 5;
const int led_red = 3;
const int buzzer = 9;
byte chance = random(255);
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
pinMode(led_green, OUTPUT);
pinMode(led_yellow, OUTPUT);
pinMode(led_red, OUTPUT);
pinMode(buzzer, OUTPUT);
}
void loop() {
// put your main code here, to run repeatedly:
int data = analogRead(A0);
float voltage = data * (5.00 / 1023.00);
int percentage = map(data, 0, 1023, 0, 100);
Serial.print("Potentiometer at ");
Serial.print(voltage);
Serial.print("V or ");
Serial.print(percentage);
Serial.println("%");
Serial.print("The random number is: ");
Serial.println(chance);
Serial.println("------------------------------");
delay(250);
int brightness = map(data, 0, 1023, 0, 255);
int sound = map(data, 0, 1023, 0, 255);
if (chance >= 0 & chance <= 83) {
analogWrite(led_green, brightness);
}
else if (chance >= 84 & chance <= 167) {
analogWrite(led_yellow, brightness);
}
else {
analogWrite(led_red, brightness);
analogWrite(buzzer, sound);
}
}
Does anyone know a fix for this or if it is just a fault in the function?