-
Notifications
You must be signed in to change notification settings - Fork 101
/
sketch.ino
97 lines (89 loc) · 1.95 KB
/
sketch.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
// clockPin
#define SRCLK 12
// data pin
#define SER 11
int offOn = 0;
bool switchPressed = false;
int count = 0;
void setup()
{
Serial.begin (115200);
Serial.println ( "Open ShiftRegister.txt project" );
pinMode (10,INPUT);
digitalWrite (10,1); // pull-up resistor set
pinMode (SRCLK,OUTPUT); // Shift Register Clock
digitalWrite (SRCLK,0); // No-Shift
pinMode (SER,OUTPUT); // Shift Register Data
digitalWrite (SER,0); // Clear Data
}
void shiftOut_X(uint8_t dataPin, uint8_t clockPin, uint8_t bitOrder, uint8_t NofRegisters, uint8_t *val)
{
uint8_t i;
uint8_t i1;
uint8_t Sbyte;
uint8_t shiftNo;
if (bitOrder == LSBFIRST)
{
shiftNo = 0;
Sbyte = *(val + shiftNo);
for (i1 = 0; i1 < NofRegisters; i1++)
{
for (i = 0; i < 8; i++)
{
digitalWrite(dataPin, !!(Sbyte & (1 << i)));
digitalWrite(clockPin, HIGH);
digitalWrite(clockPin, LOW);
}
Sbyte = *(val + (++shiftNo));
}
}
else if (bitOrder == MSBFIRST)
{
shiftNo = NofRegisters - 1;
Sbyte = *(val + shiftNo);
for (i1 = 0; i1 < NofRegisters; i1++)
{
for (i = 0; i < 8; i++)
{
digitalWrite(dataPin, !!(Sbyte & (1 << (7 - i))));
digitalWrite(clockPin, HIGH);
digitalWrite(clockPin, LOW);
}
Sbyte = *(val + (--shiftNo));
}
}
}
void loop ()
{
uint8_t val;
if (!digitalRead (10)) // Button Down
switchPressed = true;
else
{
if (switchPressed) // Button Up
{
if (!offOn)
{
count++;
val = count & 0xFF;
shiftOut_X ( SER, SRCLK, MSBFIRST, 1, &val);
/*
digitalWrite (SER,1);
Serial.println (++count);
digitalWrite (SRCLK,1); // Cause shift
*/
}
offOn = 1; // Only written once
}
else
{
if (offOn) // Only do this once
{
digitalWrite (SER,0);
digitalWrite (SRCLK,0);
}
offOn = 0;
}
switchPressed = false;
}
}