End mid loop() @ sensor input, how?

Sry for reposting here but I'm truly stuck :confused:

Been looking in to millis() w/o getting any smarter and been looking at hardware interrupt and a bunch of other
ideas.

//LEDpins
int redPin = 11;
int greenPin = 10;
int bluePin = 9;

//SENSORpins
int HALL = 2;
int BALL = 3;
int TAP = 4;

//Other
int Mood;              // Random mood
int brightness = 0;    // how bright the LED is
int fadeAmount = 5;    // how many points to fade the LED by

  
void setup(){
  //LEDpins
  pinMode(redPin, OUTPUT);
  pinMode(greenPin, OUTPUT);
  pinMode(bluePin, OUTPUT);
  setColourRgb(0,0,0); 
  
  //Sensors
  pinMode(HALL, INPUT_PULLUP);
  pinMode(BALL, INPUT_PULLUP);
  pinMode(TAP, INPUT_PULLUP);

  Serial.begin(9600);
}


void loop() {
Mood = random(11);
  //Randomise Mood
if (Mood <=1) Angry();

else if (Mood == 2 && Mood <= 5) Calm();
  
else if (Mood == 6 && Mood <= 8) Play();
  
else
{
  Serial.println("rgbFade"); 
for (int i=0; i <= 1; i++)
{
    unsigned int rgbColour[3];
    
 // Start off with red.
  rgbColour[0] = 255;
  rgbColour[1] = 0;
  rgbColour[2] = 0;  

  // Choose the colours to increment and decrement.
  for (int decColour = 0; decColour < 3; decColour += 1) {
    int incColour = decColour == 2 ? 0 : decColour + 1;

    // cross-fade the two colours.
    for(int i = 0; i < 255; i += 1) {
      rgbColour[decColour] -= 1;
      rgbColour[incColour] += 1;
      
      setColourRgb(rgbColour[0], rgbColour[1], rgbColour[2]);
      delay(20);
    }
  }
}
  Serial.println("rgbFade End");  
return;
}

//End void loop
}

 void setColourRgb(unsigned int red, unsigned int green, unsigned int blue) {
  analogWrite(redPin, red);
  analogWrite(greenPin, green);
  analogWrite(bluePin, blue);
}


void Angry(){
 Serial.println("Angry"); 
  //for (int i=0; i <= 10; i++){
   // fade in from min to max in increments of 5 points:
  for (int fadeValue = 0 ; fadeValue <= 255; fadeValue += 2) {
    // sets the value (range from 0 to 255):
    analogWrite(redPin, fadeValue);
    // wait for 30 milliseconds to see the dimming effect
    delay(30);
  }

  // fade out from max to min in increments of 5 points:
  for (int fadeValue = 255 ; fadeValue >= 0; fadeValue -= 2) {
    // sets the value (range from 0 to 255):
    analogWrite(redPin, fadeValue);
    // wait for 30 milliseconds to see the dimming effect
    delay(30);

     }
Serial.println("Angry End wrong");

  //exit Angry
  if (digitalRead (BALL) == LOW && digitalRead (TAP) == LOW) {
     Serial.println("Angry End right");
    return;
  }
else Angry();
  }
  
  

  

void Calm(){
   Serial.println("Calm");


  //code for Calm

   // fade in from min to max in increments of 5 points:
  for (int fadeValue = 0 ; fadeValue <= 255; fadeValue += 2) {
    // sets the value (range from 0 to 255):
    analogWrite(greenPin, fadeValue);
    // wait for 30 milliseconds to see the dimming effect
    delay(30);
  }

  // fade out from max to min in increments of 5 points:
  for (int fadeValue = 255 ; fadeValue >= 0; fadeValue -= 2) {
    // sets the value (range from 0 to 255):
    analogWrite(greenPin, fadeValue);
    // wait for 30 milliseconds to see the dimming effect
    delay(30);
    Serial.println("Calm end wrong");
  }
  if (digitalRead (BALL) == LOW) {
    Serial.println("Calm end right");
    return;}
  }
  
  
void Play(){

  //code for Play 
  Serial.println("play"); 
 // fade in from min to max in increments of 5 points:
  for (int fadeValue = 0 ; fadeValue <= 255; fadeValue += 2) {
    // sets the value (range from 0 to 255):
    analogWrite(bluePin, fadeValue);
    // wait for 30 milliseconds to see the dimming effect
    delay(30);
  }

  // fade out from max to min in increments of 5 points:
  for (int fadeValue = 255 ; fadeValue >= 0; fadeValue -= 2) {
    // sets the value (range from 0 to 255):
    analogWrite(bluePin, fadeValue);
    // wait for 30 milliseconds to see the dimming effect
    delay(30);
     Serial.println("play end wrong");
  }
  if (digitalRead (HALL) == LOW) {
     Serial.println("play end right");
    return;
  }
  }

I want the If-statements @ the end of every moodloop to execute as soon as the sensor gets a signal. Not having to time the sensor interaction with the end of the loop and the check of the sensorstate...

I'm at a total loss here, short of adding the If-statement every other line of the code to constantly check for a reading. There has to be a better way, doesn't it?!

1 Like