Newbie needs pointers.... :)
Hello friends.
This is my first C program, and I have been plugging at this for a while. I had it running perfect in Basic, but C is more complicated and I'm making slow progress. I would like comments from the pros. This part of the program is to control an LED display, with last position in eeprom, to be reloaded upon startup.
I have problem with the counter tc going below 0 and over 16, despite having put limits on tc. I also have problem with the delay loop, which I should put in a subroutine because I need to reuse it for every position. I call each display updates separately because it was easier to debug. Are there obvious flaws, major mistakes to correct? I'm not looking for a total debug, just a few pointers to guide me in the right direction. I need to complete this project soon, so any help will be greatly rewarded with emoticons and future consideration... :)
Obviously not a pro job, but as long as it works in the end, I'll be happy.
There's plenty of time for me to learn to be more proficient in C, since this is my first one.
Thanks for your patience.
Robert
Code:
#include <EEPROM.h>
#include <ExtraOutputs.h>
const int buttonPin3 = 3;
const int buttonPin4 = 4;
const int ledPin11 = 11;
const int ledPin10 = 10;
//__________display connections_________________
const int latchPin = 17;
const int clockPin =15;
const int dataPin = 16;
const int numberOfChips = 3;
ExtraOutputs eo(latchPin, clockPin);
int button3 = 0;
int button4 = 0;
word tick1;
int tc;
int oldtc;
int i;
int j;
int d;
void setup() {
pinMode(ledPin11, OUTPUT);
pinMode(ledPin10, OUTPUT);
pinMode(buttonPin3, INPUT);
pinMode(buttonPin4, INPUT);
Serial.begin(9600);
//___________read eeprom for last tc position__________
oldtc = EEPROM.read(10);
tc = oldtc;
Serial.println(tc, DEC);
Serial.print("tc");
//_________update display__________
do
{
if (d == 1) eo.digitalWrite(8, HIGH);
if (d == 2) eo.digitalWrite(9, HIGH);
if (d == 3) eo.digitalWrite(10, HIGH);
if (d == 4) eo.digitalWrite(11, HIGH);
if (d == 5) eo.digitalWrite(12, HIGH);
if (d == 6) eo.digitalWrite(13, HIGH);
if (d == 7) eo.digitalWrite(14, HIGH);
if (d == 8) eo.digitalWrite(15, HIGH);
if (d == 9) eo.digitalWrite(0, HIGH);
if (d == 10) eo.digitalWrite(1, HIGH);
if (d == 11) eo.digitalWrite(2, HIGH);
if (d == 12) eo.digitalWrite(3, HIGH);
if (d == 13) eo.digitalWrite(4, HIGH);
if (d == 14) eo.digitalWrite(5, HIGH);
if (d == 15) eo.digitalWrite(6, HIGH);
if (d == 16) eo.digitalWrite(7, HIGH);
delay (50);
d++;
} while (d <= tc); // here I have problem with integer and pointer conflict
}
void loop()
{
// The eeprom works fine, but loops every time tc changes, I would prefer it only
// updates after the delay. A pointer would be preferable here, to have the
//eeprom outside the loop combined with the delay routine.
mem_write:
{
oldtc = EEPROM.read(10);
if (tc != oldtc)
{
EEPROM.write(10, tc);
Serial.println("eeprom");
}
}
//___________ read switches______________________
button3 = digitalRead(buttonPin3);
button4 = digitalRead(buttonPin4);
//____________both switches off___________________
//I'm working on this delay so it's not ready yet.
if (button3 == 0 && button4 == 0)
{
int x,y;
for(x = 0; x < 2000; x++)
{
for(y = 0; y < 2000000; y++)
{
}
//__________going up display____________________________
// this works fine
if (button3 == HIGH)
{
digitalWrite(ledPin11, HIGH);
tc = tc + 1;
Serial.println(tc, DEC);
if (tc >= 16) tc == 16;
if (tc == 1) eo.digitalWrite(8, HIGH);
if (tc == 2) eo.digitalWrite(9, HIGH);
if (tc == 3) eo.digitalWrite(10, HIGH);
if (tc == 4) eo.digitalWrite(11, HIGH);
if (tc == 5) eo.digitalWrite(12, HIGH);
if (tc == 6) eo.digitalWrite(13, HIGH);
if (tc == 7) eo.digitalWrite(14, HIGH);
if (tc == 8) eo.digitalWrite(15, HIGH);
if (tc == 9) eo.digitalWrite(0, HIGH);
if (tc == 10) eo.digitalWrite(1, HIGH);
if (tc == 11) eo.digitalWrite(2, HIGH);
if (tc == 12) eo.digitalWrite(3, HIGH);
if (tc == 13) eo.digitalWrite(4, HIGH);
if (tc == 14) eo.digitalWrite(5, HIGH);
if (tc == 15) eo.digitalWrite(6, HIGH);
if (tc == 16) eo.digitalWrite(7, HIGH);
delay(300);
digitalWrite(ledPin11, LOW);
}
//_________going down display__________________
// this works fine too
if (button4 == HIGH)
{
digitalWrite(ledPin10, HIGH);
tc = tc - 1;
Serial.println(tc, DEC);
if (tc <= 0) tc == 0;
if (tc == 15) eo.digitalWrite(7, LOW);
if (tc == 14) eo.digitalWrite(6, LOW);
if (tc == 13) eo.digitalWrite(5, LOW);
if (tc == 12) eo.digitalWrite(4, LOW);
if (tc == 11) eo.digitalWrite(3, LOW);
if (tc == 10) eo.digitalWrite(2, LOW);
if (tc == 9) eo.digitalWrite(1, LOW);
if (tc == 8) eo.digitalWrite(0, LOW);
if (tc == 7) eo.digitalWrite(15, LOW);
if (tc == 6) eo.digitalWrite(14, LOW);
if (tc == 5) eo.digitalWrite(13, LOW);
if (tc == 4) eo.digitalWrite(12, LOW);
if (tc == 3) eo.digitalWrite(11, LOW);
if (tc == 2) eo.digitalWrite(10, LOW);
if (tc == 1) eo.digitalWrite(9, LOW);
if (tc == 0) eo.digitalWrite(8, LOW);
delay(300);
digitalWrite(ledPin10, LOW);
}
}