Go Down

Topic: Restarting with EEPROM data (Read 689 times) previous topic - next topic

jeeep

Good Morning/Afternoon/Evening,
  Below is a program that recalls info from the EEPROM. I need this data available after the power is cut and then restored to the Arduino. I am using shift registers and this is where the problem starts. After I cut the power to the Arduino and then hook it back up, the data is not correctly shifted out. However, if I cut the power and then hook it back up and then initiate the Serial Monitor the data is correctly shifted out. So my question is how do I get the Arduino to correctly shift the data out after the power has been cut and restored, without having to start the serial monitor? Thanks for any insight:

Code: [Select]


void setup() {
value1 = EEPROM.read(m_1);
value1b = value1 - 48;
       delay(100);
       Serial.begin (9600);
       Serial.print(value1b);
       shiftOut(dataPin, clockPin, BLANK);
       shiftOut(dataPin, clockPin, BLANK);
       shiftOut(dataPin, clockPin, Variable_a_[value1b]);
     shiftOut(dataPin, clockPin, Variable_b_[value1b]);
      }

etracer

Remove the Serial.begin and Serial.print from your code and try it.  You're probably running into "flow-control" because you're writing serial output and nothing is there to receive it.

jeeep

Tried that and still nothing happens

etracer

Why don't you post the complete sketch?  Without the rest of the code we can't really see what's happening.  For example, we don't know what you've initialized dataPin and clockPin to.  We don't know what type of data Variable_a_[] and Variable_b_[] are.

Additonally I don't see you setting dataPin and clockPin as outputs.

jeeep

Code: [Select]

#include <EEPROM.h>
#include <keypad.h>

#define ROWS 4
#define COLS 3

keypad kpd = keypad(ROWS, COLS);

int m_digit = 0;
int m_dollar = 1;
int prevalue;
int value;
int value_dollar;
int value_dollar_fixed;

 
// Definition of the block of pins to be used with pull-up pins in the INPUT
int lin0 = 8;    // Line 1 - Pin 3 - Understands characters 1 2 3
int lin1 = 7;    // Line 2 - Pin 2 - Understands characters 4 5 6
int lin2 = 6;    // Line 3 - Pin 1 - Understands characters 7 8 9
int lin3 = 5;    // Line 4 - Pin 0 - Understands characters * 0 #
int LedPin = 13 ;     // indicates that the pin 13 is connected to the LED

//Pin connected to ST_CP of 74HC595
int latchPin = 10;
//Pin connected to SH_CP of 74HC595
int clockPin = 12;
////Pin connected to DS of 74HC595
int dataPin = 11;

//holders for infromation you're going to pass to shifting function
int dataDOLLARa[] = {0xBF, 0x94, 0xF7, 0xF7, 0xFD, 0xEF, 0xEF, 0x97, 0xFF, 0xFF};
int dataDOLLARaBLANK = 0x00;
int dataDOLLARaALL = 0xFF;
int dataDOLLARb[] = {0x1F, 0x12, 0x1D, 0x1E, 0x12, 0x1E, 0x1F, 0x12, 0x1F, 0x12};
int dataDOLLARbBLANK = 0x00;
int dataDOLLARbALL = 0xFF;


