-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathregulacja-swiatla-auto.ino
143 lines (122 loc) · 3.26 KB
/
regulacja-swiatla-auto.ino
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
struct NUMBERS{
bool led[10][8] = { {1,1,1,1,1,1,0,0},//0
{0,0,0,0,1,1,0,0},//1
{1,1,0,1,1,0,1,0},//2
{1,0,0,1,1,1,1,0},//3
{0,0,1,0,1,1,1,0},//4
{1,0,1,1,0,1,1,0},//5
{1,1,1,1,0,1,1,0},//6
{0,0,0,1,1,1,0,0},//7
{1,1,1,1,1,1,1,0},//8
{1,0,1,1,1,1,1,0}};//9
int pin[8] = {PA0, PA1, PA2, PA3, PA4,PA5, PA6, PA7};
int button1 = PA8;
int button2 = PA9;
int output = PB6;// output voltage for carlight.
int number = 0;// level of lights
int lastNumber = 0;
void setPinMode(){ // basic setting of pinout
for ( int c = 0; c < 8; c++ ){
pinMode(pin[c], OUTPUT);
}
pinMode(button1, INPUT_PULLUP);
pinMode(button2, INPUT_PULLUP);
pinMode(output, PWM);
}
void checkButtonState(){
if ( digitalRead(button1) == 0){
number++;
}
else if( digitalRead(button2) == 0 ){
number--;
}
while ( digitalRead(button1) == 0 || digitalRead(button2) == 0 ){
crasyLogo( 1 );
}
if ( number > 9 ){ // seting range and make loop
number = 0;
}
if ( number < 0 ){
number = 9;
}
}
void displayAll(){ // Testing display if numbers are properly displayed
for ( int i = 0; i < 10; i++ ){
for ( int a = 0; a < 15; a++){
for ( int c = 0; c < 8; c++){
digitalWrite(pin[c], led[i][c] );
delay(1);
digitalWrite(pin[c], 0 );
delay(1);
}
}
}
}
void display(){ // display actual level of lights
cleanDisplay();
for ( int c = 0; c < 8; c++){
digitalWrite(pin[c], led[number][c] );
delay(1);
digitalWrite(pin[c], 0 );
delay(1);
}
}
void cleanDisplay(){
for ( int c = 0; c < 8; c++){
digitalWrite(pin[c], 0);
}
}
bool numberChanged(){
if ( number != lastNumber ){
return true;
lastNumber = number;
}
else {
return false;
}
}
void setOutput(){
if ( numberChanged() ){
int out = 0;
int oneStep = 65535 / 11;
out = oneStep * (number + 2);
pwmWrite(output, out);
}
if ( number == 0 ){ // sequring output when level is 0.
pwmWrite(output, 13106); // for some reason 0 dont update and this is manual corecion
} // Car lights when see 0V thinks that conection is brake, and is not
//updating level of lights. Making 10 steps, and shift number in 1 place fixt that problem.
}
void initiateLights(){
number = 9;
setOutput();
crasyLogo( 20 );
number = 0;
setOutput();
}
void crasyLogo( int howMany ){
for ( int a = 0; a < howMany; a++){
for ( int c = 0; c < 8; c++){
digitalWrite(pin[c], led[8][c] );
delay(10);
digitalWrite(pin[c], 0 );
delay(10);
}
}
}
};
void setup() {
// put your setup code here, to run once:
NUMBERS numbers;
numbers.setPinMode();
numbers.displayAll();
numbers.initiateLights();
while(1){
numbers.checkButtonState();
numbers.display();
numbers.setOutput();
}
}
void loop() {
// put your main code here, to run repeatedly:
}