Problem with char = char; ... it messes up my program

Hi there,
I've been working on a sketch and I want to compare the new character i receive to my previous one. So I tried to use the command "key_previous = key;"
But then the program doesn't work like before. It messes up my array and saves the units to random places in the array.
If I remove my this specific command, then suddenly everything works like a charm.
Is there something wrong with the syntax of my command? Please help...
I provide you my code below...

char inData[20]; // Allocate some space for the string
char inChar; // Where to store the character read
byte index = 0; // Index into array; where to store the character
int mypinakas[6];
int dtmf_digit;
byte irq_state;
int m_x=0;
int m_y =0;
int dtmfdelay;

byte dato;           
 char key;            
 char key_previous;  
 
 void setup()
{
  
  
Serial.begin (4800);
  Serial.println("Starting test...");
 

 pinMode(IRQ_PIN, INPUT);
 pinMode(D0_PIN, INPUT);
 pinMode(D1_PIN, INPUT);
 pinMode(D2_PIN, INPUT);
 pinMode(D3_PIN, INPUT);
   
}

void loop()
{
    
 
 irq_state = digitalRead(IRQ_PIN);
 
 if ( irq_state == 1 )
 
    {
     
//    key_previous = key;  // **HERE IS THE PROBLEM******
                                    // ** IF I Remove it from comments then it doesn't work. Why is that?

Serial.print("Key : ");
      char key = read_codigo(); 
      Serial.println(key); 
      delay(300);          
 
     
   
if((key!='#')||(key!='*')) { 
 
 
  index++; 
}
  
 

if(key=='*') {
 Serial.println("DTMF Reset...Waiting for delay."); memset(mypinakas, 0, sizeof(mypinakas));  index=0;
}
 
if(key=='#') { 
  
   m_x=mypinakas[0];
   m_y=mypinakas[1];
   dtmfdelay = m_x*10+m_y;
Serial.print("Thermosifwnas ON for: ");
Serial.print(dtmfdelay);
Serial.println(" minutes.");

memset(mypinakas, 0, sizeof(mypinakas)); 
//Serial.print(mypinakas[0]); Serial.print(mypinakas[1]);Serial.print(mypinakas[2]);Serial.println(mypinakas[3]);
index=0;
}
      
      
       if(index < 5) // One less than the size of the array
     {
     
         mypinakas[index-1] = dtmf_digit; // Store it
      
     }
      
     
    }
        
}

where is "read_codigo();" that might help resolve the issue if we could see the code

and with the code you have shown here you never even look at key_previous or compare it to anything..soooo whats its purpose?
perhaps your ENTIRE code might help.

char inData[20]; // Allocate some space for the string
char inChar; // Where to store the character read
byte index = 0; // Index into array; where to store the character
int mypinakas[6];
int dtmf_digit;
byte irq_state;
int m_x=0;
int m_y =0;
int dtmfdelay;

byte dato;           
 char key;            
 char key_previous;  
 
 void setup()
{
  
  
Serial.begin (4800);
  Serial.println("Starting test...");
 

 pinMode(IRQ_PIN, INPUT);
 pinMode(D0_PIN, INPUT);
 pinMode(D1_PIN, INPUT);
 pinMode(D2_PIN, INPUT);
 pinMode(D3_PIN, INPUT);
   
}

void loop()
{
    
 
 irq_state = digitalRead(IRQ_PIN);
 
 if ( irq_state == 1 )
 
    {
     
//    key_previous = key;  // **HERE IS THE PROBLEM******
                                    // ** IF I Remove it from comments then it doesn't work. Why is that?

Serial.print("Key : ");
      char key = read_codigo(); 
      Serial.println(key); 
      delay(300);          
 
     
   
if((key!='#')||(key!='*')) { 
 
 
  index++; 
}
  
 

if(key=='*') {
 Serial.println("DTMF Reset...Waiting for delay."); memset(mypinakas, 0, sizeof(mypinakas));  index=0;
}
 
if(key=='#') { 
  
   m_x=mypinakas[0];
   m_y=mypinakas[1];
   dtmfdelay = m_x*10+m_y;
Serial.print("Thermosifwnas ON for: ");
Serial.print(dtmfdelay);
Serial.println(" minutes.");

memset(mypinakas, 0, sizeof(mypinakas)); 
//Serial.print(mypinakas[0]); Serial.print(mypinakas[1]);Serial.print(mypinakas[2]);Serial.println(mypinakas[3]);
index=0;
}
      
      
       if(index < 5) // One less than the size of the array
     {
     
         mypinakas[index-1] = dtmf_digit; // Store it
      
     }
      
     
    }
        
}

