Changes
Page history
Update Overview with new firmware
authored
Mar 27, 2022
by
Reid Schneyer
Hide whitespace changes
Inline
Side-by-side
Test-Stand/Overview.md
View page @
c7d48f15
...
...
@@ -15,38 +15,88 @@ See here: https://git.ece.iastate.edu/danc/MicroCART/-/wikis/Test-Stand/Electron
# The Firmware
```
cpp
int
analogPin
=
A7
;
int
btnPin
=
2
;
const
int
DATA_PIN
=
A7
;
const
int
BUTTON_PIN
=
2
;
int
buttonState
=
0
;
int
val
=
0
;
double
scalar
=
0.3516
;
// 360/1024
const
double
TEN_BIT_SCALAR
=
0.3516
;
// 360/1024
int
homePosition
;
// Rate variables
long
lastTime
=
0
;
long
currTime
=
0
;
long
lastReading
=
0
;
long
currReading
=
0
;
long
deltaTime
=
0
;
long
deltaReading
=
0
;
double
rate
;
// Long Press Variables
long
buttonTimer
=
0
;
const
long
LONG_PRESS_MILLIS
=
250
;
boolean
buttonActive
=
false
;
boolean
longPressActive
=
false
;
boolean
isInPositionMode
=
true
;
void
setup
()
{
// put your setup code here, to run once:
Serial
.
begin
(
9600
);
pinMode
(
btnPin
,
INPUT
);
pinMode
(
BUTTON_PIN
,
INPUT
);
pinMode
(
LED_BUILTIN
,
OUTPUT
);
homePosition
=
analogRead
(
analogPin
);
homePosition
=
analogRead
(
DATA_PIN
);
lastTime
=
millis
();
lastReading
=
analogRead
(
DATA_PIN
);
}
void
loop
()
{
// put your main code here, to run repeatedly:
buttonState
=
digitalRead
(
btnPin
);
if
(
buttonState
==
HIGH
){
digitalWrite
(
LED_BUILTIN
,
!
digitalRead
(
LED_BUILTIN
));
homePosition
=
analogRead
(
analogPin
);
}
val
=
analogRead
(
analogPin
);
double
absoluteDegrees
=
scalar
*
(
double
)
val
;
double
relativeDegrees
=
scalar
*
(
double
)
homePosition
;
double
displayPosition
=
absoluteDegrees
-
relativeDegrees
;
if
(
displayPosition
<
0
)
{
displayPosition
=
displayPosition
+
360
;
currTime
=
millis
();
currReading
=
analogRead
(
DATA_PIN
);
if
(
digitalRead
(
BUTTON_PIN
)
==
HIGH
){
if
(
buttonActive
==
false
){
// Button was not pressed last cycle
buttonActive
=
true
;
buttonTimer
=
millis
();
}
if
(((
millis
()
-
buttonTimer
)
>
LONG_PRESS_MILLIS
)
&&
(
longPressActive
==
false
)){
// Detected a long press, swap modes
longPressActive
=
true
;
isInPositionMode
=
!
isInPositionMode
;
}
}
else
{
if
(
buttonActive
==
true
){
// Button was being pressed last cycle
if
(
longPressActive
==
true
){
// Button was being long pressed last cycle, user has released the long press
longPressActive
=
false
;
}
else
{
// Short press occured, grab current position regardless of mode
homePosition
=
analogRead
(
DATA_PIN
);
}
buttonActive
=
false
;
}
}
// LED on if we're in position mode, off if we're in rate mode
digitalWrite
(
LED_BUILTIN
,
isInPositionMode
);
if
(
isInPositionMode
==
true
){
double
absoluteDegrees
=
TEN_BIT_SCALAR
*
(
double
)
currReading
;
double
relativeDegrees
=
TEN_BIT_SCALAR
*
(
double
)
homePosition
;
double
displayPosition
=
absoluteDegrees
-
relativeDegrees
;
displayPosition
+=
(
displayPosition
<
0
)
?
(
360
)
:
(
0
);
Serial
.
println
(
displayPosition
);
}
else
{
// We're in rate mode
deltaTime
=
currTime
-
lastTime
;
deltaReading
=
currReading
-
lastReading
;
rate
=
1000
*
((
TEN_BIT_SCALAR
*
deltaReading
)
/
(
deltaTime
));
Serial
.
println
(
rate
);
}
// Reset vars for next cycle
lastTime
=
currTime
;
lastReading
=
currReading
;
}
```
\ No newline at end of file