Arduino doesn't do what I've written in the code (FIXED).

Hi Conguito,

After a quick look - seems like your function call guardamemoria(valores); is out of place. It is currently within the loop that you are writing each LED state bit into variable "valores".... It think you want the function to be executed after you've completed "popping" all the bits into "valores".

After you fix that you might want to fix function void guardamemoria() to wait key input, like you did with the do-loop from before, otherwise the program will fly right through the procedure...

Well.....Skorn I did what you say, I toke the calling to guardamemoria(valores) after the Led state bit writing operation and I insert another do...while loop for the guardamemoria function so that it stays in the switch....case routine till I press the 'F' key, now that is working correct and the programm doesn't goes directly to the Led blinking routine, but now I see that the arduino stays in this part of the programm and it doesn't respond to any key press, including the 'F' key........can it be that the way I'm using to store every bit in the byte valores isn't correct? It's posible to read the values of the ledPins array and store it in the byte valores in an easier way?
Thanks for your help, I'm still new at Arduino (not in electronic), but I'm learning new things everyday from people like you who helps to understand how it all works.

OK, I'll give you a hint :):

You declare and initialize certain variables globally. They work the first time you run through the program, but some of their values may need to be reset for subsequent iterations.

Repost an updated version of your code if you run into any other problems.

Well I'm at this point:

/*
Using 8 outputs and saving output presets in the EEPROM
 */
#include <Keypad.h>				//Inlcude Keypad Library
#include <EEPROM.h>                            //Include EEPROM Library
#include <DigitalToggle.h>		      //Include DigitalToggle Library
int ledPins[] = {
  0,1,2,3,4,5,6,7};		              //Defining program mode Leds
int bypassled = 11;                           //Defining the bypass Led
int latchPin = 14; 			      //Defining latchPin from the 74HC595
int clockPin = 15; 			      //Defining clockPin from the 74HC595
int dataPin = 13; 			      //Defining dataPin from the 74HC595 
const byte filas = 4; 			// rows of the Keypad
const byte colus = 4; 			// columns of the Keypad
char teclas[filas][colus] = {       //mapping the Keypad layout
  { 
    '1', '2', '3', '4'             }
  ,
  { 
    '5', '6', '7', '8'             }
  ,
  { 
    '9', '0', 'A', 'B'             }
  ,
  { 
    'C', 'D', 'E', 'F'             }
};

// defining the pins that are used for the rows and columns of the Keypad

byte filaPins[filas] = {
  16, 17, 18, 19 }; 
byte coluPins[colus] = {
  8, 9, 10, 12 };       
Keypad teclado = Keypad( makeKeymap(teclas), filaPins, coluPins, filas, colus );
byte dataOut = 0;
byte valores = B00000000;
int posi = 0;
byte memo0 = B00000000;				                  //the bypass value is always the same, so it's not necessary to save it in the EEPROM
byte memo[] = {
  0,5,10,15,20,25,30,35,40,45,50,55,60,65};  // defining the EEPROM adresses

void setup()					 
{
  for(int index = 0; index <= 8; index++)	//defining output pins
  {
    pinMode(ledPins[index], OUTPUT);
  }
  pinMode(bypassled, OUTPUT);
  pinMode(latchPin, OUTPUT);		
  pinMode(clockPin, OUTPUT);
  pinMode(dataPin, OUTPUT);

  for(int index = 0; index <= 7; index++)
  {
    digitalWrite(ledPins[index], LOW); 	// shutdown the programming Leds
  }
  digitalWrite(bypassled, HIGH);		// lighting the bypass Led
  digitalWrite(latchPin, LOW);
  shiftOut(dataPin, clockPin, LSBFIRST, memo0);  
  digitalWrite(latchPin, HIGH);
  delay(1000);					//wait a second........

}

void loop()					
{
  teclado.setDebounceTime(10);
  char key = teclado.getKey();			
  switch(key)					//the key we select sends a memory content assigned to this key
  {					        
  case '1':					
    enviamemo(0);                               
    break;						
  case '2':
    enviamemo(1);
    break;
  case '3':
    enviamemo(2);
    break;
  case '4':
    enviamemo(3);
    break;
  case '5':
    enviamemo(4);
    break;
  case '6':
    enviamemo(5);
    break;
  case '7':
    enviamemo(6);
    break;
  case '8':
    enviamemo(7);
    break;
  case '9':
    enviamemo(8);
    break;
  case '0':
    enviamemo(9);
    break;
  case 'A':
    enviamemo(10);
    break;
  case 'B':
    enviamemo(11);
    break;
  case 'C':
    enviamemo(12);
    break;
  case 'D':
    enviamemo(13);
    break;
  case 'E':
    modobypass();              //In case we select the 'E' key we set the bypass mode
    break;
  case 'F':
    modoPrograma();            //if we select the 'F' key we are entering in the programming mode
    break;
  }
}   

