how to attachinterrupt()

so i was trying to attach a interrupt to a pin(18) on arduino mega 2560 R3 and i wrote this code so i want the cursor to display a character and blink its like a typing system .The interrupt is not given response by the microcontroller i dont see any character blinking also i have double-checked the wiring this is the code -
6 Replies
Robo_17
Robo_17OP6d ago
#include <LiquidCrystal.h>
#include <string.h>
const int rs =12,en=11,d4=5,d5=4,d6=3,d7=2;
int A,B,C,D,E,H,L = 0x0;
//int S,Z,AC,Carry
int flags[4] = {0,0,0,0};
//memory array
int memory[60] = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
LiquidCrystal lcd(rs,en,d4,d5,d6,d7);
//push button,execute,enter
int pushButton = 18;
int execute = 19;
//int enter = ;
///////////////////////////
int executeState = 0;
int enterState = 0;
//always high pin
int alwayshigh = 34;

//The array of states
int state = 1; //this is needed to take input from user using the push button
int states[2] = {0,1};//1=Write,0=Read

//the array for characters to take input
char instructionChars[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789,";
char currentCharacter = 'A';
/////////////////////////////////////////////////////
#include <LiquidCrystal.h>
#include <string.h>
const int rs =12,en=11,d4=5,d5=4,d6=3,d7=2;
int A,B,C,D,E,H,L = 0x0;
//int S,Z,AC,Carry
int flags[4] = {0,0,0,0};
//memory array
int memory[60] = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
LiquidCrystal lcd(rs,en,d4,d5,d6,d7);
//push button,execute,enter
int pushButton = 18;
int execute = 19;
//int enter = ;
///////////////////////////
int executeState = 0;
int enterState = 0;
//always high pin
int alwayshigh = 34;

//The array of states
int state = 1; //this is needed to take input from user using the push button
int states[2] = {0,1};//1=Write,0=Read

//the array for characters to take input
char instructionChars[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789,";
char currentCharacter = 'A';
/////////////////////////////////////////////////////
int currentCursor[] = {0,0};
void setup() {
// put your setup code here, to run once:
pinMode(pushButton,INPUT_PULLUP);
pinMode(execute,INPUT);
// pinMode(enter,INPUT);
pinMode(alwayshigh,OUTPUT);
digitalWrite(alwayshigh,HIGH);
lcd.begin(16,2);
lcd.print("8085 Simulator");
delay(2000);
lcd.clear();
lcd.setCursor(6,0);
attachInterrupt(digitalPinToInterrupt(pushButton), changecharacter, RISING);
for(int i =0;i<=3;i++){
lcd.print(flags[i]);
}
lcd.setCursor(16,1);
currentCursor[0] = 16;
}

//The ISR of the pushbutton
void changecharacter(){
for(int i=0;i<=36;i++){
if (instructionChars[i] == currentCharacter){
currentCharacter = instructionChars[i];
break;
}
currentCursor[0] -= 1;
}
delay(500);
}
///////////////////////////////

//The ISR of the the execute
// void executefunction(){

// delay(100);
// }
//////////////////////////////////
//The ISR of enter
// void enter(){

// }
int blinkingCharacter(char character,int x,int y){
while(enterState == 0){
lcd.setCursor(x,y);
lcd.print(character);
delay(1000);
lcd.print(" ");
}
return 0;
}
void loop() {
// put your main code here, to run repeatedly:
// lcd.setCursor(0,1);
// lcd.print(millis()/1000);
// delay(100);
switch(state){
case 1:
blinkingCharacter(currentCharacter,currentCursor[0],currentCursor[1]);
}
}
int currentCursor[] = {0,0};
void setup() {
// put your setup code here, to run once:
pinMode(pushButton,INPUT_PULLUP);
pinMode(execute,INPUT);
// pinMode(enter,INPUT);
pinMode(alwayshigh,OUTPUT);
digitalWrite(alwayshigh,HIGH);
lcd.begin(16,2);
lcd.print("8085 Simulator");
delay(2000);
lcd.clear();
lcd.setCursor(6,0);
attachInterrupt(digitalPinToInterrupt(pushButton), changecharacter, RISING);
for(int i =0;i<=3;i++){
lcd.print(flags[i]);
}
lcd.setCursor(16,1);
currentCursor[0] = 16;
}

//The ISR of the pushbutton
void changecharacter(){
for(int i=0;i<=36;i++){
if (instructionChars[i] == currentCharacter){
currentCharacter = instructionChars[i];
break;
}
currentCursor[0] -= 1;
}
delay(500);
}
///////////////////////////////

//The ISR of the the execute
// void executefunction(){

// delay(100);
// }
//////////////////////////////////
//The ISR of enter
// void enter(){

// }
int blinkingCharacter(char character,int x,int y){
while(enterState == 0){
lcd.setCursor(x,y);
lcd.print(character);
delay(1000);
lcd.print(" ");
}
return 0;
}
void loop() {
// put your main code here, to run repeatedly:
// lcd.setCursor(0,1);
// lcd.print(millis()/1000);
// delay(100);
switch(state){
case 1:
blinkingCharacter(currentCharacter,currentCursor[0],currentCursor[1]);
}
}
note i replaced the pushbutton with a jumper wire and just make it high and low from +5V
DarwinWasW
DarwinWasW6d ago
https://wokwi.com/projects/432368632038042625 works with a led - may be too much in ISR
int18 - Wokwi ESP32, STM32, Arduino Simulator
Run IoT and embedded projects in your browser: ESP32, STM32, Arduino, Pi Pico, and more. No installation required!
Robo_17
Robo_17OP6d ago
hmn will test
DarwinWasW
DarwinWasW6d ago
With ISR, you use volatile variables and just change a Boolean for the process to occur in the loop. doing a for loop and a delay will result in drama
Robo_17
Robo_17OP6d ago
Actually i want to just change the currentchar value to the next character in the string so ig i will just keep a track of the currentchar index in that string outside isr
DarwinWasW
DarwinWasW5d ago
Remember to use volatile on variables changed in ISR That are used else where.

Did you find this page helpful?