-
Notifications
You must be signed in to change notification settings - Fork 0
/
v_out.cpp
167 lines (152 loc) Β· 3.39 KB
/
v_out.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#include <iostream>
#include <cmath>
#include <string>
#include <chrono>
#include <thread>
#include <Windows.h>
#include <mmsystem.h>
#pragma comment(lib, "winmm.lib")
#define CYAN "\033[36m"
#define RED "\033[1;31m"
#define YELLOW "\033[1;33m"
#define RESET "\033[0m"
using namespace std;
using namespace std::this_thread; // sleep_for, sleep_until
using namespace std::chrono_literals; // ns, us, ms, s, h, etc
float R1;
float R2;
float vIn;
float vOut;
float value;
float rounded;
bool hasCalculated = false;
string answer;
string scale;
// ---> AUDIO
void musicIntro()
{
PlaySound(
TEXT("audio/MM_vout_music_intro"),
NULL,
SND_ASYNC);
}
void audioProgress(int i)
{
if (i < 1)
{
PlaySound(
TEXT("audio/MM_vout_processing"),
NULL,
SND_ASYNC);
}
}
void audioConfirm()
{
PlaySound(
TEXT("audio/MM_vout_nav_active"),
NULL,
SND_ASYNC);
}
// ---> MATH
float voltageDivider()
{
vOut = (R2 / (R1 + R2)) * vIn;
hasCalculated = true;
return vOut;
}
float nearestValue(float in)
{
value = (int)(in * 1000 + .5);
if (in < 1)
{
scale = "mV";
return value; // mV
}
else
{
scale = "V";
return (value / 1000); // V
}
}
// ---> CIRCUIT
void circuitDescription()
{
cout << CYAN << "Enter input voltage " << RESET << "(in volts): ";
cin >> vIn;
audioConfirm();
cout << RED << "Enter value for Resistor #1 " << RESET << "(in ohms): ";
cin >> R1;
audioConfirm();
cout << RED << "Enter value for Resistor #2 " << RESET << "(in ohms): ";
cin >> R2;
audioConfirm();
sleep_for(600ms);
}
void circuitDetails()
{
cout << endl;
cout << "Circuit Details:" << endl;
cout << "-----------------------------" << endl;
sleep_for(300ms);
cout << CYAN << "Input voltage (Vin) = " << vIn << " volts" << endl;
sleep_for(300ms);
cout << RED << "Resistor #1 (R1) = " << R1 << " ohms." << endl;
sleep_for(300ms);
cout << RED << "Resistor #2 (R2) = " << R2 << " ohms." << endl;
sleep_for(300ms);
rounded = nearestValue(vOut);
cout << CYAN << "Output voltage (Vout) = " << rounded << " " + scale << RESET << endl;
cout << "-----------------------------" << endl;
cout << endl;
}
// ---> ASCII
void animation()
{
char punc[7] = {':', ':', '.', '.', '.', '.', '.'};
int i;
cout << endl;
for (i = 0; i < 7; i++)
{
audioProgress(i);
sleep_for(150ms);
cout << punc[i];
}
cout << endl;
}
// ---> MAIN
int main()
{
if (hasCalculated == false)
{
musicIntro();
cout << YELLOW << endl;
cout << " /-------------------------/" << endl;
sleep_for(500ms);
cout << " / V-OUT - Voltage Divider /" << endl;
sleep_for(500ms);
cout << "/-------------------------/" << endl;
cout << RESET << endl;
sleep_for(1s);
}
circuitDescription();
voltageDivider();
animation();
sleep_for(300ms);
circuitDetails();
cout << "Would you like to make another calculation? (yes/no) ";
cin >> answer;
audioConfirm();
for (int i = 0; i < answer.length(); i++)
{
answer[i] = tolower(answer[i]);
}
if (answer == "yes")
{
main();
}
else
{
cout << endl;
cout << YELLOW << "V-OUT exited. Goodbye." << RESET << endl;
}
}