void modoPrograma()				
{						
  digitalWrite(bypassled, LOW);
  {
    digitalWrite(latchPin, LOW);
    shiftOut(dataPin, clockPin, LSBFIRST, memo0);  
    digitalWrite(latchPin, HIGH);
    delay(500);
    for (int i = 0; i <= 2; i++)
    {
      for(int index = 0; index <= 7; index++)
      {
        digitalWrite(ledPins[index], HIGH); 		// we make blink the programming Leds three times, so we know that we are entering programming mode
      }						
      delay(500);
      for(int index = 0; index <= 7; index++)
      {
        digitalWrite(ledPins[index], LOW); 	
      }						
      delay(500);
    }
  }
  {
    char key;
    do
    {
     key = teclado.getKey();
      switch(key) 						
    {					              
    case '1':						//Now we select which outputs we want to have ON or OFF
      digitalToggle(ledPins[0]);
      break;	                //The programming Leds shows us which Output will be ON or OFF
    case '2':						//in the preset, every Led corresponds to an Output.
      digitalToggle(ledPins[1]);
      break;			//we use the digitalToggle library to change the state of each Led		
    case '3':						
      digitalToggle(ledPins[2]);
      break;					
    case '4':						
      digitalToggle(ledPins[3]);
      break;			
    case '5':						
      digitalToggle(ledPins[4]);			
      break;
    case '6':						
      digitalToggle(ledPins[5]);			
      break;
    case '7':
      digitalToggle(ledPins[6]);
      break;
    case '8':
      digitalToggle(ledPins[7]);
      break;
    }
  }
while (key != 'E');                                  //If we press the 'E' key we go to the next step
    
}
  posi = 0;                                         //we reset posi
  valores = B00000000;                   //we reset valores
  for(int i=0; i <= 7; i++)			//we save every Led state in the byte valores
  {
    bitWrite(valores, posi, digitalRead (ledPins[i])); 	
    posi = posi + 1; 					
  }
    guardamemoria(valores); 			// calling the save function
    {
      digitalWrite(latchPin, LOW);
      shiftOut(dataPin, clockPin, LSBFIRST, memo0);  
      digitalWrite(latchPin, HIGH);
      delay(500);
      for (int i = 0; i <= 2; i++)
      {
        for(int index = 0; index <= 7; index++)
        {
          digitalWrite(ledPins[index], HIGH); 		// we make blink the programming Leds three times, so we know that we are exiting programming mode
        }						
        delay(500);
        for(int index = 0; index <= 7; index++)
        {
          digitalWrite(ledPins[index], LOW); 		
        }						
        delay(500);
      }
    }  
}
void guardamemoria(byte valores)	//function to save the byte valores in the EEPROM, this byte contains the preset information that we have programmed
{
  char key;
  do
  {
  char key = teclado.getKey();         //HERE IS WHERE THE ARDUINO STAYS FOREVER AND IT DOESN'T EXIT, IT DOESN'T MATTER WHICH KEY I PRESS!!!!!
  switch(key)					//Now we select in which adress of the EEPROM we want to save the Byte value
  {					
  case '1':				
    grabamemo(0, valores);              
    break;				
  case '2':
    grabamemo(1, valores);
    break;
  case '3':
    grabamemo(2, valores);
    break;
  case '4':
    grabamemo(3, valores);
    break;
  case '5':
    grabamemo(4, valores);
    break;
  case '6':
    grabamemo(5, valores);
    break;
  case '7':
    grabamemo(6, valores);
    break;
  case '8':
    grabamemo(7, valores);
    break;
  case '9':
    grabamemo(8, valores);
    break;
  case '0':
    grabamemo(9, valores);
    break;
  case 'A':
    grabamemo(10, valores);
    break;
  case 'B':
    grabamemo(11, valores);
    break;
  case 'C':
    grabamemo(12, valores);
    break;
  case 'D':
    grabamemo(13, valores);
    break;
  }
  }
  while (key != 'F');
}
void grabamemo(int pos, byte valores)        //saving to the EEPROM function
{
  EEPROM.write(memo[pos], valores);
}
void enviamemo(int pos)                      //function to send the preset saved on the EEPROM adress to the 74HC595
{
  digitalWrite(bypassled, LOW);
  digitalWrite(latchPin, LOW);
  shiftOut(dataPin, clockPin, LSBFIRST, memo[pos]);  
  digitalWrite(latchPin, HIGH);
}
void modobypass()
{
  digitalWrite(bypassled, HIGH);
  digitalWrite(latchPin, LOW);
  shiftOut(dataPin, clockPin, LSBFIRST, memo0);  
  digitalWrite(latchPin, HIGH);

}

