LED and interrupt -- HELP Please

I’m a total beginner and have no real background in c++ programing, but for my school project I want to make optical theremin with 7 segment LED display. when I combined 2 program (7 segment LED display + optical theremin) they’re work as well, but when my teacher asked me to put a push button for the input, I being confused with the program.

I want the push button work as an interrupt, so when I press the button, the character of 7 segment LED should change asap, and the random character still running when the button is not pressed. But the fact is when I press the button, they will run random (it because I used a program for LED DICE) and I have to wait until the character is done and it will stop blinking until I press the button again. and if the button not press it’s just make tone signal without blinking any character.

Is there anyone can help me with the program please…?

``````int ROW1 = 1;
int ROW2 = 2;
int ROW3 = 3;
int ROW4 = 4;
int ROW5 = 5;
int ROW6 = 6;
int ROW7 = 7;

//pin definitions
#define PHONES 9
// headphones connected to digital pin 9
#define PHOTOCELL 0

//Button pin
int buttonPin = 10;
int buttonState;

//Ran will be randomized from 1 to 6
long ran;

//variable definitions
long val = 0;
//stores raw value from photocell
//maximum value from calibration phase
// minimum value from calibration phase
double f = 0;
// frequency of sound
double normf = 0;
// normalized frequency
double logf = 0;
// logarithm of normalized frequency
int ilogf = 0;
// rounded logarithm
int i = 0;
// loop dummy variable
double factor = 0;
// scaling factor for calibration
double shift = 0;
// shift for calibration
long maxfreq = 1048;
// maximum desired frequency after calibration
long minfreq = 131;
// minimum desired frequency after calibration

double gap = 1.148698355;
//ratio of consecutive notes (pentatonic)
// it's the 5th root of 2

void setup()

{
//Set the pin of the button as an Input
pinMode (buttonPin, INPUT);
//This code line is necessary for a correct random

pinMode(ROW1, OUTPUT);
pinMode(ROW2, OUTPUT);
pinMode(ROW3, OUTPUT);
pinMode(ROW4, OUTPUT);
pinMode(ROW5, OUTPUT);
pinMode(ROW6, OUTPUT);
pinMode(ROW7, OUTPUT);

pinMode(PHONES, OUTPUT);
// sets the digital pin as output
for (i = 0; i< 500; i++) {
// calibration loop runs for 5 seconds
tone(PHONES, val);
// play raw tone to guide calibration
// as the values climb, store the largest
}
// as the values drop, store the smallest
}
delay(10);
// reasonable delay

}
//Now we use the calibration to calculate scale and shift parameters
// scale parameter
//it's like a slope

shift = factor * minread - minfreq;
//shift parameter: it's like an offset
}

void loop()
{
//Read the status of the button
if (buttonState == HIGH){
//Randomize from 1 to 6
ran = random(7);

// BEST
if (ran == 1){
// for B
digitalWrite(ROW1, HIGH);
digitalWrite(ROW4, HIGH);
digitalWrite(ROW5, HIGH);
digitalWrite(ROW6, HIGH);
digitalWrite(ROW7, HIGH);
theremin_sens();
digitalWrite(ROW1, LOW);
digitalWrite(ROW4, LOW);
digitalWrite(ROW5, LOW);
digitalWrite(ROW6, LOW);
digitalWrite(ROW7, LOW);
delay(500);
// for E
digitalWrite(ROW1, HIGH);
digitalWrite(ROW2, HIGH);
digitalWrite(ROW4, HIGH);
digitalWrite(ROW6, HIGH);
digitalWrite(ROW7, HIGH);
theremin_sens();
digitalWrite(ROW1, LOW);
digitalWrite(ROW2, LOW);
digitalWrite(ROW4, LOW);
digitalWrite(ROW6, LOW);
digitalWrite(ROW7, LOW);
delay(500);
// for S
digitalWrite(ROW1, HIGH);
digitalWrite(ROW2, HIGH);
digitalWrite(ROW4, HIGH);
digitalWrite(ROW5, HIGH);
digitalWrite(ROW6, HIGH);
theremin_sens();
digitalWrite(ROW1, LOW);
digitalWrite(ROW2, LOW);
digitalWrite(ROW4, LOW);
digitalWrite(ROW4, LOW);
digitalWrite(ROW5, LOW);
digitalWrite(ROW6, LOW);
delay(500);
// for  t
digitalWrite(ROW1, HIGH);
digitalWrite(ROW4, HIGH);
digitalWrite(ROW6, HIGH);
digitalWrite(ROW7, HIGH);
theremin_sens();
digitalWrite(ROW1, LOW);
digitalWrite(ROW4, LOW);
digitalWrite(ROW6, LOW);
digitalWrite(ROW7, LOW);
delay(1000);
}

//2013
if (ran ==2){
// for 2
digitalWrite(ROW2, HIGH);
digitalWrite(ROW3, HIGH);
digitalWrite(ROW4, HIGH);
digitalWrite(ROW6, HIGH);
digitalWrite(ROW7, HIGH);
theremin_sens();
digitalWrite(ROW2, LOW);
digitalWrite(ROW3, LOW);
digitalWrite(ROW4, LOW);
digitalWrite(ROW6, LOW);
digitalWrite(ROW7, LOW);
delay(500);
// for 0
digitalWrite(ROW1, HIGH);
digitalWrite(ROW2, HIGH);
digitalWrite(ROW3, HIGH);
digitalWrite(ROW5, HIGH);
digitalWrite(ROW6, HIGH);
digitalWrite(ROW7, HIGH);
theremin_sens();
digitalWrite(ROW1, LOW);
digitalWrite(ROW2, LOW);
digitalWrite(ROW3, LOW);
digitalWrite(ROW5, LOW);
digitalWrite(ROW6, LOW);
digitalWrite(ROW7, LOW);
delay(500);
// for 1
digitalWrite(ROW3, HIGH);
digitalWrite(ROW5, HIGH);
theremin_sens();
digitalWrite(ROW3, LOW);
digitalWrite(ROW5, LOW);
delay(500);
// for 3
digitalWrite(ROW2, HIGH);
digitalWrite(ROW3, HIGH);
digitalWrite(ROW4, HIGH);
digitalWrite(ROW5, HIGH);
digitalWrite(ROW6, HIGH);
theremin_sens();
digitalWrite(ROW2, LOW);
digitalWrite(ROW3, LOW);
digitalWrite(ROW4, LOW);
digitalWrite(ROW5, LOW);
digitalWrite(ROW6, LOW);
delay(1000);
}
}
}
//If the button is not pressed, sets on the leds  <<< (it doesn’t work)
// FAST FAST FAST FAST FAST FAST FAST FAST