void setup() {
 //set pins to output because they are addressed in the main loop
  pinMode(lin0, INPUT);    // Transforms the input to pin 3
  pinMode(lin1, INPUT);    // Transforms the input to pin 2
  pinMode(lin2, INPUT);    // Transforms the input pin 1 in
  pinMode(lin3, INPUT);    // Transforms the input pin 0 in

  kpd.init();    //  Starts at the keyboard matrix 4x3
  pinMode(LedPin, OUTPUT); // Set the LED pin as output
  pinMode(latchPin, OUTPUT);

  digitalWrite(lin0, HIGH);     // indicates use of the Pull-Up Line 1
  digitalWrite(lin1, HIGH);     // indicates use of the Pull-Up Row 2
  digitalWrite(lin2, HIGH);     // indicates use of the Pull-Up Line 3
  digitalWrite(lin3, HIGH);     // indicates use of the Pull-Up Line 4
     
  Serial.begin (9600), // Start the serial connection at 9600 baud
  Serial.print("Hello, how are you today?");  // indicates to the operator
  Serial.print('\n');
  value_dollar = EEPROM.read(m_dollar);
  value_dollar_fixed = value_dollar - 48;
       delay(100);
       Serial.print(value_dollar_fixed);
       shiftOut(dataPin, clockPin, dataDOLLARaBLANK);
       shiftOut(dataPin, clockPin, dataDOLLARbBLANK);
       shiftOut(dataPin, clockPin, dataDOLLARa[value_dollar_fixed]);
     shiftOut(dataPin, clockPin, dataDOLLARb[value_dollar_fixed]);
       EEPROM.write(0, 0);
}

void loop() {

char key = kpd.get_key();

   if(key != '\0')
   {
       digitalWrite(LedPin, HIGH);  
       delay(100);                  
       digitalWrite(LedPin, LOW);
       shiftOut(dataPin, clockPin, dataDOLLARaBLANK);
       shiftOut(dataPin, clockPin, dataDOLLARbBLANK);
       value = EEPROM.read(m_digit);
       delay(100);
     
       
     if (value == 0)
     {
       EEPROM.write(1, key);
       EEPROM.write(0, 1);
       Serial.begin(9600);
       Serial.print("key1=");
       Serial.print(key);
       Serial.print('\n');
               if(key == '1')
               {
                digitalWrite(latchPin, 0);
                shiftOut(dataPin, clockPin, dataDOLLARaBLANK);
            shiftOut(dataPin, clockPin, dataDOLLARbBLANK);
                delay(50);
                shiftOut(dataPin, clockPin, dataDOLLARaBLANK);
            shiftOut(dataPin, clockPin, dataDOLLARbBLANK);
                delay(50);
                shiftOut(dataPin, clockPin, dataDOLLARa[1]);
            shiftOut(dataPin, clockPin, dataDOLLARb[1]);
                digitalWrite(latchPin, 1);
                }
                }
     if (value == 1)
       {
         EEPROM.write(2, key);
         EEPROM.write(0, 2);
         Serial.begin(9600);
         Serial.print("key2=");
         Serial.print(key);
         Serial.print('\n');
         if(key == '1')
               {
                digitalWrite(latchPin, 0);
                shiftOut(dataPin, clockPin, dataDOLLARaBLANK);
            shiftOut(dataPin, clockPin, dataDOLLARbBLANK);
                delay(50);
                shiftOut(dataPin, clockPin, dataDOLLARaBLANK);
            shiftOut(dataPin, clockPin, dataDOLLARbBLANK);
                delay(50);
                shiftOut(dataPin, clockPin, dataDOLLARa[1]);
            shiftOut(dataPin, clockPin, dataDOLLARb[1]);
                digitalWrite(latchPin, 1);
                }
       }
      if (value == 2)
       {
         EEPROM.write(3, key);
         EEPROM.write(0, 0);    
         Serial.begin(9600);
         Serial.print("key3=");
         Serial.print(key);
         Serial.print('\n');
         if(key == '1')
               {
                digitalWrite(latchPin, 0);
                shiftOut(dataPin, clockPin, dataDOLLARaBLANK);
            shiftOut(dataPin, clockPin, dataDOLLARbBLANK);
                delay(50);
                shiftOut(dataPin, clockPin, dataDOLLARaBLANK);
            shiftOut(dataPin, clockPin, dataDOLLARbBLANK);
                delay(50);
                shiftOut(dataPin, clockPin, dataDOLLARa[1]);
            shiftOut(dataPin, clockPin, dataDOLLARb[1]);
                digitalWrite(latchPin, 1);
                }
                }
     }  
     }
     

