Arduino UNO V3 program code

Here you will find all the program codes accompanying the book

 

 

You can purchase the book from ePubli:

https://www.epubli.de/preview/130629

 

Because the preview on ePubli doesn't work very well – sorry, with low carb or whatever these vegetarians are called, I have nothing to do with that – here is an excerpt:

 

Arduino1_excerpt

 

Program Downloads (typing is not)

Arduino

 

For insiders (Password protected)

Buch

 

Videos:

 

 

 

 

The highlight, the perfect watch (Forget that perfect right now, she has in 100 years approx. 2 seconds difference)

/*
7-Segment LED display red
—A—
| |
F B
| |
—G—
| |
E C
| |
—D—

*/
//———————————————————- The declaration —————————————————————-
//Define exits
#define A 6
#define B 5
#define C 4
#define D 3
#define E 2
#define F 1
#define G 0
#define DP 7 // Decimalpunk
#define common_cathode 0
#define common_anode 1
bool segMode = common_cathode; // Negative or positive representation (LCD or LED), depending on the display type (common_cathode oder common_anode)
mind you[] {A,B,C,D,E,F,G,DP}; // Segment-Pins 6 – 0
byte chars = 35; // Number of characters for subsequent array Chars

byte Chars[35][9] {
{‘0’,1,1,1,1,1,1,0,0}, //0
{‘1’,0,1,1,0,0,0,0,0}, //1
{‘2’,1,1,0,1,1,0,1,0}, //2
{‘3’,1,1,1,1,0,0,1,0}, //3
{‘4’,0,1,1,0,0,1,1,0}, //4
{‘5’,1,0,1,1,0,1,1,0}, //5
{‘6’,1,0,1,1,1,1,1,0}, //6
{‘7’,1,1,1,0,0,0,0,0}, //7
{‘8’,1,1,1,1,1,1,1,0}, //8
{‘9’,1,1,1,1,0,1,1,0}, //9
{‘a’,1,1,1,0,1,1,1,0},//A/10
{‘b’,0,0,1,1,1,1,1,0},//b/11
{‘c’,1,0,0,1,1,1,0,0},//C/12
{‘d’,0,1,1,1,1,0,1,0},//d/13
{‘e’,1,0,0,1,1,1,1,0},//E/14
{‘f’,1,0,0,0,1,1,1,0},//F/15
{‘g’,1,0,1,1,1,1,0,0},//G/16
{‘h’,0,1,1,0,1,1,1,0},//H/17
{‘i’,0,0,0,0,1,1,0,0},//I/18
{‘j’,0,1,1,1,1,0,0,0},//J/19
{‘l’,0,0,0,1,1,1,0,0},//L/20
{‘n’,0,0,1,0,1,0,1,0},//n/21
{‘o’,0,0,1,1,1,0,1,0},//o/22
{‘p’,1,1,0,0,1,1,1,0},//P/23
{‘q’,1,1,1,0,0,1,1,0},//q/24
{‘r’,0,0,0,0,1,0,1,0},//r/25
{‘s’,1,0,1,1,0,1,1,0},//S/26 looks like number 5
{‘t’,0,0,0,1,1,1,1,0},//t/27
{‘u’,0,1,1,1,1,1,0,0},//U/28
{‘y’,0,1,1,1,0,1,1,0},//y/29
{‘-‘,0,0,0,0,0,0,1,0},//-/30
{‘.’,0,0,0,0,0,0,0,1},//./31
{‘]’,1,1,1,1,0,0,0,0},//]/32
{‘[‘,1,0,0,1,1,1,0,0},//[/33
{‘_’,0,0,0,1,0,0,0,0},//_/34
};

unsigned long time_in_milliseconds = 0; //Zeitvariable in Millis
unsigned long time_correction_millis = 0; //Correction variable, since Arduino UNO about after 50 Days the millis() on 0 resets
unsigned int plus_hours = 0; // Time setting to current time
unsigned int plus_minutes = 0; // Time setting to current time correction minutes
unsigned int plus_seconds = 0; // Time setting to current time correction seconds
unsigned int plus_milliseconds = 0; // Time setting to current time Correction milliseconds
unsigned int hours = 0; // hours for time
unsigned int minutes = 0; // minutes for time
unsigned int seconds = 0; // seconds for time
unsigned int milliseconds = 0; // milliseconds e.g. for stop watch
char timestamp = “23:59:59:999”; // Compiled timestamp
int x; // Function call for global time setting
//const int key[] = {8,9,10,11,12,13}; //Job 0 is on the far right
int count = 0;
long stay_on = 0;
unsigned long time_reset_key = 0;
int seg_pin[] = {8,9,10,11,12,13}; //Block-Nr, consisting of 2 digits (7-segment ads)
//int ziff_nr[] = {12,13}; //digit number 1 = front, 2 = rear
int key_analog_pin = A0;
int analog_value; //handover vatiable
int display_mode; //Choose, what is displayed:
//0 = clock, 1 = correction hours, 2 = correction minutes, 3 = correction seconds,
//4 = It is tempered 5 = light and others (output 0 – 1024
//—————————————————— End of Declaration —————————————————-

