mirror of
https://github.com/RobTillaart/Arduino.git
synced 2024-10-03 18:09:02 -04:00
0.1.1 AtomicWeight
This commit is contained in:
parent
74daa8b597
commit
ce8751d505
@ -3,148 +3,14 @@
|
||||
// FILE: AtomicWeight.h
|
||||
// AUTHOR: Rob Tillaart
|
||||
// DATE: 2022-03-09
|
||||
// VERSION: 0.1.0
|
||||
// VERSION: 0.1.1
|
||||
// PURPOSE: Arduino library for atomic weights
|
||||
// URL: https://github.com/RobTillaart/AtomicWeight
|
||||
|
||||
|
||||
#include "Arduino.h"
|
||||
|
||||
#define ATOMIC_WEIGHT_LIB_VERSION (F("0.1.0"))
|
||||
|
||||
|
||||
/////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// list of elements
|
||||
// weight = weight * 222.909
|
||||
//
|
||||
struct element
|
||||
{
|
||||
char name[3];
|
||||
uint16_t weight;
|
||||
}
|
||||
elements[119] =
|
||||
{
|
||||
{"n", 225}, // neutronium
|
||||
{"H", 225},
|
||||
{"He", 892},
|
||||
{"Li", 2186},
|
||||
{"Be", 2009},
|
||||
{"B", 2410},
|
||||
{"C", 2677},
|
||||
{"N", 3122},
|
||||
{"O", 3566},
|
||||
{"F", 4235},
|
||||
{"Ne", 4498},
|
||||
{"Na", 5125},
|
||||
{"Mg", 5418},
|
||||
{"Al", 6014},
|
||||
{"Si", 6261},
|
||||
{"P", 6904},
|
||||
{"S", 7148},
|
||||
{"Cl", 7903},
|
||||
{"Ar", 8905},
|
||||
{"K", 8715},
|
||||
{"Ca", 8934},
|
||||
{"Sc", 10021},
|
||||
{"Ti", 10670},
|
||||
{"V", 11355},
|
||||
{"Cr", 11590},
|
||||
{"Mn", 12246},
|
||||
{"Fe", 12448},
|
||||
{"Co", 13137},
|
||||
{"Ni", 13083},
|
||||
{"Cu", 14165},
|
||||
{"Zn", 14580},
|
||||
{"Ga", 15542},
|
||||
{"Ge", 16192},
|
||||
{"As", 16701},
|
||||
{"Se", 17601},
|
||||
{"Br", 17811},
|
||||
{"Kr", 18679},
|
||||
{"Rb", 19052},
|
||||
{"Sr", 19531},
|
||||
{"Y", 19818},
|
||||
{"Zr", 20335},
|
||||
{"Nb", 20710},
|
||||
{"Mo", 21386},
|
||||
{"Tc", 21845},
|
||||
{"Ru", 22529},
|
||||
{"Rh", 22939},
|
||||
{"Pd", 23722},
|
||||
{"Ag", 24045},
|
||||
{"Cd", 25057},
|
||||
{"In", 25594},
|
||||
{"Sn", 26462},
|
||||
{"Sb", 27141},
|
||||
{"Te", 28443},
|
||||
{"I", 28288},
|
||||
{"Xe", 29266},
|
||||
{"Cs", 29626},
|
||||
{"Ba", 30611},
|
||||
{"La", 30963},
|
||||
{"Ce", 31233},
|
||||
{"Pr", 31410},
|
||||
{"Nd", 32152},
|
||||
{"Pm", 32322},
|
||||
{"Sm", 33517},
|
||||
{"Eu", 33874},
|
||||
{"Gd", 35052},
|
||||
{"Tb", 35426},
|
||||
{"Dy", 36223},
|
||||
{"Ho", 36764},
|
||||
{"Er", 37284},
|
||||
{"Tm", 37657},
|
||||
{"Yb", 38572},
|
||||
{"Lu", 39002},
|
||||
{"Hf", 39787},
|
||||
{"Ta", 40335},
|
||||
{"W", 40980},
|
||||
{"Re", 41507},
|
||||
{"Os", 42404},
|
||||
{"Ir", 42847},
|
||||
{"Pt", 43485},
|
||||
{"Au", 43906},
|
||||
{"Hg", 44713},
|
||||
{"Tl", 45559},
|
||||
{"Pb", 46187},
|
||||
{"Bi", 46584},
|
||||
{"Po", 46588},
|
||||
{"At", 46811},
|
||||
{"Rn", 49486},
|
||||
{"Fr", 49709},
|
||||
{"Ra", 50377},
|
||||
{"Ac", 50600},
|
||||
{"Th", 51723},
|
||||
{"Pa", 51500},
|
||||
{"U", 53059},
|
||||
{"Np", 52829},
|
||||
{"Pu", 54390},
|
||||
{"Am", 54167},
|
||||
{"Cm", 55059},
|
||||
{"Bk", 55059},
|
||||
{"Cf", 55950},
|
||||
{"Es", 56173},
|
||||
{"Fm", 57288},
|
||||
{"Md", 57511},
|
||||
{"No", 57733},
|
||||
{"Lr", 58402},
|
||||
{"Rf", 58179},
|
||||
{"Db", 58402},
|
||||
{"Sg", 59294},
|
||||
{"Bh", 58848},
|
||||
{"Hs", 61746},
|
||||
{"Mt", 59740},
|
||||
{"Ds", 62637},
|
||||
{"Rg", 60631},
|
||||
{"Cn", 63529},
|
||||
{"Nh", 63752},
|
||||
{"Fl", 64421},
|
||||
{"Mc", 64421},
|
||||
{"Lv", 65312},
|
||||
{"Ts", 65535},
|
||||
{"Og", 65535},
|
||||
};
|
||||
#define ATOMIC_WEIGHT_LIB_VERSION (F("0.1.1"))
|
||||
|
||||
|
||||
/////////////////////////////////////////////////////////////////////////
|
||||
@ -155,29 +21,26 @@ elements[119] =
|
||||
class PTOE
|
||||
{
|
||||
public:
|
||||
PTOE(uint8_t size = 118) { _size = size; }; // all by default
|
||||
PTOE(uint8_t size = 118); // all by default
|
||||
uint8_t size();
|
||||
|
||||
uint8_t size() { return _size; };
|
||||
|
||||
uint8_t electrons(uint8_t el) { return el; };
|
||||
uint8_t neutrons(uint8_t el) { return round(weight(el) - el); };
|
||||
uint8_t protons(uint8_t el) { return el; };
|
||||
uint8_t electrons(uint8_t el);
|
||||
uint8_t neutrons(uint8_t el);
|
||||
uint8_t protons(uint8_t el);
|
||||
|
||||
float weight(uint8_t el) { return elements[el - 1].weight * _weightFactor; };
|
||||
|
||||
char * name(uint8_t el) { return elements[el - 1].name; };
|
||||
uint8_t find(char * abbrev)
|
||||
{
|
||||
for (uint8_t i = 0; i < _size; i++)
|
||||
{
|
||||
if (strcmp(elements[i].name, abbrev) == 0) return i;
|
||||
}
|
||||
return 255;
|
||||
}
|
||||
float weight(uint8_t el);
|
||||
float weight(const char * formula);
|
||||
float weight(char * formula);
|
||||
|
||||
|
||||
char * name(uint8_t el);
|
||||
uint8_t find(char * abbrev);
|
||||
|
||||
|
||||
// DEBUG
|
||||
float weightFactor() { return _weightFactor; };
|
||||
float weightFactor();
|
||||
|
||||
|
||||
private:
|
||||
|
@ -6,6 +6,14 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/)
|
||||
and this project adheres to [Semantic Versioning](http://semver.org/).
|
||||
|
||||
|
||||
## [0.1.1] - 2022-12-30
|
||||
- fix offset in some functions
|
||||
- move code to .cpp file
|
||||
- add **float weight(char \* formula)**
|
||||
- refactor and clean up a bit.
|
||||
- update readme.md.
|
||||
|
||||
|
||||
## [0.1.0] - 2022-12-30
|
||||
- initial release
|
||||
- renamed class from AtomicWeight to PTOE.
|
||||
@ -13,7 +21,6 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
|
||||
- add examples.
|
||||
- update readme.md.
|
||||
|
||||
|
||||
----
|
||||
|
||||
## [0.0.1] - 2022-03-09
|
||||
|
@ -15,14 +15,21 @@ Arduino library for atomic weights.
|
||||
|
||||
This library is mainly as a base for educational purposes.
|
||||
Learning the periodic table of elements, the abbreviations and weight.
|
||||
It also provides the number of electrons, neutrons and protons per element.
|
||||
It also provides the number of electrons, neutrons and protons per element.
|
||||
|
||||
Furthermore the library has a **weight()** function, that returns the weight
|
||||
of either an element or by formula. See examples.
|
||||
|
||||
Note: library is still experimental. More testing is needed.
|
||||
|
||||
|
||||
#### internal
|
||||
|
||||
Table uses compressed weight to save RAM
|
||||
- store weight as **uint16_t**, 0..65535
|
||||
- weight factor = 222.909 = (65535.0 / heaviest element=118)
|
||||
- error < 0.5%
|
||||
The PTOE class uses a table that has compressed weight to save RAM
|
||||
- it stores weights as **uint16_t**, 0..65535 instead of floats.
|
||||
- weight factor = 222.909 = (65535.0 / heaviest element = 118)
|
||||
- error < 0.3%
|
||||
- the table (and thus the class) does not handle isotopes.
|
||||
|
||||
|
||||
## Interface
|
||||
@ -35,7 +42,11 @@ Table uses compressed weight to save RAM
|
||||
- **uint8_t electrons(uint8_t element)** returns the number of electrons of the element.
|
||||
- **uint8_t neutrons(uint8_t element)** returns the number of neutrons of the element.
|
||||
- **uint8_t protons(uint8_t element)** returns the number of protons of the element.
|
||||
- **float weight(uint8_t element)** returns the weight of the element. (error < 0.5%, table uses "weight compression")
|
||||
- **float weight(uint8_t element)** returns the weight of the element.
|
||||
The error < 0.3%, table uses "weight compression"/
|
||||
- **float weight(char \* formula)** returns the weight of a molecule e.g. "H2O".
|
||||
Returns 0 if it cannot parse the formula given.
|
||||
Cannot parse complex formulas with brackets () in it.
|
||||
- **uint8_t find(char \* abbreviation)** returns the element number.
|
||||
- **char \* name(uint8_t element)** returns the abbreviation of element.
|
||||
|
||||
@ -45,6 +56,20 @@ Table uses compressed weight to save RAM
|
||||
- **float weightFactor()** returns weightFactor.
|
||||
|
||||
|
||||
## weight
|
||||
|
||||
The **weight(formula)** function is meant to calculate the weight of a molecule.
|
||||
It does not care about the order of the atoms.
|
||||
So "C6H6" is equal to "H6C6" or even "CCCCCCHHHHHH" or "C3H3C3H3".
|
||||
Elements are defined as 1 or two characters long.
|
||||
The first must be uppercase, the second must be lowercase.
|
||||
If no number is provided the count of 1 is assumed.
|
||||
|
||||
The function returns a float, so to get the integer weight, one should use **round()**.
|
||||
|
||||
The weight parsing does not support brackets () in the formula so "H2(SO4)2" fails.
|
||||
|
||||
|
||||
## Operation
|
||||
|
||||
See examples
|
||||
@ -59,24 +84,30 @@ See examples
|
||||
|
||||
#### should
|
||||
|
||||
- extend unit tests
|
||||
- extend formula parser with () support.
|
||||
- extend formula parser with error codes?
|
||||
|
||||
|
||||
#### could
|
||||
|
||||
- state table
|
||||
- liquid, gas, solid, unknown (2 bits per element) = ~30 bytes
|
||||
- (short) table of English names
|
||||
- which ones ?
|
||||
- move code to .cpp file
|
||||
- parameters element should be less than _size
|
||||
- function **float massPercentage("H2O", "H")** ~10%
|
||||
- function **float atomicPercentage("H2O", "H")** ~33%
|
||||
- performance **find()** ?
|
||||
- case (in)sensitive **find()**
|
||||
|
||||
#### could
|
||||
|
||||
- extend unit tests
|
||||
- function weight("H2O") => 18
|
||||
- easier to parse with separator: weight("C6 H6 O6 ")
|
||||
|
||||
#### wont (unless)
|
||||
|
||||
- parameters element should be less than \_size
|
||||
- user responsibility
|
||||
- more information?
|
||||
- Electron bands K L M etc?
|
||||
- database needed
|
||||
- Electron bands K L M etc?
|
||||
- temperatures,
|
||||
- melt
|
||||
- evaporate
|
||||
|
@ -3,8 +3,7 @@
|
||||
// FILE: elements_float.h
|
||||
// AUTHOR: Rob Tillaart
|
||||
// DATE: 2022-03-09
|
||||
// VERSION: 0.1.0
|
||||
// PURPOSE: list of weights
|
||||
// PURPOSE: list of weights (float)
|
||||
// URL: https://github.com/RobTillaart/AtomicWeight
|
||||
|
||||
|
||||
|
@ -3,11 +3,13 @@
|
||||
// PURPOSE: demo
|
||||
// URL: https://github.com/RobTillaart/AtomicWeight
|
||||
|
||||
|
||||
#include "Arduino.h"
|
||||
#include "AtomicWeight.h"
|
||||
|
||||
PTOE ptoe;
|
||||
|
||||
|
||||
void setup()
|
||||
{
|
||||
Serial.begin(115200);
|
||||
@ -19,15 +21,18 @@ void setup()
|
||||
|
||||
n = ptoe.find("Au");
|
||||
Serial.println(ptoe.protons(n));
|
||||
|
||||
|
||||
n = ptoe.find("H");
|
||||
Serial.println(ptoe.protons(n));
|
||||
|
||||
|
||||
Serial.println("\ndone...");
|
||||
}
|
||||
|
||||
|
||||
void loop()
|
||||
{
|
||||
}
|
||||
|
||||
// -- END OF FILE --
|
||||
|
||||
// -- END OF FILE --
|
||||
|
||||
|
@ -0,0 +1,58 @@
|
||||
// FILE: atomic_weight_formula.ino
|
||||
// AUTHOR: Rob Tillaart
|
||||
// PURPOSE: demo
|
||||
// URL: https://github.com/RobTillaart/AtomicWeight
|
||||
|
||||
#include "Arduino.h"
|
||||
#include "AtomicWeight.h"
|
||||
|
||||
|
||||
PTOE ptoe;
|
||||
|
||||
char formula1[24] = "C6H6O6";
|
||||
char formula2[24] = "H2SO4";
|
||||
char formula3[24] = "HHSOOOO";
|
||||
|
||||
void setup()
|
||||
{
|
||||
Serial.begin(115200);
|
||||
while (!Serial);
|
||||
Serial.println(__FILE__);
|
||||
Serial.print("ATOMIC_WEIGHT_LIB_VERSION: ");
|
||||
Serial.println(ATOMIC_WEIGHT_LIB_VERSION);
|
||||
|
||||
Serial.print("C");
|
||||
Serial.print(" \t");
|
||||
Serial.println(ptoe.weight("C"));
|
||||
|
||||
Serial.print("C6");
|
||||
Serial.print(" \t");
|
||||
Serial.println(ptoe.weight("C6"));
|
||||
|
||||
Serial.print("He6");
|
||||
Serial.print(" \t");
|
||||
Serial.println(ptoe.weight("He6"));
|
||||
|
||||
Serial.print("O6");
|
||||
Serial.print(" \t");
|
||||
Serial.println(ptoe.weight("O6"));
|
||||
|
||||
Serial.print(formula1);
|
||||
Serial.print(" \t");
|
||||
Serial.println(ptoe.weight(formula1));
|
||||
|
||||
Serial.print(formula2);
|
||||
Serial.print(" \t");
|
||||
Serial.println(ptoe.weight(formula2));
|
||||
|
||||
Serial.print(formula3);
|
||||
Serial.print(" \t");
|
||||
Serial.println(ptoe.weight(formula3));
|
||||
}
|
||||
|
||||
void loop()
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
// -- END OF FILE --
|
@ -3,10 +3,10 @@
|
||||
// PURPOSE: demo
|
||||
// URL: https://github.com/RobTillaart/AtomicWeight
|
||||
|
||||
|
||||
#include "Arduino.h"
|
||||
#include "AtomicWeight.h"
|
||||
|
||||
uint32_t start, stop;
|
||||
|
||||
PTOE ptoe;
|
||||
|
||||
@ -23,9 +23,9 @@ void setup()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void loop()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
|
||||
@ -45,4 +45,5 @@ void printElem(uint8_t i)
|
||||
}
|
||||
|
||||
|
||||
// -- END OF FILE --
|
||||
// -- END OF FILE --
|
||||
|
||||
|
@ -3,22 +3,22 @@
|
||||
// FILE: elements_float.h
|
||||
// AUTHOR: Rob Tillaart
|
||||
// DATE: 2022-03-09
|
||||
// VERSION: 0.1.0
|
||||
// PURPOSE: list of weights
|
||||
// PURPOSE: list of weights (float)
|
||||
// URL: https://github.com/RobTillaart/AtomicWeight
|
||||
|
||||
|
||||
/////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
//
|
||||
// float periodic table
|
||||
//
|
||||
struct element
|
||||
{
|
||||
char name[3];
|
||||
float weight;
|
||||
}
|
||||
elements[118] =
|
||||
elements[119] =
|
||||
{
|
||||
{"n", 1.00794}, // neutronium
|
||||
{"H" , 1.00794},
|
||||
{"He", 4.002602},
|
||||
{"Li", 9.80665},
|
||||
|
@ -2,7 +2,7 @@
|
||||
// AUTHOR: Rob Tillaart
|
||||
// URL: https://github.com/RobTillaart/AtomicWeight
|
||||
// PURPOSE: generate the 16 bit table for the AtomicWeight library
|
||||
// instead of a float per element it uses an uint16_t
|
||||
// instead of a float per element it uses an uint16_t
|
||||
// and a weight factor. The error is less than 0.3%.
|
||||
|
||||
|
||||
@ -16,12 +16,7 @@ void setup()
|
||||
while (!Serial);
|
||||
Serial.println(__FILE__);
|
||||
|
||||
/////////////////////////////////////////////////////////
|
||||
//
|
||||
// generate 16 bit table
|
||||
//
|
||||
|
||||
// HEADER
|
||||
// HEADER
|
||||
Serial.println();
|
||||
Serial.println();
|
||||
Serial.println("/////////////////////////////////////////");
|
||||
@ -31,15 +26,14 @@ void setup()
|
||||
Serial.println("//");
|
||||
Serial.println("struct element");
|
||||
Serial.println("{");
|
||||
Serial.println(" // uint8_t nr;");
|
||||
Serial.println(" char name[3];");
|
||||
Serial.println(" uint16_t weight;");
|
||||
Serial.println("}");
|
||||
Serial.println("elements[118] =");
|
||||
Serial.println("elements[119] =");
|
||||
|
||||
// ELEMENTS
|
||||
// ELEMENTS
|
||||
Serial.println("{");
|
||||
for (int i = 0; i < 118; i++)
|
||||
for (int i = 0; i < 119; i++)
|
||||
{
|
||||
Serial.print(" {\"");
|
||||
Serial.print(elements[i].name);
|
||||
@ -52,8 +46,11 @@ void setup()
|
||||
Serial.println();
|
||||
}
|
||||
|
||||
|
||||
void loop()
|
||||
{
|
||||
}
|
||||
|
||||
// -- END OF FILE --
|
||||
|
||||
// -- END OF FILE --
|
||||
|
||||
|
@ -15,7 +15,7 @@
|
||||
"type": "git",
|
||||
"url": "https://github.com/RobTillaart/AtomicWeight.git"
|
||||
},
|
||||
"version": "0.1.0",
|
||||
"version": "0.1.1",
|
||||
"license": "MIT",
|
||||
"frameworks": "arduino",
|
||||
"platforms": "*",
|
||||
|
@ -1,5 +1,5 @@
|
||||
name=AtomicWeight
|
||||
version=0.1.0
|
||||
version=0.1.1
|
||||
author=Rob Tillaart <rob.tillaart@gmail.com>
|
||||
maintainer=Rob Tillaart <rob.tillaart@gmail.com>
|
||||
sentence=Arduino library for AtomicWeight
|
||||
|
Loading…
Reference in New Issue
Block a user