analogread - two states?

Apologies for my lack of programming skills!

I'm reading an analog value from an IR sensor. Dividing that value by 4 gives me a suitable range of values between (around) 0 and 150. Ultimately I want it to send a midi CC message, but to begin with, I'm using serialprint to show the value onscreen.

I want the value to, in effect, trigger a switch, so if the value goes above (say) 75, it will display a message once then wait until the value falls below 75 and display an alternate message, again, just once. I've got it showing messages, but within the loop they keep coming and I want it to just alternate between two states, like a light switch.

I've seen some complex solutions for analog switching, but feel sure there must be a simple way...

Thanks in advance

most people use a bit as a block once something has happened that they do not want to happen again

something like this

if (irSensor>=75 && onetime==0){
Serial.print (“something”);
onetime=1;
}else if (irSensor<75){
onetime=0;}

Read the value and if above 75 print the first message and set a flag variable to true to indicate that it has been printed and a second one to false. Do not print the first message whilst the first flag is true whatever the value. When the value falls below 75 print the second message and set the first flag to false and set the second one to true. Do not print the second message whilst the second flag is true whatever the value.

You should probably add hysteresis (switch off at a lower value than switch on) so if the signal is hovering around the switch value it will not wildly toggle off and on.

void loop() {
    static boolean shown=false;
    int irSensor = analogRead(irSensorPin);
    if (irSensor >= 75) {
       if (!shown) {
           Serial.print ("something");
           shown = true;
        }
    if (irSensor < 70) {
        shown = false;
    }
}

‘static’ means keep the value in memory for the next time loop() runs. It’s an alternative to making the variable global.

thanks for this - it now seems to work with the following code - if you can see any errors or bad coding, please let me know…

int current;
int maxi = 35;
int state = 0;

void setup() {
Serial.begin(9600); // open the serial port at 9600 bps:
}

void loop()
{
current = analogRead(0) / 4;
if (state == 0 && current > maxi){
state = 1;
}
if (state == 1 && current < maxi){
state = 0;
Serial.println(“changed”);
}
delay(50);
}

It's not an error but it looks like you intend 'maxi' to be a constant: a named value that doesn't change. If that is the case it should be declared 'const':const int maxi = 35; This will allow the compiler to alert you if you accidentally write code that would change the value of 'maxi'.

It is not an error but since the 'state' variable appears to be used as a 0 or 1 flag you should declare it as 'boolean'. That is a data type that only holds 0 or 1. When using a boolean variable it is appropriate to use the values 'true' and 'false' instead of 0 and 1.

It's generally a good idea to use the names A0, A1, A2,... instead of numeric constants when referencing analog input pins. The numbers will work but if you try to use a number that is too high the name will cause a compile-time error where the number will compile without error and always read 0. For example on an UNO you will get 0 if you use analogRead(8) but get a compile-time error if you try analogRead(A8).