//——————————————————– Initialize ——————————————————-
void setup() {
// Define pins as OUTPUT
pinMode(themselves[0],OUTPUT);
pinMode(themselves[1],OUTPUT);
pinMode(themselves[2],OUTPUT);
pinMode(themselves[3],OUTPUT);
pinMode(themselves[4],OUTPUT);
pinMode(themselves[5],OUTPUT);
pinMode(themselves[6],OUTPUT);
pinMode(themselves[7],OUTPUT);
pinMode(seg_pin[0],OUTPUT);
pinMode(seg_pin[1],OUTPUT);
pinMode(seg_pin[2],OUTPUT);
pinMode(seg_pin[3],OUTPUT);
pinMode(seg_pin[4],OUTPUT);
pinMode(seg_pin[5],OUTPUT);

}
//—————————————————— End initialization ————————————————

//—————————————————— main program ————————————————

void loop() {

x = get_time(‘all’); // Fill in all global variables
x = get_analog_value(); //get the analog value
if (display_mode == 0) {
Print_Two_Digits(hours, “hours”, false);
Print_Two_Digits(minutes, “minutes”, false);
Print_Two_Digits(seconds, “seconds”, false);
}

if (display_mode == 4) {
x = scale_temperature();
x = print_to_display(x);
}
if (display_mode == 5) {
x = print_to_display(analog_value); //return the value of the analog input
//x = print_to_display(123456); //Test-String, if you should have problems
}

if (display_mode == 1) {
x = Print_Two_Digits(plus_hours, “hours”, false);
}

if (display_mode == 2) {
x = Print_Two_Digits(plus_minutes, “minutes”, false);
}

if (display_mode == 3) {
x = Print_Two_Digits(plus_seconds, “seconds”, false);
}

//Check for key status
x = change_analog_key_status();

}
//—————————————————— End of main program ————————————————

//———————————————————- Functions ——————————————————
void setState(bool mode){ //Display characters in mode mode, i.e. positive or negative
for(int i = 0;i<=6;i++)
{
digitalWrite(themselves[i],mode);
}
}

 

void Print(whether) // Output to the 7-segment display
{
setState(segMode);//Switch off segment

if(num > chars || num < 0 )// If the number or. the character is not defined…
{
for(int i = 0;i <= 6;i++)
{
digitalWrite(themselves[i],HIGH);
delay(100);
digitalWrite(themselves[i],LOW);
}
for(int i = 0;i <= 2;i++)
{
delay(10);
setState(HIGH);
delay(10);
setState(LOW);
}
}else // otherwise, if sign or. number defined, then spend
{
if(segMode == 0){ //…output segMode accordingly (positive or negative)
for(int i = 0;i<8;i++){
digitalWrite(themselves[i],Chars[num][i+1]);
}

//Kontrast oder How-To-Works
x = kontrast(num);

//Clear
for(int i = 0;i<8;i++){
digitalWrite(themselves[i],LOW);
}
}
else{
//setState(HIGH);
for(int i = 0;i<8;i++){
digitalWrite(themselves[i],!Chars[num][i+1]);
digitalWrite(themselves[i],HIGH);
}
}
}
}

you are a contrast(whether){
//Consistent display of the numbers, 7 and 1 only a few LEDs are switched on
if (num == 1 || num == 7){
delay(1);
return;
}
delay(3);

}

