-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
153 lines (148 loc) · 4.79 KB
/
main.c
File metadata and controls
153 lines (148 loc) · 4.79 KB
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
// In this code, the main program includes the necessary libraries for the accelerometer, LEDs, button, timer, CS43L22 audio DAC,
// I2C, DMA, and I2S interfaces. It also defines the threshold and deadzone angles for each LED, the low-pass filter coefficient,
// the maximum angle of tilt in any direction, and the audio parameters (sample rate, bits per sample, channels, and volume).
// The setup function initializes the accelerometer, LEDs, button, timer, I2C, CS43L22, DMA, and I2S interfaces, sets the audio buffer
// for the DMA, and starts the audio output. The loop function continuously reads and processes the accelerometer values, detects and
// handles the button press, switches between the tilt switch and LED blinking modes, and plays the "beep" sound every time the mode is
// switched from the tilt switch to the LED blinking mode.
// Define the threshold angles for each LED
#define LED1_THRESHOLD 10
#define LED2_THRESHOLD 100
#define LED3_THRESHOLD 170
// Define the deadzone range for each LED
#define LED1_DEADZONE 5
#define LED2_DEADZONE 10
#define LED3_DEADZONE 5
// Define the low-pass filter coefficient
#define LPF_COEFF 0.8
// Define the maximum angle of tilt in any direction
#define MAX_ANGLE 180
// Define the audio parameters
#define SAMPLE_RATE 44100
#define BITS_PER_SAMPLE 16
#define CHANNELS 2
#define VOLUME 50
// Variables to store the accelerometer values
float x_accel, y_accel, z_accel;
// Variables to store the angle of tilt
float x_angle, y_angle;
// Variables to store the filtered accelerometer values
float x_accel_filtered, y_accel_filtered, z_accel_filtered;
// Variables to store the filtered angle of tilt
float x_angle_filtered, y_angle_filtered;
// Variable to track the current mode (tilt switch or LED blinking)
int mode = 0;
// Audio buffer
int16_t audio_buffer[BUFFER_SIZE];
void setup()
{
// Initialize the accelerometer
initAccelerometer();
// Initialize the LEDs
initLEDs();
// Initialize the button
initButton();
// Initialize the timer
initTimer();
// Initialize the I2C interface
initI2C();
// Initialize the CS43L22
initCS43L22(SAMPLE_RATE, BITS_PER_SAMPLE, CHANNELS, VOLUME);
// Initialize the DMA and I2S interfaces
initDMA();
initI2S();
// Set the audio buffer for the DMA
setAudioBuffer(audio_buffer);
// Start the audio output
startAudioOutput();
}
void loop()
{
// Read the accelerometer values
x_accel = readAccelerometerX();
y_accel = readAccelerometerY();
z_accel = readAccelerometerZ();
// Calculate the angle of tilt in each axis
x_angle = atan(x_accel / sqrt(y_accel*y_accel + z_accel*z_accel)) * 180 / PI;
y_angle = atan(y_accel / sqrt(x_accel*x_accel + z_accel*z_accel)) * 180 / PI;
// Apply the low-pass filter to the accelerometer values
x_accel_filtered = x_accel_filtered * LPF_COEFF + (1 - LPF_COEFF) * x_accel;
y_accel_filtered = y_accel_filtered * LPF_COEFF + (1 - LPF_COEFF) * y_accel;
z_accel_filtered = z_accel_filtered * LPF_COEFF + (1 - LPF_COEFF) * z_accel;
// Calculate the filtered angle of tilt in each axis
x_angle_filtered = atan(x_accel_filtered / sqrt(y_accel_filtered*y_accel_filtered + z_accel_filtered*z_accel_filtered)) * 180 / PI;
y_angle_filtered = atan(y_accel_filtered / sqrt(x_accel_filtered*x_accel_filtered + z_accel_filtered*z_accel_filtered)) * 180 / PI;
// Check if the board is tilted in the direction of LED1
if (x_angle_filtered > LED1_THRESHOLD && x_angle_filtered < MAX_ANGLE - LED1_DEADZONE)
{
// Turn on LED1
turnOnLED(1);
}
else
{
// Turn off LED1
turnOffLED(1);
}
// Check if the board is tilted in the direction of LED2
if (y_angle_filtered > LED2_THRESHOLD && y_angle_filtered < MAX_ANGLE - LED2_DEADZONE)
{
// Turn on LED2
turnOnLED(2);
}
else
{
// Turn off LED2
turnOffLED(2);
}
// Check if the board is tilted in the direction of LED3
if (y_angle_filtered < -LED3_THRESHOLD && y_angle_filtered > -(MAX_ANGLE - LED3_DEADZONE))
{
// Turn on LED3
turnOnLED(3);
}
else
{
// Turn off LED3
turnOffLED(3);
}
// Check if the button has been pressed
if (isButtonPressed())
{
// Switch between the tilt switch and LED blinking modes
if (mode == 0)
{
// Switch to LED blinking mode
mode = 1;
// Play the "beep" sound to indicate that the system is paused
playBeep();
}
else
{
// Switch to tilt switch mode
mode = 0;
}
}
// Check the current mode
if (mode == 0)
{
// Tilt switch mode
// Continue reading and processing the accelerometer values
}
else
{
// LED blinking mode
// Blink the LEDs in the specified pattern
if (getTime() % 1.5 < 1)
{
// Turn on LED1 and LED2
turnOnLED(1);
turnOnLED(2);
}
else
{
// Turn on LED3 and LED4
turnOnLED(3);
turnOnLED(4);
}
}
}