Sometimes I'm really thinking about the "easy option" using a bigger Atmel microcontroller........

Compare the two snippets in your code (from different functions).

    char key;
    do
    {
     key = teclado.getKey();
      switch(key) 						
// ...
    char key;
    do
    {
      char key = teclado.getKey();         //HERE IS WHERE THE ARDUINO STAYS FOREVER AND IT DOESN'T EXIT, IT DOESN'T MATTER WHICH KEY I PRESS!!!!!
      switch(key)					//Now we select in which adress of the EEPROM we want to save the Byte value

See? The heavily commented line redeclares char key when you teclado.getKey().

spatula, thanks for your advice, I haven't see it, now it works and exit the switch....case routine, but it seems like the Arduino doesn't read the ledPins state and doesn't save the state of the pins to the EEPROM. It's possible that the digitalRead function doesn't work with pins in ouput mode........or that I need to use another method instead of bitWrite to save the ledPins state in the byte valores........I will read some more manuals and hoping that the digitalRead function works with output pins.
Thanks for your help, another little step ahead.

Conguito:
hoping that the digitalRead function works with output pins.

It does. I'm pretty sure you'll find that your remaining problems are logic faults in your code.

I hope so PeterH, I'm looking for an error in the code or something that I've written wrong, I'm sure that there something that I don't see at this moment but sooner or later I'll find out what's the problem. Thanks for your help, in the case that I can't find the error in this week I'll scream for help here $)

Conguito:
I'm looking for an error in the code or something that I've written wrong,

In other words, the Arduino was doing EXACTLY what you had written in the code, although that was not what you intended it to do. :stuck_out_tongue:

Well.....I'm not sure if there are an error or it can be that I haven't used the right way........at this moment I am making test using bitRead and port manipulation.......will see if it works.

After looking for something strange in the code and seeing that I'm not able to find anything strange and testing different methods to save pin values in a byte and saving this byte to the EEPROM in the last three days.......I'll let this project stopped till the moment I've more knowledge about Arduino, by now I'll do this project in the "simply" way, I will store the presets for the outputs in the code itself, the problem is that if i want to change any of the presets I have to do it reprogramming again the Arduino. I hope that my learning curve makes me try to finish this project like it was originally in my mind soon.
Thanks all for your help.

Hi Conguito,

After looking for something strange in the code and seeing that I'm not able to find anything strange and testing different methods to save pin values in a byte and saving this byte to the EEPROM in the last three days

You'll have to elaborate a little more on what you tested and where the problem is. Is the problem reading the port states and writing the data bits to byte "valores"? That part of your code looks ok to me and think it should work as is. Have you tested reading the EEPROM to see if "valores" is being written to the correct location? You can write a simple seperate sketch to check the values that have been stored in the EEPROM.

While you are in the developing stage its helpful while debugging to enable serial communication and at points in the program print out program state information data which will be displayed in the Arduino IDE's serial monitor window. For example:

Serial.begin(9600); // add this in your setup()

Serial.println(valores, BIN); // add this in your code right after fully populating valores with all the bits.

Well.....the problem is that the pins that use the serial port are in use as output pins, but I've had another idea, I have here arround and will use an atmega1284p with the same sketch and use the second serial port to see what is happening.........maybe the error isn't where I have been looking, so I will try with a larger microcontroller to have a serial monitor to look while the arduino is running, so it will be easier to find out where is the problem.

Well, I post to tell you all that I finally got it to work, it was an error in the code. I forgot to put an EEPROM.read instruction in a function, so Arduino was never able to read from the EEPROM. Now it's all working fine. The serial monitor was a big help, finally I used the code with an atmega1284p mounted like a sanguino board to be able to use the serial monitor and I find out the error very soon.
I want to thank you all for your help, if someone is interested I can paste here the code. First I have to optimize it, but I think that in two days I will have it finished.

Hi Conguito,

Glad to hear you got it working.