int Print_Two_Digits(int two_digits_wert, char mode[], boolean one_segment){
// modus = “nothing” –> no display of the mode
// mode = “hours”, “minutes”, “seconds” displays in front of the display, which value is output
int digit_links = 0;
int digit_right = 0;
int modus_char = 30;
int seg_pin_out = 0; //The pin for the output

//Show mode, if passed //display h
if (modus == “hours”) {
mode_char = 17;
seg_pin_out = seg_pin[4]; //Activate first segment
}
if (modus == “minutes”) { //To sue, since m cannot be displayed
mode_char = 21;
seg_pin_out = seg_pin[2];
}
if (modus == “seconds”) { //show s
mode_char = 26;
seg_pin_out = seg_pin[0];
}

//Share ad 2 digits up
if (two_digits_wert >= 10) {
digit_links = two_digits_wert / 10;
digit_right = two_digits_value – digit_links * 10;
} else {
digit_right = two_digits_value;
}

x = clear_all_seg();

//Output two numbers on a 7-segment display
if (one_segment == true) { //Show only, if only one digit is displayed
Print(mode_char);
delay(500);
}
digitalWrite(seg_pin_out + 1,HIGH);
Print(digit_links);
if (one_segment == true) {
delay(1000);
}
digitalWrite(seg_pin_out + 1,LOW);

digitalWrite(seg_pin_out,HIGH);

Print(digit_right);

if (one_segment == true) {
delay(1000);
}
digitalWrite(seg_pin_out,LOW);

//empty or. Point
if (one_segment == true) {
digitalWrite(seg_pin_out,HIGH);
Print(31);
delay(1000);
}
}

int clear_all_seg() {
digitalWrite(seg_pin[0] ,LOW);
digitalWrite(seg_pin[1] ,LOW);
digitalWrite(seg_pin[2] ,LOW);
digitalWrite(seg_pin[3] ,LOW);
digitalWrite(seg_pin[4] ,LOW);
digitalWrite(seg_pin[5] ,LOW);
}

int get_time(char mode[]) {
//correction routine, because the Arduino UNO about after 50 Days the millis() on 0 resets
//time_correction_millis = check_if_reset_time(time_in_milliseconds);
time_in_milliseconds = millis() + time_in_millis(plus_hours,”hours”) + time_in_millis(plus_minutes,”minutes”) + time_in_millis(plus_seconds,”seconds”) + plus_milliseconds + time_correction_millis; // Load the current processor time incl. correction
hours = convert_time(time_in_milliseconds,”24_hours”);
minutes = convert_time(time_in_milliseconds,”60_minutes”);
seconds = convert_time(time_in_milliseconds,”60_seconds”);

return hours;
}

unsigned long time_in_millis(long time_value, char calc_modus[]) {
//factor for conversion
long t_millis = 0; //Return same value by default

if (calc_modus == “hours”) {
// Convert hours to milliseconds
t_millis = time_value * 60 * 60 * 1000;
}
if (calc_modus == “minutes”) {
// Convert minutes to milliseconds
t_millis = time_value * 60 * 1000;
}
if (calc_modus == “seconds”) {
// Convert minutes to milliseconds
t_millis = time_value * 1000;
}

return t_millis;
}

unsigned int convert_time(unsigned long t_millis,char conv_modus[]) {
//Convert a time in milliseconds to mode: Tage ‘days’, hours’ ’24_hours’, minutes’ ’60_minutes’, seconds’ ’60_seconds’, ‘millis’ ‘1000_millis’
// 60_ or. 1000_ returns only the time specification, which is correct for a watch, also z.B. seconds between 0 – 59

unsigned long days_tmp;
unsigned long hours_tmp;
unsigned long minutes_tmp;
unsigned long seconds_tmp;
unsigned long conv_return; //return value

// hours per day max. 23 (:59:59:999)
if (conv_modus == “hours”) {
conv_return = t_millis / 1000 / 60 / 60;
//return 17; //return h
return conv_return;
}

if (conv_modus == “minutes”) {
conv_return = t_millis / 1000 / 60;
//return 21; //m bzw. Return n for minutes
return conv_return;
}

if (conv_modus == “seconds”) {
conv_return = t_millis / 1000;
//return 26; //s for seconds
return conv_return;
}

if (conv_modus == “millis”) {
return t_millis;
}

if (conv_modus == “days”) {
conv_return = t_millis / 24 / 1000 / 60 / 60;
return conv_return;
}

if (conv_modus == “24_hours”) { //Returns only the current hour
days_tmp = convert_time(t_millis, “days”);
conv_return = t_millis – days_tmp * 24 * 1000 * 60 * 60;
conv_return = convert_time(conv_return,”hours”);
return conv_return;
}

if (conv_modus == “60_minutes”) { //Return only the current minutes
hours_tmp = convert_time(t_millis, “hours”);
conv_return = t_millis – hours_tmp * 1000 * 60 * 60;
conv_return = convert_time(conv_return,”minutes”);
return conv_return;
}

if (conv_modus == “60_seconds”) {
minutes_tmp = convert_time(t_millis,”minutes”);
conv_return = t_millis – minutes_tmp * 1000 * 60;
conv_return = convert_time(conv_return, “seconds”);
return conv_return;
}

if (conv_modus == “1000_millis”) {
seconds_tmp = convert_time(t_millis,”seconds”);
conv_return = t_millis – seconds_tmp * 1000;
conv_return = convert_time(conv_return, “millis”);
return conv_return;
}

return 14; // E for error / output errors

}