// for 1
digitalWrite(ROW1, HIGH);
theremin_sens2();
digitalWrite(ROW1, LOW);
// for 2
digitalWrite(ROW2, HIGH);
theremin_sens2();
digitalWrite(ROW2, LOW);
// for 3
digitalWrite(ROW3, HIGH);
theremin_sens2();
digitalWrite(ROW3, LOW);
// for 5
digitalWrite(ROW5, HIGH);
theremin_sens2();
digitalWrite(ROW5, LOW);
// for 6
digitalWrite(ROW6, HIGH);
theremin_sens2();
digitalWrite(ROW6, LOW);
// for 7
digitalWrite(ROW7, HIGH);
theremin_sens2();
digitalWrite(ROW7, LOW);

delay(10);

// REAPEAT REAPEAT REAPEAT REAPEAT REAPEAT

// for 1
digitalWrite(ROW1, HIGH);
theremin_sens2();
digitalWrite(ROW1, LOW);
// for 2
digitalWrite(ROW2, HIGH);
theremin_sens2();
digitalWrite(ROW2, LOW);
// for 3
digitalWrite(ROW3, HIGH);
theremin_sens2();
digitalWrite(ROW3, LOW);
// for 4
digitalWrite(ROW4, HIGH);
theremin_sens2();
digitalWrite(ROW4, LOW);
// for 7
digitalWrite(ROW7, HIGH);
theremin_sens2();
digitalWrite(ROW7, LOW);
// for 6
digitalWrite(ROW6, HIGH);
theremin_sens2();
digitalWrite(ROW6, LOW);
// for 5
digitalWrite(ROW5, HIGH);
theremin_sens2();
digitalWrite(ROW5, LOW);
}
}
int theremin_sens() {
for (i= 0; i <1000; i++)
{
f = factor * val - shift;
// this linearly maps the frequency to

// a value between minfreq and maxfreq

// according to the calibration result
normf = f / (double) minfreq;
// Dividing an exponential function by the min value
logf = log(normf) / log(gap);
// allows us to take the log (base gap) and the result
ilogf = round(logf);
// is the number of notes above the lowest, once we round it.

f = minfreq * pow(gap,ilogf);
// we better "unlog" it.

tone(PHONES, f);
// this produces the tone signal

}
}

int theremin_sens2() {
for (i= 0; i <150; i++)
{
f = factor * val - shift;
// this linearly maps the frequency to

// a value between minfreq and maxfreq

// according to the calibration result
normf = f / (double) minfreq;
// Dividing an exponential function by the min value
logf = log(normf) / log(gap);
// allows us to take the log (base gap) and the result
ilogf = round(logf);
// is the number of notes above the lowest, once we round it.
f = minfreq * pow(gap,ilogf);
// we better "unlog" it.

tone(PHONES, f);
// this produces the tone signal
}
}
``````