Conway6288:
I have copied the code from a book that I have (Which is where this project originated)
I just changed the pins on it to suit where mine have gone. Which is probably where the problem lies. I tried it before changing the pins and the display flashed but nothing else. When I changed the pins to what they are on my board there is nothing on the display. I think the code is pretty old and I probably have the encoder hooked up wrong.
Just a hint to help you with code questions … when you post a port of code, highlight it and then click on the Code box. It’ll put your code in a box, like a quote box, making it much easier to read.
ie - from your post above …
Here’s the code -
#include <EEPROM.h>
int address = 0;
byte selectedTimeIndex;
int aPin = A5;
int bPin = 13;
int buttonPin = 2;
int segmentPins[] = {A1,3,4,5,A0,7,8};
int displayPins[] = {11,10,9,6};
int times [] = {5,10,15,20,30,45,100,130,200,230,300,400,500,600,700,800,900,1000,1500,2000,3000};
int numTimes = 19;
int timerMinute;
int timerSecond;
int buzzerPin = 12;
boolean stopped = true;
byte digits [10][8] = {
// a b c d e f g .
(1,1,1,1,1,1,0,0), //0
(0,1,1,0,0,0,0,0), //1
(1,1,0,1,1,0,1,0), //2
(1,1,1,1,0,0,1,0), //3
(0,1,1,0,0,1,1,0), //4
(1,0,1,1,0,1,1,0), //5
(1,0,1,1,1,1,1,0), //6
(1,1,1,0,0,0,0,0), //7
(1,1,1,1,1,1,1,0), //8
(1,1,1,1,0,1,1,0) //9
};
void setup()
{
for (int i=0; i < 8; i++)
{
pinMode(segmentPins[i], OUTPUT);
}
for (int i=0; i < 4; i++)
{
pinMode(displayPins[i], OUTPUT);
}
pinMode(buzzerPin, OUTPUT);
pinMode(buttonPin, INPUT);
pinMode(aPin, INPUT);
pinMode(bPin, INPUT);
selectedTimeIndex = EEPROM.read(address);
timerMinute = times[selectedTimeIndex] / 100;
timerSecond = times[selectedTimeIndex] % 100;
}
void loop()
{
selectedTimeIndex = EEPROM.read(address);
if (digitalRead(buttonPin))
{
stopped = ! stopped;
digitalWrite(buzzerPin, LOW);
while (digitalRead(buttonPin)) {};
EEPROM.write(address, selectedTimeIndex);
}
updateDisplay();
}
void updateDisplay() //mmss
{
int minsecs = timerMinute * 100 + timerSecond;
int v = minsecs;
for (int i = 0; i < 4; i++)
{
int digit = v % 10;
setDigit(i);
setSegments(digit);
v = v/10;
process();
}
setDigit(5); // all digits off to prevent uneven illumination
}
void process()
{
for (int i = 0; i <100; i++) //tweak this number between flicker and blur
{
int change = getEncoderTurn();
if(stopped)
{
changeSetTime(change);
}
else
{
updateCountingTime();
}
}
if (timerMinute == 0 && timerSecond == 0)
{
digitalWrite(buzzerPin, HIGH);
}
}
void changeSetTime(int change)
{
selectedTimeIndex += change;
if(selectedTimeIndex < 0)
{
selectedTimeIndex = numTimes;
}
else if (selectedTimeIndex > numTimes)
{
selectedTimeIndex =0;
}
timerMinute = times[selectedTimeIndex] /100;
timerSecond = times[selectedTimeIndex] %100;
}
void updateCountingTime()
{
static unsigned long lastMillis;
unsigned long m= millis();
if (m> (lastMillis + 1000) && (timerSecond > 0 || timerMinute >0))
{
digitalWrite(buzzerPin, HIGH);
delay(10);
digitalWrite(buzzerPin, LOW);
if (timerSecond == 0)
{
timerSecond = 59;
timerMinute = 59;
}
else
{
timerSecond = 59;
}
lastMillis = m;
}
}
void setDigit (int digit)
{
for (int i = 0; i < 4; i++)
{
digitalWrite(displayPins[i], (digit != i));
}
}
void setSegments (int n)
{
for (int i = 0; i < 8; i++)
{
digitalWrite(segmentPins[i], ! digits [n] [i]);
}
}
int getEncoderTurn()
{
//return -1, 0 or +1
static int oldA = LOW;
static int oldB = LOW;
int result = 0;
int newA = digitalRead(aPin);
int newB = digitalRead(bPin);
if (newA != oldA || newB != oldB)
{
//something has changed
if (oldA == LOW && newA == HIGH)
{
result = -(oldB * 2 - 1);
}
}
oldA = newA;
oldB = newB;
return result;
}
Notice how it’s all nicely indented now.