byte read_codigo()
{
 


 byte D0,D1,D2,D3;    
 
  
 D0 = digitalRead(D0_PIN);
 D1 = digitalRead(D1_PIN);
 D2 = digitalRead(D2_PIN);
 D3 = digitalRead(D3_PIN);
 
 
 bitWrite(dato,0,D0);
 bitWrite(dato,1,D1);
 bitWrite(dato,2,D2);
 bitWrite(dato,3,D3);


 if ( dato == 1  ) key = '1';
 if ( dato == 2  ) key = '2';
 if ( dato == 3  ) key = '3';
 if ( dato == 4  ) key = '4';
 if ( dato == 5  ) key = '5';
 if ( dato == 6  ) key = '6';
 if ( dato == 7  ) key = '7';
 if ( dato == 8  ) key = '8';
 if ( dato == 9  ) key = '9';
 
 if ( dato == 10 ) key = '0';
 if ( dato == 11 ) key = '*';
 if ( dato == 12 ) key = '#';
 if ( dato == 13 ) key = 'A';
 if ( dato == 14 ) key = 'B';
 if ( dato == 15 ) key = 'C';
 if ( dato == 0  ) key = 'D';
 
dtmf_digit =atoi (&key);

return key;

}

This is all my code an still it messes with other variables. I dont know why. All I know is that when I remove the comments from "key_previous=key;" then it doesn't work right.
I want to extend my program, i am developing now. And I want afterwards to compare the key_previous to key char so i know its not hte same. If its the same i will store it to the previous location of the index in the array. But it doesn't matter because the program stops working right after i remove just the comments.

using the line " char key = read_codigo(); " after storing key into previous_key may be a scope issue?

Why are you re-scoping key?

 key_previous = key;  // **HERE IS THE PROBLEM******

Why are you doing this anyway? You never use key_previous.

In this code:

    if((key!='#')||(key!='*')) { 
      index++; 
    }

Please describe for what values of key you think that index will not be incremented.

eddiea6987:
the error starts here whe n you try to convert the char to an integer.

dtmf_digit =atoi (&key);

Plus I agree with this. You are converting the address of key to an integer. There is no useful point in doing that.

Numbers are going to be the delay of my system in minutes.
I want to compare key_previous to key, so I am ensured that the same character is not gong to be stored to another place of the array again.And then in the second place of the array there is going to be saved a different number.
I am working on a project with dtmf so I can turn on my boiler when i am gone from home. So when i get there its hot.
The user who calls, will press two numbers and then the "#" so the boiler is turned on.
But I am reading with a small delay. So if a user presses more a number it can seees it as another number.
I dont want to have duplicate numbers when i receive, so i want to exclude them so there are no mistakes.
So 11,22,33,44,55,66,77,88,99 is not going to work.

And to do that i need to compare the previous to the current key.

You haven't answered my question about index++.

In any case:

    if(index < 5) // One less than the size of the array

{

mypinakas[index-1] = dtmf_digit; // Store it

}

What if index is zero? Then you are storing in location 255 which is out of range.

Please do not focus to the code but to what causes problems to my program when i enter the command.
I want to make a code for use of key_previous after i solve the problem of saving values to it.

I realised that by just entering this command the numbers are not saved normally but in random places in the array. Do ot ask me more for the code because it works for me.
My question is if "key_previous=key;" is causing any problems to the arduino compiler or something. Or to ROM. I dont know.

