So, I copied all your code and changed the name of loop() to doTheStuff(). Then I pasted Robin's loop() function in.
That function requires a global variable called pauseSelected and it calls checkPauseButton(), which is a function we need to write.
Looking at your attempt to use the sleep library, I worked out which pin is the pause button and removed those, replacing with checkPauseButton(). This is a little complex because it needs to 'debounce' the button. That is, it ignores any changes to the button state which occur less than 10ms after the first change is detected.
Along the way, I also created a named constant for 9, since that was used in 2 places in the program and those two places must always have the same value. I also moved the zeroing of total back to the point immediately before you start adding things to it.
The doTheStuff() can't spend a lot of time in delays, so I removed all delays. It's going to end up printing a lot of crap to the Serial Monitor. Once you've verified for yourself that it's working OK, remove all those prints.
int total;
int readings;
int irnow;
int average;
int newaverage;
int potValue;
bool pauseSelected = false;
const byte pot = A3;
const byte button = 8; //the pause button - should be connected between this pin and ground so it reads LOW when pressed
const byte button2 = 10;
const int NumberOfIRReadingsToAverage = 9;
void setup() {
Serial.begin(9600);
pinMode(2, OUTPUT);
pinMode(pot, INPUT);
pinMode(button, INPUT);
pinMode(button2, INPUT);
}
void loop() {
checkPauseButton();
if (pauseSelected == false) {
doTheStuff();
}
}
void doTheStuff() {
irnow = 0;
total = 0;
while ( irnow < NumberOfIRReadingsToAverage)
{
readings = analogRead(A1);
total = total + readings;
irnow = irnow + 1;
}
average = total / NumberOfIRReadingsToAverage;
newaverage = map(average, 0, 1023, 500, 200);
Serial.println(newaverage);
potValue = analogRead(pot);
int mappedpot = map(potValue, 0, 1023, 0, 90);
if (newaverage < 390 - mappedpot)
{
if (newaverage > 385 - mappedpot)
{
digitalWrite(2, HIGH);
}
if (newaverage <= 370 - mappedpot)
{
digitalWrite(2, HIGH);
}
}
else {
digitalWrite(2, LOW);
}
}
void checkPauseButton() {
const unsigned long DebounceInterval = 10; //milliseconds - ignore button state flips faster than this
static unsigned long lastButtonPress;
static int lastButtonState = HIGH;
int buttonState = digitalRead(button);
if (lastButtonState != buttonState)
{
//the button has changed since we last looked at it
lastButtonState = buttonState;
if (millis() - lastButtonPress > DebounceInterval)
{
//it last changed a long time ago, so this is a new event
lastButtonPress = millis(); //record the time that it changed
if (buttonState == LOW)
{
pauseSelected = !pauseSelected; //flip the pause state
}
}
}
}