From 46d9a24815e26d78066042c3578e06af18760ece Mon Sep 17 00:00:00 2001 From: Vikturix Date: Sun, 19 Mar 2023 19:12:55 +0000 Subject: [PATCH] new project, controlling rgb lights and detecting light level --- battery_rgb/battery_rgb.ino | 90 +++++++++++++++++++++++++++++++++++++ 1 file changed, 90 insertions(+) create mode 100644 battery_rgb/battery_rgb.ino diff --git a/battery_rgb/battery_rgb.ino b/battery_rgb/battery_rgb.ino new file mode 100644 index 0000000..f8453d7 --- /dev/null +++ b/battery_rgb/battery_rgb.ino @@ -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); +} \ No newline at end of file