new project, controlling rgb lights and detecting light level

This commit is contained in:
Vikturix 2023-03-19 19:12:55 +00:00
parent 97fc15429f
commit 46d9a24815

View File

@ -0,0 +1,90 @@
//light measure
int pin_light = A5;
int light = 0;
//output pins
int red = 11;
int green = 10;
int blue = 9;
//imaginary battery
unsigned int bat_cap = 50000;
unsigned int bat = 0;
unsigned int ticks = 0;
unsigned int wait = 100;
double bat_percent;
//initialises pins and serial
void setup() {
// put your setup code here, to run once:
pinMode(red, OUTPUT);
pinMode(green, OUTPUT);
pinMode(blue, OUTPUT);
Serial.begin(9600);
}
//lame function to make life easier
void RGBout(int rV, int gV, int bV) {
analogWrite(red, rV);
analogWrite(green,gV);
analogWrite(blue,bV);
}
//nifty calculation (also lame function (also makes life easier))
double get_bat_percent() {
double temp = (double)bat/(double)bat_cap*100;
return temp;
}
//displays the charge of the battery to both outputs
void show_bat() {
bat_percent = get_bat_percent();
Serial.print(ticks);
Serial.print(" ms elapsed. Charge: ");
Serial.print(bat_percent);
Serial.println("%");
if (0 < bat_percent && bat_percent <= 20) {
RGBout(128,0,0);
//red
} else if (20 < bat_percent && bat_percent <= 40) {
RGBout(128,128,0);
//yellow
} else if (40 < bat_percent && bat_percent <= 60) {
RGBout(0,128,0);
//green
} else if (60 < bat_percent && bat_percent <= 80) {
RGBout(0,128,128);
//turquoise
} else if (80 < bat_percent && bat_percent <= 100) {
RGBout(0,0,128);
//white
}
}
//you know what this does
void loop() {
// put your main code here, to run repeatedly:
light = analogRead(pin_light);
bat += light;
ticks += wait;
if (bat >= bat_cap) {
Serial.print(ticks);
Serial.print(" ms. The system is fully charged.");
bat = bat_cap;
RGBout(128,128,128);
delay(120398435087134);
} else {
show_bat();
}
delay(wait);
}