if(key=='*') 
          {
              Serial.println("DTMF Reset...Waiting for delay."); memset(mypinakas, 0, sizeof(mypinakas));  [glow=yellow,2,300]index=0;[/glow]
          }
           
          if(key=='#') 
          { 
            
             m_x=mypinakas[0];
             m_y=mypinakas[1];
             dtmfdelay = m_x*10+m_y;
              Serial.print("Thermosifwnas ON for: ");
              Serial.print(dtmfdelay);
              Serial.println(" minutes.");
          
              memset(mypinakas, 0, sizeof(mypinakas)); 
              //Serial.print(mypinakas[0]); Serial.print(mypinakas[1]);Serial.print(mypinakas[2]);Serial.println(mypinakas[3]);
              [glow=yellow,2,300]index=0;[/glow]
          }

and then

 [glow=yellow,2,300]if(index < 5)[/glow] // One less than the size of the array
           {
               mypinakas[index-1] = dtmf_digit; // Store it
           }

it is always going to be less then 5 because you either set it less then 5 or you never let it get passed and then you SET it to 0 and then subtract 1 ? 0 -1 ?, Also index < 5 is NOT ONE less then the array it is EVERYTHING less then 5 which means 4 and below and the array is 6

Please do not focus to the code but to what causes problems to my program when i enter the command.

well making one variable = another variable of the same type is not a problem that means the problem is in your code, so how are we not suppose to look at the code as a whole?

The array should just be 3 but i was just testing some values.

I REPEAT, the code works great without "key_previous=key;"
I think we should focus on it. I want to use key_previous afterwards, and the code doesn;t have it anywhere else.
But somehow it messes other variables or something.

I am posting some results of the serial monitor later so you can get an idea of what i mean.

nathanas:
Please do not focus to the code but to what causes problems to my program when i enter the command.
I want to make a code for use of key_previous after i solve the problem of saving values to it.
..

My question is if "key_previous=key;" is causing any problems to the arduino compiler or something. Or to ROM. I dont know.

Don't focus on the code? Focus on what then?

Your code is bugged. You just don't realize it. If adding a line like that makes a major problem happen (and the line itself seems to be OK) then you have other problems. Adding that line simply makes them appear.


In this code:

if((key!='#')||(key!='*')) { 
      index++; 
    }

The variable index will always be incremented. So the if statement serves no purpose.


This code:

...
index=0;
}
      
  if(index < 5) // One less than the size of the array
     {
         mypinakas[index-1] = dtmf_digit; // Store it
     }

Will overwrite memory by writing to array position 255.


This code:

  dtmf_digit =atoi (&key);

Is not doing what you I imagine that you think it is. The address of key will never change. So why convert it to an integer and place that integer into dtmf_digit?


In this code:

char key;            
...

    char key = read_codigo();

You have two variables called "key", not one.


Fix the obvious bugs, then you might find your problem goes away.

I think we should focus on it. I want to use key_previous afterwards, and the code doesn;t have it anywhere else.
But somehow it messes other variables or something.

What I describe above is the "somehow" part.

Thanks for your response i will look through it tomorrow...and i am posting the results.

by deleting " char key; " all worked fine.

I think my problem is converting key to integer.
How can I convert it right? "key_previous = atoi (key); " doesn't seem to work right...

nathanas:
I think my problem is converting key to integer.
How can I convert it right? "key_previous = atoi (key); " doesn't seem to work right...

What do you mean "my problem"? I thought you said removing the 2nd char definition fixed the issue. Are you describing a new problem?

nathanas:
by deleting " char key; " all worked fine.
...
I think my problem is converting key to integer ...
...

Are we to assume that you ignored all the suggestions in my earlier post because now it all "worked fine"?

But:

... doesn't seem to work right...

Can you be a little more specific about that?

  • What is your revised code?
  • What happens when you run it?
  • What do you expect to happen?