//Display
#include <SoftwareSerial.h>
// These are the Arduino pins required to create a software seiral
// instance. We'll actually only use the TX pin.
//SoftwareSerial Serial7Segment(0, 1); //RX pin, TX pin
const int softwareTx = 10;
const int softwareRx = 9;
SoftwareSerial Serial7Segment(softwareRx, softwareTx);
// Send the clear display command (0x76)
// This will clear the display and reset the cursor
void clearDisplay()
{
Serial7Segment.write(0x76); // Clear display command
// Serial7Segment.write(0x81); // Send factory reset command
}
// Set the displays brightness. Should receive byte with the value
// to set the brightness to
// dimmest------------->brightest
// 0--------127--------255
void setBrightness(byte value)
{
Serial7Segment.write(0x7A); // Set brightness command byte
Serial7Segment.write(value); // brightness data byte
}
// variablen and pin declaration
int PotConst2 = A1;
int PotConst1 = A8;
int PotConst1Value;
int PotConst2Value;
float varstar = 465849; // 467470 // 0.348% error (5min) 465848.846
float varsun = 466161;
float varmoon = 451109;
long previousMillis = 0;
long previousMicros = 0;
const int StarPin = 16;
const int SunPin = 17;
const int MoonPin = 18;
const int ConstPin = 19;
int starstate = 0;
int sunstate = 0;
int moonstate = 0;
int conststate = 0;
// the setup routine
void setup()
{
pinMode(16, INPUT);
pinMode(17, INPUT);
pinMode(18, INPUT);
pinMode(19, INPUT);
pinMode(7, OUTPUT);
digitalWriteFast(7, LOW);
pinMode(13, OUTPUT);
digitalWriteFast(13, LOW);
// initialize serial communication at 9600 bits per second:
Serial7Segment.begin(9600);
// setBrightness(0); // Lowest brightness
// setBrightness(127); // Medium brightness
setBrightness(255); // High brightness
clearDisplay();
}
void loop() {
digitalWriteFast(7, LOW);
starstate = digitalReadFast(StarPin);
if (starstate == HIGH) {
star();
}
else {
moonstate = digitalReadFast(MoonPin);
if (moonstate == HIGH) {
moon();
}
else {
sunstate = digitalReadFast(SunPin);
if (sunstate == HIGH) {
sun();
}
else {
conststate = digitalReadFast(ConstPin);
if (conststate == HIGH) {
constant();
}
}
}
}
}
void star(){
Serial7Segment.print("STAR");
unsigned long currentMicros = micros();
if(currentMicros - previousMicros > varstar)
{
previousMicros = currentMicros;
digitalWriteFast(7, HIGH);
}
}
void moon(){
Serial7Segment.print("Eoon");
unsigned long currentMicros = micros();
if(currentMicros - previousMicros > varmoon)
{
previousMicros = currentMicros;
digitalWriteFast(7, HIGH);
}
}
void sun(){
Serial7Segment.print(" Sun");
unsigned long currentMicros = micros();
if(currentMicros - previousMicros > varsun)
{
previousMicros = currentMicros;
digitalWriteFast(7, HIGH);
}
}
void constant() {
PotConst2Value = analogRead(PotConst2);
PotConst1Value = analogRead(PotConst1);
int interval1 =map(PotConst1Value, 0, 1023, 0, 9899);
int interval2 =map(PotConst2Value, 0, 1023, 1, 100);
displaydigits(interval1 + interval2);
unsigned long currentMillis = millis();
if(currentMillis - previousMillis > interval1 + interval2)
{
previousMillis = currentMillis;
digitalWriteFast(7, HIGH);
}
}
void displaydigits(int dig)
{
Serial7Segment.write("w");
Serial7Segment.write(256);
if ((dig < 10) && (dig >= 0)){//1
Serial7Segment.print("XXX");
Serial7Segment.print(dig);
}
if ((dig >= 10) && (dig < 100)){
Serial7Segment.print("XX");
Serial7Segment.print(dig);
}
if ((dig >= 100) && (dig < 1000)){
Serial7Segment.print("X");
Serial7Segment.print(dig);
}
if (dig >= 1000){
Serial7Segment.print(dig);
}
if ((dig < 0) && (dig > -10)){//1
Serial7Segment.print("XX");
Serial7Segment.print(dig);
}
if ((dig <= -10) && (dig > -100)){
Serial7Segment.print("X");
Serial7Segment.print(dig);
}
if (dig < -100){
Serial7Segment.print("");
Serial7Segment.print(dig);
}
}