I will try my best but remember this is very very new for me. I am just reading and piecing code together to get it working so I can expand from there. The goal is to have it work like below.
- When the arduino boots up my 7 segment display will display all zeros.
- Press the push button on the rotary to allow editing.
- Once the button is pressed I can count up or down on the display one millisecond at a time.
- Press the rotary again to lock the numbers and not allow any more changes unless I press the button again (one press to start editing, second press to lock changes).
dont laugh at my butchered code.
const int rotary_CLK = 3;
const int rotary_DT = 2;
const int rotary_SW = 0; // rotary push button
int rotary_state = 0;
int rotary_counter = 0;
volatile int current_time = 0;
unsigned long prev = 0;
unsigned long waitmilli = 0;
int time_counter = 0;
unsigned long start = millis();
char display[4];
int disp_count[4];
int display_counter = 0;
const int SCLK_pin = 10;
const int RCLK_pin = 11;
const int DIO_pin = 12;
void rotary_interrupts()
{
if(rotary_counter == 1) // no press no change
{
Serial.print("rotary_counter interrupt(): ");
Serial.println(rotary_counter);
static unsigned long last_time = 0;
unsigned long current_time = millis();
if (current_time - last_time > 5)
{
if (digitalRead(rotary_DT) == LOW)
{
current_time = current_time + 1;
}
else
{
current_time = current_time - 1;
}
current_time = min(1000, max(0, current_time));
last_time = current_time;
}
}
void showDisplay()
{
for (int i = 0; i < 4; i++)
{
int val = disp_c[i];
if ((val >= 32) && (val <= 47))
{
switch (val)
{
case 45 : val = 20;
break;
default : val = 20;
break;
}
}
else if ((val >= 48) && (val <= 57)) //0-9
{
val -= 48;
}
disp_count[i] = val;
}
}
void showCounter()
{
showDisplay();
for (int i = 0; i < 4; i++)
{
setDigits(i, disp_count[i]);
}
}
void setDigits(int dig, int character)
{
int digits[] = {128, 64, 32, 16, 8, 4, 2, 1};
int characters[] = {3, 159, 37, 13, 153, 73, 65, 31, 1, 9, 2, 158, 36, 12, 152, 72, 64, 30, 0, 8, 255};
digitalWrite(RCLK_pin, LOW);
shiftOut(DIO_pin, SCLK_pin, LSBFIRST, characters[character]);
shiftOut(DIO_pin, SCLK_pin, LSBFIRST, digits[dig]);
digitalWrite(RCLK_pin, HIGH);
}
void showText(char a, char b , char c, char d)
{
display[0] = d;
display[1] = c;
display[2] = b;
display[3] = a;
}
void setup()
{
Serial.begin(9600);
pinMode(rotary_CLK, INPUT);
pinMode(rotary_DT, INPUT);
pinMode(rotary_SW, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(rotary_CLK), rotary_interrupts, LOW);
if(rotary_state == LOW)
{
Serial.print("rotary_state setup(): ");
Serial.println(rotary_state);
if(rotary_counter >= 2) rotary_counter = 0; // reset the counter back to zero
rotary_counter++;
delay(1000);
}
}
void loop()
{
showCounter();
if(Rotary_Push_Button_Counter == 2)
{
rotary_counter = 0;
Serial.print("rotary_counter loop(): ");
Serial.println(rotary_counter);
if( millis() > (prev + waitmilli))
{
showText(' ', (time_counter / 1000) % 10, (time_counter / 100) % 10 + 10, (time_counter / 10) % 10);
display_counter--;
}
}
Appreciate any help, its always good to learn proper coding techniques so this is my start.
Thanks