PC voice alarms

Hi guys,
I love this Arduino thing…

But I’m not the brightest light in the house, sorry

please see code

I cut it down for simplicity here so it gives an easy look into what I’m trying to achieve

What I want is for the code to stop when the last line is complete and will not repeat in the loop

I have seen alternate ways to program this but its way over my head

I tried as hard as I can but cant get my head around while loops if else and map coding which may actually be the right solution to this project, sorry

requirements:
when the PC is switched on Analog0 reads input of 5v (high)
this turns on an OUTPUT which plays a sound file that the PC is ON
when the PC is shutdown the Analog1 goes LOW and plays a sound file the PC is OFF

All conditions must be read once only per PC restart

I’m out of ideas really

Maybe a whole new code is necessary ?

I tried MAP but am confused how to implement it

Any suggestions please ?

const int PCoff = A0;     // pin flowsensor is attached to
   const int FET2 = 5;             // pin for Fet2
   int relay = 12; 
   const int threshold = 850;      // arbitrary threshold level in range of analog input
 
   void setup(){
   
     delay (1000);
   
   int FET1 = 3;                      // PSU 5V otherwise nothing happens
   pinMode(FET1, OUTPUT);
   digitalWrite(FET1, HIGH);          //  FET1 ON 
   
     delay (1000);
   
   pinMode(relay, OUTPUT);
   digitalWrite(relay, HIGH);        // relay switch ON here   
   
     delay (3000);                     // mp3 voice play time says start seq initialised
   
   digitalWrite(relay, LOW);         // relay switch OFF here
   
     delay (1000);                     //if theres a pop sound add short delay time
   
   digitalWrite(FET1, LOW);          // FET1 switch OFF
   }
 
   void loop()
  {
                           //   FET2 pc shutdown complete
                          
  //delay(2000);
  int analogValue0 = analogRead(PCoff);                  // read analog0 input
  pinMode(FET2, OUTPUT);
 
 /*                             =============================
                                  ANALOG IN LED1 OUTPUTS HERE                        */
            int LED1GREEN = 2; 
            int LED1RED = 4; 
            pinMode(LED1GREEN, OUTPUT);
            pinMode(LED1RED, OUTPUT);
            if (analogValue0 > threshold){digitalWrite(LED1GREEN, HIGH);}
            if (analogValue0 > threshold){digitalWrite(LED1RED, LOW);}
            if (analogValue0 < threshold){digitalWrite(LED1RED, HIGH);}
            if (analogValue0 < threshold){digitalWrite(LED1GREEN, LOW);}
            
                     /*===========================================*/
                     
  if (analogValue0 < threshold){digitalWrite(FET2, HIGH);}    // if analog0 0Volts turn on FET2
  delay (1000);
  pinMode(relay, OUTPUT);
  if (analogValue0 < threshold){digitalWrite(relay, HIGH);}   // relay switch ON here 
  delay (3000);                                               // mp3A play time
  digitalWrite(relay, LOW);                                   // relay switch OFF here
  delay (200);   
  digitalWrite(FET2, LOW);                                    // FET2 switch OFF
                       
                         
  }

I tried MAP but am confused how to implement it

Why?

The state change detection example is where you need to start. You need to detect when the signal changes from 0 to 5V and when it changes from 5V to 0.

Note that those happen to be what the digital pins read when on and off. Your signal is a digital one. Reading it with an analog pin doesn't make sense.

Is it ok to use the digital pins as input from an external 5v source ?

I thought digital inputs needed to go to ground like via a sensor ?

use the code brackets for your code. It makes reading it much easier for anyone trying to help. Its the icon with the sheet that has <> in the middle ``

Is it ok to use the digital pins as input from an external 5v source ?

Yes.

I thought digital inputs needed to go to ground like via a sensor ?

No. The pin doesn't know, or care, why it is seeing 0 or 5V.

ok thanks

tried another code which works ok, but it has goto

tried other codes but cant find examples I can hack and learn from, nor can I write code at the level needed to make this work seamlessly and far more efficient

If I could put var, inputs, map, read, break, continue commands together that would be great

I wish I could spend the next year learning C but I cant

digital inputs will be used please ignore the analog ones for now

experimented with button counts, var, pullups/downs, button reads, input reads/counts, just cant figure how to put it together

the code is incomplete, this is just the LOW input cycle

 /* This will check each input 5v or 0v and loop until a new condition is read*/

const int analogPin = A0;    // pin that the sensor is attached to
const int ledPin = 12;       // pin that the LED is attached to
const int threshold = 850;   // an arbitrary threshold level that's in the range of the analog input

void setup() {
  pinMode(ledPin, OUTPUT);  // initialize the LED pin as an output:
  Serial.begin(9600);}

void loop() {
  start:
  Serial.println("First loop");
  delay(10);
  int analogValue = analogRead(analogPin);  // read the value of Analog pin A0

  if (analogValue<threshold){goto startdelay;}      // if the analog value is high turn on the LED:
  if(analogValue>threshold){digitalWrite(ledPin,LOW);}   //this is needed to force ledPin low
  else {goto start;}
  
  startdelay:
  void loop();{
  if (analogValue>threshold){goto start;}
  if (analogValue<threshold){digitalWrite(ledPin,HIGH);}   // voice alarm plays for 3 sec
  delay(3000);   // voice alarm plays for 3 sec
  Serial.println("Analog pin A0 is low so PC SHUTDOWN COMPLETE");
  Serial.println("Second loop done");
  delay(10);
       
  {goto start;}

   }}

requirements:
when the PC is switched on Analog0 reads input of 5v (high)
this turns on an OUTPUT which plays a sound file that the PC is ON
when the PC is shutdown the Analog1 goes LOW and plays a sound file the PC is OFF

How is the Arduino powered ?

Hi HeliBob,
it will be powered by 5VSB off the PSU

firehorse:
Hi HeliBob,
it will be powered by 5VSB off the PSU

If this is a separate PSU then the Arduino will be powered all the time. How is it connected to the PC ?

The PC PSU is connected to the Power Vin pin on the Arduino

(A FET board shield plugs onto the Arduino, provides 5V power to the Arduino)

So you want the arduino to play a tune when it's own power supply is dead?

the PC PSU is on standby, providing 5VSB to the Arduino, but the PC is off