Viking Skull Lamp  V1.0.1
Loading...
Searching...
No Matches
motorControl.cpp
Go to the documentation of this file.
1/*
2 * Created on May 28 2022
3 *
4 * Copyright (c) 2022 - Daniel Hajnal
5 * hajnal.daniel96@gmail.com
6 * This file is part of the Viking Skull Lamp project.
7 * Modified 2022.06.27
8*/
9
10/*
11MIT License
12Copyright (c) 2022 Daniel Hajnal
13Permission is hereby granted, free of charge, to any person obtaining a copy
14of this software and associated documentation files (the "Software"), to deal
15in the Software without restriction, including without limitation the rights
16to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
17copies of the Software, and to permit persons to whom the Software is
18furnished to do so, subject to the following conditions:
19The above copyright notice and this permission notice shall be included in all
20copies or substantial portions of the Software.
21THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
27SOFTWARE.
28*/
29
30#include "motorControl.hpp"
31
32// Set the front state to unknown by default.
34
35void motorInit(){
36
37 // Set the endstop pins to input.
38 pinMode( FRONT_ENDSTOP, INPUT );
39 pinMode( BACK_ENDSTOP, INPUT );
40
41 // Set the motor pin to output.
42 pinMode( MOTOR, OUTPUT );
43
44 // Disable the motor.
45 digitalWrite( MOTOR, 0 );
46
47 // Check if the front state can be detected at startup.
48 if( digitalRead( FRONT_ENDSTOP ) == 0 ){
49
52 Serial.println( F( "Front opened at startup!" ) );
53
54 }
55
56 else if( digitalRead( BACK_ENDSTOP ) == 0 ){
57
60 Serial.println( F( "Front closed at startup!" ) );
61
62 }
63
64 else{
65
66 closeFront();
67 Serial.println( F( "Front state unknown! Closing..." ) );
68
69 }
70
71}
72
73void openFront(){
74
75 // Store the system time, when the procedure started.
76 long timerStart;
77
78 // Turn on the motor with the MOTOR_PWM power.
79 analogWrite( MOTOR, MOTOR_PWM );
80
81 // Save system time.
82 timerStart = millis();
83
84 // The endstps has inverted logic. If the
85 // endstop is pressed, the output of it
86 // will be low. Wait until the front endstop
87 // is high.
88 while( digitalRead( FRONT_ENDSTOP ) == 1 ){
89
90 // Check for timeout.
91 if( ( millis() - timerStart ) > OPEN_TIMEOUT ){
92
93 // If timeout occured, stop the motor and return.
94 digitalWrite( MOTOR, 0 );
95
96 // Also set state to unknown.
99 return;
100
101 }
102
103 }
104
105 // If the endstop has triggered, we have to stop the motor.
106 digitalWrite( MOTOR, 0 );
107
108 // Turn on the display.
110
111 // Save the state.
113
114}
115
117
118 // Store the system time, when the procedure started.
119 long timerStart;
120
121 // Turn on the motor with the MOTOR_PWM power.
122 analogWrite( MOTOR, MOTOR_PWM );
123
124 // Save system time.
125 timerStart = millis();
126
127 // The endstps has inverted logic. If the
128 // endstop is pressed, the output of it
129 // will be low. Wait until the back endstop
130 // is high.
131 while( digitalRead( BACK_ENDSTOP ) == 1 ){
132
133 // Check for timeout.
134 if( ( millis() - timerStart ) > CLOSE_TIMEOUT ){
135
136 // If timeout occured, stop the motor and return.
137 digitalWrite( MOTOR, 0 );
138
139 // Also set state to unknown.
142 return;
143
144 }
145
146 }
147
148 // If the endstop has triggered, we have to stop the motor.
149 digitalWrite( MOTOR, 0 );
150
151 // Save the state.
153
154 // Turn off the display.
156
157}
void displayOff()
Turn off the oled panel.
Definition: oled.cpp:210
void displayOn()
Turn on the oled panel.
Definition: oled.cpp:204
ssd1306 display
Object for the display.
Definition: main.cpp:47
void closeFront()
Close the font.
enum frontState_t frontState
Store the state of the front panel.
void motorInit()
Initialize the motor.
void openFront()
Open the font.
#define FRONT_ENDSTOP
The front endstop is connected to pin 7 on the Arduino.
#define BACK_ENDSTOP
The back endstop is connected to pin 7 on the Arduino.
#define MOTOR_PWM
The motor power can be set with this definition.
#define OPEN_TIMEOUT
Maximum allowed time in ms, to open the front.
#define CLOSE_TIMEOUT
Maximum allowed time in ms, to close the front.
#define MOTOR
The motor is connected to pin 5 on the Arduino.
frontState_t
States of the front panel.
@ FRONT_OPEN
Front is opened.
@ FRONT_CLOSE
Front is closed.
@ FRONT_UNKNOWN
Front state is unknown( for example at startup ).