Updating sketch from old IDE serial HELP!!

Hello I am hoping someone can help me.
I have some code that runs fine on the old arduino IDE that i am trying to update to the new one. also updating to use the liquidcrystal libary instead of the old LCD4bit_mod one. here is the relevent portion of the code, a timer interrupt that interrupts every 4ms to read the buttons
it is all working except the serial part i think as it compiles ok and works on the old IDE (unless there are other changes I am unaware of).

#include <avr/interrupt.h>
#include <avr/io.h>
#include <LiquidCrystal.h>"
#include <EEPROM.h>


#define DEBOUNCE_MAX 17 // max value for debounce count
#define DEBOUNCE_ON 12 // debounce pressed threshold 12x4ms = 48ms
#define DEBOUNCE_OFF 2  // debounce released threshold 8ms


int  adc_key_val[5] ={30, 150, 360, 535, 760 };
int z = 255;
int y = 255;
byte NUM_KEYS = 5;
char key=-1;
char oldkey=-1;

// debounce counters
byte button_count[5];
// button status - pressed/released
byte button_status[5];
// button on flags
byte button_flag[5];

// Timer2 interrupt routine -
// 1/(160000000/256/(256-6)) = 4ms interval

ISR(TIMER2_OVF_vect) {  
  
 
  TCNT2  = 6;
  update_adc_key();
}

// Convert ADC value to key number
char get_key(unsigned int input)
{
	char k;
    
	for (k = 0; k < NUM_KEYS; k++)
	{
		if (input < adc_key_val[k])
		{
           
    return k;
        }
	}
    
    if (k >= NUM_KEYS)
        k = -1;     // No valid key pressed
    
    return k;
}

void update_adc_key(){
  int adc_key_in;
  char key_in;
  byte i;
  
  adc_key_in = analogRead(0);
  key_in = get_key(adc_key_in);
  for(i=0; i<NUM_KEYS; i++)
  {
    if(key_in==i)  //one key is pressed 
    { 
      if(button_count[i]<DEBOUNCE_MAX)
      {
        button_count[i]++;
        if(button_count[i]>DEBOUNCE_ON)
        {
          if(button_status[i] == 0)
          {
            button_flag[i] = 1; // pressed state flag ON
            button_status[i] = 1; //button debounced to 'pressed' status
          }
        }
      }
    }
    else // no button pressed
    {
      if (button_count[i] >0)
      {  
        button_count[i]--;
        if(button_count[i]<DEBOUNCE_OFF){
          button_status[i]=0;   //button debounced to 'released' status
        }
      }
    }
    
     // output button pressed state
  if(button_flag[i]==1)
  {
    button_flag[i]=0;      // clear flag to avoid repeats
    Serial.print('A'+i,BYTE);   // Output button code 

  }
  }
  
 
}

I have changed the serial part as follows:

Serial.print('A'+i,BYTE);

Changed to :

Serial.write(byte('A'+i));

This don’t seem to be working.

EDIT: should of said what I expect to happen … sorry :slight_smile:

What should happen is it should output the letter A-E depending on the button out the serial port but its not happning

the hardware setup is a DFrobot keypad LCD shield V1, that has the analog buttons through resistors on a analog pin, but I think the only problem with the code is the serial portion.

what am I doing wrong ?

I know the hardware but I don't know the "old IDE". I don't know too what you're trying to do with:

Serial.write(byte('A'+i));

(or whatever), but if you're trying to send the number of the key (or better, the number 'i') via serial, you must do:

Serial.write('0'+i);

EDIT: But I think that you can do the same by only send the byte like:

Serial.print(i);

This don’t seem to be working.

What do you expect it to do and what does it actually do ?
The conversion to byte should not be necessary.

  for (int i = 0; i < 26; i++)
  {
    Serial.write('A' + i);
  }

Should output the alphabet in capitals for instance

thatguy: it is all working except the serial part i think

Is serial output from that sketch working at all? Maybe it's as simple as selecting the right COM port, or the right serial speed. Make sure you're calling Serial.begin() in setup() and add a Serial.println() call to print out a message when it starts up. Do you see that message in the serial monitor?

i should of been more clear, sorry. the "old ide" i was talking about was the older version of the arduino software (0022). the posted code worked fine with that.

what I am trying to do is read in the buttons, and send the result through serial. but it should send capital letters instead of numbers.

for example:

Button 1 ---- A Button 2 ---- B Button 3 ---- C Button 4 ---- D Button 5 ----- E

No PeterH there is nothing coming out the serial port and i have no serial.printIn() commands anywhere. im guessing it sends buffered data out the serial or something that was not needed in 0022. i will look it up now

serial.printin seems to be simalar to serial.write but converts to hex binary etc, so i dont think that is the problem. i have serial.begin in void.setup but still nothing comes out the serial.

Where is the setup() function doing the initialization of the serial object? You must have something like this:

void setup() {
  //Initialize serial and wait for port to open:
  Serial.begin(9600);

(...)
}

Other thing that can be wrong is that the baud rate from your program don't matches the baud rate at your "serial console".

If you want the keys named "A, B, ..." your code is OK.

I don't like (but that is other problem) that the ISR function "works with" the serial.

the baud rates defiantly match in the program and the sketch. the original piece of the sketch i posted worked fine in arduino 0022. i cant find a problem with my sketch either, it should work but it don't :0.

yes, i am initilizing serial in the void setup portion of the code. i really dont understand why it is not working

Did you test one of the examples that came with Arduino IDE (for example the "ASCII table")? Did your program writes all the other messages fine?

thatguy:
serial.printin seems to be simalar to serial.write but converts to hex binary etc, so i dont think that is the problem.
i have serial.begin in void.setup but still nothing comes out the serial.

Forget your program for now. Simply put Serial.begin(); and a Serial.println(“something”); in the setup() function and run the program. Do you see any output in the serial monitor when you open it ?

Yeap! Same idea here!

i run this sketch,as requested. baud set to same as my program :

void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200); 
  Serial.println("word"); 

}

void loop() {
  // put your main code here, to run repeatedly:

}

and sure enough i open serial monitor change baud to match (15200 Baud) and i get "word" comming through the serial monitor.

And if you use:

Serial.write('A' + i);

inside a for loop?

i don't understand what you mean. do you want me to try my code outside of the interrupt and in the main loop ?

For now, no. I only mean:

void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200); 
  Serial.println("word"); 

}

void loop() {
  for (int i=0; i<26; i++){
    Serial.write('A' + i);
  }
  delay (2000);
}

See reply #2

Sorry UKHeliBob. I'm a little distracted today and I don't realize that you already had wrote this.

thatguy: i don't understand what you mean. do you want me to try my code outside of the interrupt and in the main loop ?

Wait... You are trying to write to the hardware serial port from within an ISR? As of Arduino1.0+ the serial access is now asynchronous, meaning it uses interrupt events. Not something you can do from within an ISR. Best to set a flag inside the interrupt and then have your main loop continuously poll the flag. Then print and clear the flag if the main program finds the flag set.

There he his the answer to the problem. So, I get wrong in the Reply #7. Sorry about that.