// the heart of the program
void shiftOut(int myDataPin, int myClockPin, byte myDataOut) {
 // This shifts 8 bits out MSB first,
 //on the rising edge of the clock,
 //clock idles low

 //internal function setup
 int i=0;
 int pinState;
 pinMode(myClockPin, OUTPUT);
 pinMode(myDataPin, OUTPUT);

 //clear everything out just in case to
 //prepare shift register for bit shifting
 digitalWrite(myDataPin, 0);
 digitalWrite(myClockPin, 0);

 //for each bit in the byte myDataOut?
 //NOTICE THAT WE ARE COUNTING DOWN in our for loop
 //This means that %00000001 or "1" will go through such
 //that it will be pin Q0 that lights.
 for (i=7; i>=0; i--)  {
   digitalWrite(myClockPin, 0);

   //if the value passed to myDataOut and a bitmask result
   // true then... so if we are at i=6 and our value is
   // %11010100 it would the code compares it to %01000000
   // and proceeds to set pinState to 1.
   if ( myDataOut & (1<<i) ) {
     pinState= 1;
   }
   else {      
     pinState= 0;
   }

   //Sets the pin to HIGH or LOW depending on pinState
   digitalWrite(myDataPin, pinState);
   //register shifts bits on upstroke of clock pin  
   digitalWrite(myClockPin, 1);
   //zero the data pin after shift to prevent bleed through
   digitalWrite(myDataPin, 0);
 }

 //stop shifting
 digitalWrite(myClockPin, 0);
}






Thank you

etracer

A couple of early thoughts...

I don't even know how this compiles (but it does).  This isn't correct:
Code: [Select]
Serial.begin (9600), // Start the serial connection at 9600 baud

There's a space before the (9600) and a spurious comma afterwords and there is no terminating semicolon.

Also, why don't you use the built-in shiftOut function?  In your version you're unnecessarily setting the pinMode for the clock and data pins with every call.  These only need to be set once in your setup() code.

I'm a little iffy on your shiftOut code and potential timing issues.  For example you set the dataPin back to 0 immediately after raising the clock pin - with no delay.  This leaves a very short window (only a few clock cycles) where a high data bit is actually represented with a high data pin.  Depending on the device you're talking to this may be too short an interval to actually record as a high bit once the device "catches" the clock transition.

I think you'd have better luck using the tested and reliable built-in shitOut function.

Lastly, how do you know your code isn't running when the serial monitor isn't open?  If it's because you're not getting data out through your shift register then the problem might be in that code.  If you're using a Diecimila or Duemilanove (or clone of these), then opening the serial monitor will cause a reset of the chip.  This may be what you're seeing rather then it "unsticking".  Try putting some led blink logic in your loop() as a status indicator so you can tell that the code is actually running.  This will help narrow down where the problem is.

Anachrocomputer

Quote
There's a space before the (9600) and a spurious comma afterwords and there is no terminating semicolon.


The space is not significant; you can leave it out or add it to aid readability, but the compiler will not care.  As for the comma, well spotted!  It's a tiny difference!  In fact, a comma is legal in C there.  It's a expression separator that has a special meaning, but it only matters if you are assigning a variable with the value of the expression:

Code: [Select]
int i;

i = 2, 3;  // Assigns 3 to 'i' and the 2 is discarded


It's used by keen C programmers in for-loops:

Code: [Select]
int i, j;

for (i = 0, j = 1024; i < 256; i++, j -= 3) {
}

jeeep

Thanks etracer for your help,
  I pulled the shiftout code from this site here and I'm not nearly competent enough in coding to rewrite it (I've tried this morning to no avail). I did however add some blink logic to the void setup() to confirm that the program is running. I can also tell when the code is running (without computer connection to Arduino progrom) because I have a keypad hooked up to the Arduino and when I press a key that key gets shifted out to some leds.

  Also, I've noticed that when I have the Arduino hooked up to my computer and I open Serial Monitor the Arduino resets and the number stored in the EEPROM comes up as programmed. This tells me its not necessarily a shiftout problem but something else. Is this assumption correct?

Go Up