unsigned long check_if_reset_time (unsigned long last_time_in_millis){
unsigned long time_correction = 0;

if (last_time_in_millis > millis()) { //If Millis up 0 deferred
//Enhancement Notice: There could be a difference in the millisecond range!!!!
time_correction = get_day_millis(last_time_in_millis);

//correction times 0 reset, because the correction is included in the time_correction
plus_hours = 0;
plus_minutes = 0;
plus_seconds = 0;
plus_milliseconds = 0;

return time_correction;

} else {
return time_correction_millis; //If no correction, then return existing correction as value
}
}

unsigned long get_day_millis(unsigned long t_millis_total){
// Eliminate Full Days 86 400 000 milliseconds
long millis_pro_tag = 24 * 60 * 60 * 1000;
int whole_days = t_millis_total / millis_pro_tag – 0.5; //Round to whole days, 40.5 days would 41, also – 0.5. 40 days will be like this 39.5 and will up 40 rounded up
long millis_whole_days = whole_days * millis_pro_tag;
return (t_millis_total – millis_whole_days); //Return time in milliseconds of the day started

}

boolean key_pressed(int k_pin){
if (digitalRead(k_pin) == LOW) { //Button is not pressed
if (count > 200) {
count = 0;
stay_on = 0;
}

} else { //If button is pressed, Pin key[] is HIGH / 5 Volt
stay_on = 1;
count = count + 1;
time_reset_key = millis();
}

if (time_reset_key + 1000 < millis()) {
count = 0;
stay_on = 0;
digitalWrite(3, LOW);
}

if (stay_on == 2) {
digitalWrite(3, HIGH);
return false; //If button already pressed, Suppress bounce and return False
}

if (stay_on == 1) {
//digitalWrite(2, HIGH);
stay_on = 2;
return true;
} else {
//digitalWrite(2, LOW);
return false;
}

}

int get_analog_value() {
analog_value = analogRead(key_analog_pin); // Are 1024 values ​​back (0 no electricity – 1023 5V)
return analog_value; //Return value for shortcut, but is also made available globally in analog_value

}

int print_to_display(long output_value){
//use Print_Two_Digits to print all of them 6 Digits
//Print_Two_Digits(hours, “hours”, false);
//Print_Two_Digits(minutes, “minutes”, false);
//Print_Two_Digits(seconds, “seconds”, false);

int right_two_digits;
int mid_two_digits;
int left_two_digits;

//isolate the left two digits
x = output_value / 100;
right_two_digits = output_value – x * 100;

//isolate the right two digits
left_two_digits = output_value / 10000;

//isolate remainder = middle two digits
mid_two_digits = output_value – left_two_digits * 10000 – right_two_digits; //e.g. 3200 for 32, still have to go through 100 to be shared
mid_two_digits = mid_two_digits / 100;

//Print_Two_Digits(left_two_digits, “hours”, false);
//Print_Two_Digits(mid_two_digits, “minutes”, false);
//Print_Two_Digits(right_two_digits, “seconds”, false);

Print_Two_Digits(left_two_digits, “hours”, false);
Print_Two_Digits(mid_two_digits, “minutes”, false);
Print_Two_Digits(right_two_digits, “seconds”, false);

}

int change_analog_key_status() {

//Prevent double attacks, if briefly analog_value == 0
if (analog_value == 0) {
if (stay_on < 2 && stay_on > 0) {
delay(200);
stay_on = stay_on + 1;
return;
} else {
stay_on = 0;
}
}

//Falls SET-Taste, then change mode
if (analog_value > 1005 && stay_on == 0) {
stay_on = 1;
display_mode = display_mode + 1;
if (display_mode > 5) {
display_mode = 0;
}
}

//Will be changed depending on the SET mode
if (analog_value > 600 && analog_value < 800 && stay_on == 0) {
stay_on = 1;

//correct time
if (display_mode == 1) {
plus_hours = plus_hours + 1;
delay(200);
if (plus_hours >= 23) {
plus_hours = 0;
}
}
if (display_mode == 2) {
plus_minutes = plus_minutes + 1;
delay(200);
if (plus_minutes >= 59) {
plus_minutes = 0;
}
}
if (display_mode == 3) {
plus_seconds = plus_seconds + 1;
delay(200);
if (plus_seconds >=59) {
plus_seconds = 0;
}
}
}
}

int scale_temperature() {
//values ​​can be scaled here

int temp;
temp = analog_value / 3;
return temp;
}

//———————————————————- end functions —————————————————