/* ============================================
code is placed under the MIT license
Copyright (c) 2024 J-M-L
For the Arduino Forum : https://forum.arduino.cc/u/j-m-l
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
===============================================
*/
#include <FastLED.h>
constexpr uint8_t numLeds = 118;
constexpr uint8_t dataPin = 3;
CRGB leds[numLeds];
// value in the enum corresponds to the first led index in the leds array
enum PeriodicElement : uint8_t {
Hydrogen = 0, Helium = 1,
Lithium = 2, Beryllium = 3, Boron = 4, Carbon = 5, Nitrogen = 6, Oxygen = 7, Fluorine = 8, Neon = 9,
Sodium = 10, Magnesium = 11, Aluminum = 12, Silicon = 13, Phosphorus = 14, Sulfur = 15, Chlorine = 16, Argon = 17,
Potassium = 18, Calcium = 19, Scandium = 20, Titanium = 21, Vanadium = 22, Chromium = 23, Manganese = 24, Iron = 25, Cobalt = 26, Nickel = 27, Copper = 28, Zinc = 29, Gallium = 30, Germanium = 31, Arsenic = 32, Selenium = 33, Bromine = 34, Krypton = 35,
Rubidium = 36, Strontium = 37, Yttrium = 38, Zirconium = 39, Niobium = 40, Molybdenum = 41, Technetium = 42, Ruthenium = 43, Rhodium = 44, Palladium = 45, Silver = 46, Cadmium = 47, Indium = 48, Tin = 49, Antimony = 50, Tellurium = 51, Iodine = 52, Xenon = 53,
Cesium = 54, Barium = 55, Lanthanum = 56, Hafnium = 57, Tantalum = 58, Tungsten = 59, Rhenium = 60, Osmium = 61, Iridium = 62, Platinum = 63, Gold = 64, Mercury = 65, Thallium = 66, Lead = 67, Bismuth = 68, Polonium = 69, Astatine = 70, Radon = 71,
Francium = 72, Radium = 73, Actinium = 74, Rutherfordium = 75, Dubnium = 76, Seaborgium = 77, Bohrium = 78, Hassium = 79, Meitnerium = 80, Darmstadtium = 81, Roentgenium = 82, Copernicium = 83, Nihonium = 84, Flerovium = 85, Moscovium = 86, Livermorium = 87, Tennessine = 88, Oganesson = 89,
Cerium = 90, Praseodymium = 91, Neodymium = 92, Promethium = 93, Samarium = 94, Europium = 95, Gadolinium = 96, Terbium = 97, Dysprosium = 98, Holmium = 99, Erbium = 100, Thulium = 101, Ytterbium = 102, Lutetium = 103,
Thorium = 104, Protactinium = 105, Uranium = 106, Neptunium = 107, Plutonium = 108, Americium = 109, Curium = 110, Berkelium = 111, Californium = 112, Einsteinium = 113, Fermium = 114, Mendelevium = 115, Nobelium = 116, Lawrencium = 117
};
const uint8_t ledCntPerElement = 1; // how many leds to light up starting from the index of an element
const uint8_t maxElementsInGroups = 20; // Max 20 elements in a group
struct PeriodicGroup {
const char * name; // Name of the group
CRGB color; // Color for the group
uint8_t count; // Count of elements in the group
PeriodicElement elements[maxElementsInGroups]; // Elements in the group mapping to the first pixel position
};
PeriodicGroup groups[] = {
{ "Alkali Metals", CRGB::LightBlue, 6, { Lithium, Sodium, Potassium, Rubidium, Cesium, Francium } },
{ "Alkaline Earth Metals", CRGB::LightGreen, 6, { Beryllium, Magnesium, Calcium, Strontium, Barium, Radium } },
{ "Transition Metals", CRGB::Gold, 20, { Scandium, Titanium, Vanadium, Chromium, Manganese, Iron, Cobalt, Nickel, Copper, Zinc, Yttrium, Zirconium, Niobium, Molybdenum, Technetium, Ruthenium, Rhodium, Palladium, Silver, Cadmium } },
{ "Post-transition Metals", CRGB::Orange, 8, { Aluminum, Gallium, Indium, Thallium, Tin, Lead, Bismuth, Mercury } },
{ "Metalloids", CRGB::Yellow, 6, { Boron, Silicon, Germanium, Arsenic, Antimony, Tellurium } },
{ "Nonmetals", CRGB::Cyan, 6, { Carbon, Nitrogen, Oxygen, Phosphorus, Sulfur, Selenium } },
{ "Halogens", CRGB::Red, 5, { Fluorine, Chlorine, Bromine, Iodine, Astatine } },
{ "Noble Gases", CRGB::Purple, 6, { Helium, Neon, Argon, Krypton, Xenon, Radon } },
{ "Lanthanides", CRGB::Blue, 15, { Lanthanum, Cerium, Praseodymium, Neodymium, Promethium, Samarium, Europium, Gadolinium, Terbium, Dysprosium, Holmium, Erbium, Thulium, Ytterbium, Lutetium } },
{ "Actinides", CRGB::Green, 15, { Actinium, Thorium, Protactinium, Uranium, Neptunium, Plutonium, Americium, Curium, Berkelium, Californium, Einsteinium, Fermium, Mendelevium, Nobelium, Lawrencium } }
};
const uint8_t groupCnt = sizeof groups / sizeof * groups; // The number of groups
void showMenu() {
Serial.println(F("Choose a group"));
for (uint8_t i = 0; i < groupCnt; i++) {
Serial.print(i);
Serial.print(F(": "));
Serial.println(groups[i].name);
}
}
void showGroup(uint8_t groupIndex) {
if (groupIndex >= groupCnt) return;
FastLED.clear(); // Turn off LEDs
Serial.print(F("Showing the ")); Serial.println(groups[groupIndex].name);
for (uint8_t i = 0; i < groups[groupIndex].count; i++) {
for (uint8_t ledIndex = 0; ledIndex < ledCntPerElement; ledIndex++) leds[groups[groupIndex].elements[i] + ledIndex] = groups[groupIndex].color;
}
FastLED.show();
}
void setup() {
Serial.begin(9600); // assuming a BT module is connected to pin 0 and 1 and talking to MIT App Inventor app
FastLED.addLeds<WS2812B, dataPin, GRB>(leds, numLeds);
FastLED.clear();
showMenu();
}
void loop() {
if (Serial.available()) {
char choice = Serial.read();
if ((choice >= '0') && (choice < '0' + groupCnt)) showGroup(choice - '0');
showMenu();
}
}
/*
// if you wonder, no I did not layout the pixels manually,
// I wrote a small program that was generating the JSON for wokwi
// just add manually the connection from pin 3 to the Hydrogen DIN
// and remove the last comma when pasting the JSON for the parts or connections
struct Element {
const char* name;
const char* symbol;
int row;
int col;
};
Element periodicTable[] = {
{"Hydrogen", "H", 0, 0}, {"Helium", "He", 0, 17},
{"Lithium", "Li", 1, 0}, {"Beryllium", "Be", 1, 1}, {"Boron", "B", 1, 12}, {"Carbon", "C", 1, 13}, {"Nitrogen", "N", 1, 14}, {"Oxygen", "O", 1, 15}, {"Fluorine", "F", 1, 16}, {"Neon", "Ne", 1, 17},
{"Sodium", "Na", 2, 0}, {"Magnesium", "Mg", 2, 1}, {"Aluminum", "Al", 2, 12}, {"Silicon", "Si", 2, 13}, {"Phosphorus", "P", 2, 14}, {"Sulfur", "S", 2, 15}, {"Chlorine", "Cl", 2, 16}, {"Argon", "Ar", 2, 17},
{"Potassium", "K", 3, 0}, {"Calcium", "Ca", 3, 1}, {"Scandium", "Sc", 3, 2}, {"Titanium", "Ti", 3, 3}, {"Vanadium", "V", 3, 4}, {"Chromium", "Cr", 3, 5}, {"Manganese", "Mn", 3, 6}, {"Iron", "Fe", 3, 7}, {"Cobalt", "Co", 3, 8}, {"Nickel", "Ni", 3, 9}, {"Copper", "Cu", 3, 10}, {"Zinc", "Zn", 3, 11}, {"Gallium", "Ga", 3, 12}, {"Germanium", "Ge", 3, 13}, {"Arsenic", "As", 3, 14}, {"Selenium", "Se", 3, 15}, {"Bromine", "Br", 3, 16}, {"Krypton", "Kr", 3, 17},
{"Rubidium", "Rb", 4, 0}, {"Strontium", "Sr", 4, 1}, {"Yttrium", "Y", 4, 2}, {"Zirconium", "Zr", 4, 3}, {"Niobium", "Nb", 4, 4}, {"Molybdenum", "Mo", 4, 5}, {"Technetium", "Tc", 4, 6}, {"Ruthenium", "Ru", 4, 7}, {"Rhodium", "Rh", 4, 8}, {"Palladium", "Pd", 4, 9}, {"Silver", "Ag", 4, 10}, {"Cadmium", "Cd", 4, 11}, {"Indium", "In", 4, 12}, {"Tin", "Sn", 4, 13}, {"Antimony", "Sb", 4, 14}, {"Tellurium", "Te", 4, 15}, {"Iodine", "I", 4, 16}, {"Xenon", "Xe", 4, 17},
{"Cesium", "Cs", 5, 0}, {"Barium", "Ba", 5, 1}, {"Lanthanum", "La", 5, 2}, {"Hafnium", "Hf", 5, 3}, {"Tantalum", "Ta", 5, 4}, {"Tungsten", "W", 5, 5}, {"Rhenium", "Re", 5, 6}, {"Osmium", "Os", 5, 7}, {"Iridium", "Ir", 5, 8}, {"Platinum", "Pt", 5, 9}, {"Gold", "Au", 5, 10}, {"Mercury", "Hg", 5, 11}, {"Thallium", "Tl", 5, 12}, {"Lead", "Pb", 5, 13}, {"Bismuth", "Bi", 5, 14}, {"Polonium", "Po", 5, 15}, {"Astatine", "At", 5, 16}, {"Radon", "Rn", 5, 17},
{"Francium", "Fr", 6, 0}, {"Radium", "Ra", 6, 1}, {"Actinium", "Ac", 6, 2}, {"Rutherfordium", "Rf", 6, 3}, {"Dubnium", "Db", 6, 4}, {"Seaborgium", "Sg", 6, 5}, {"Bohrium", "Bh", 6, 6}, {"Hassium", "Hs", 6, 7}, {"Meitnerium", "Mt", 6, 8}, {"Darmstadtium", "Ds", 6, 9}, {"Roentgenium", "Rg", 6, 10}, {"Copernicium", "Cn", 6, 11}, {"Nihonium", "Nh", 6, 12}, {"Flerovium", "Fl", 6, 13}, {"Moscovium", "Mc", 6, 14}, {"Livermorium", "Lv", 6, 15}, {"Tennessine", "Ts", 6, 16}, {"Oganesson", "Og", 6, 17},
{"Cerium", "Ce", 8, 3}, {"Praseodymium", "Pr", 8, 4}, {"Neodymium", "Nd", 8, 5}, {"Promethium", "Pm", 8, 6}, {"Samarium", "Sm", 8, 7}, {"Europium", "Eu", 8, 8}, {"Gadolinium", "Gd", 8, 9}, {"Terbium", "Tb", 8, 10}, {"Dysprosium", "Dy", 8, 11}, {"Holmium", "Ho", 8, 12}, {"Erbium", "Er", 8, 13}, {"Thulium", "Tm", 8, 14}, {"Ytterbium", "Yb", 8, 15}, {"Lutetium", "Lu", 8, 16},
{"Thorium", "Th", 9, 3}, {"Protactinium", "Pa", 9, 4}, {"Uranium", "U", 9, 5}, {"Neptunium", "Np", 9, 6}, {"Plutonium", "Pu", 9, 7}, {"Americium", "Am", 9, 8}, {"Curium", "Cm", 9, 9}, {"Berkelium", "Bk", 9, 10}, {"Californium", "Cf", 9, 11}, {"Einsteinium", "Es", 9, 12}, {"Fermium", "Fm", 9, 13}, {"Mendelevium", "Md", 9, 14}, {"Nobelium", "No", 9, 15}, {"Lawrencium", "Lr", 9, 16}
};
size_t nb = sizeof periodicTable / sizeof * periodicTable;
void setup() {
Serial.begin(115200);
Serial.println(nb);
Serial.println("-------------------------\n");
for (auto& element : periodicTable)
Serial.printf("{ \"type\": \"wokwi-neopixel\", \"id\": \"%s\", \"top\": %d, \"left\": %d, \"attrs\": {} },\r\n", element.name, -200 + element.row * 25, element.col * 25 );
Serial.println("\n\n\n\n\n\n\n");
for (byte i = 0; i < nb - 1; i++)
Serial.printf("[ \"%s:DOUT\", \"%s:DIN\", \"\", [] ],\r\n", periodicTable[i].name, periodicTable[i + 1].name );
}
void loop() {}
*/