Stuck on simple project!

Hiya,

I've been using this forum for a little while for a few small projects, however being a novice still I'm having trouble with this one and am very short for time, so would like to ask for some help! :slight_smile:

The function of the project is to replicate the process of activating a distress beacon.
An accelerometer and thermistor provide a value, and if this value goes out of a "safe range" (eg above 50 degrees or below-10), or if a large shock is detected, then an LED 1 illuminates (this is an alarm on the distress beacon).
If the user does not press a button to turn the LED 1 off (disabling the alarm on the distress beacon) within 5 seconds, then the LED 1 turns off, and LED 2 turns on (which would mean that the distress signal is sent in the real product).

So when LED 1 is on, the idea is that you can press a button (the "I'm OK") to prevent the distress signal being sent out (LED 2 turning on), and it loops back to monitoring the thermistor/accelerometer. If you don't press the button within 5 seconds, then LED 2 turns on automatically.
I've got the accelerometer/thermistor working to illuminate LED 1.

But I am stuck with the rest, and am just going round in circles making silly mistakes. If someone could help it would be much appreciated. Bear in mind that I am very new to this so fully explaining everything would be very helpful!

My code so far is here:

int accelerometer = 2;    // accelerometer
int thermistor = 3; // thermistor
int LED1 = 11; // activation LED
int LED2 = 10; // signal sent LED
int button = 9; //disable button
int delay1 = 1;

void setup() {
  
  pinMode(LED1, OUTPUT);  
  pinMode(LED2, OUTPUT);
  pinMode(thermistor, INPUT);
  pinMode(button, INPUT);
  pinMode(accelerometer, INPUT);
}

void loop() {
  // read the value from the sensor:
  int acc = analogRead(accelerometer); 
  int therm = analogRead(thermistor);  
  // Can convert to volts if easier using: analogRead(thermistor) * (5/1023)
  
  Serial.begin(9600);
  Serial.print(acc); Serial.println(therm);
  
 while (acc > 10) digitalWrite(LED1, HIGH);  //this value can be upto 1023 (this is the nalog input converted to digital signal)
 while (acc < 1) digitalWrite(LED1, HIGH);
 while (therm > 10) digitalWrite(LED1, HIGH);
 while (therm < 1) digitalWrite(LED1, HIGH);
 

if (delay1 < 3000) ;
    if (button == HIGH) ;
      digitalWrite(LED1, LOW);   //PRESS DIABLE BUTTON TO DEACTIVATE
if (delay1 > 6000);     
         {  digitalWrite(LED1, LOW);
           digitalWrite(LED2, HIGH); }
delay1 = delay1 + 1; 
}

Many thanks

Joe
(moderatore update: added code tags)

  Serial.begin(9600);

Belongs in setup() (done once) not loop() (done over and over).

 while (acc > 10) digitalWrite(LED1, HIGH);  //this value can be upto 1023 (this is the nalog input converted to digital signal)
 while (acc < 1) digitalWrite(LED1, HIGH);
 while (therm > 10) digitalWrite(LED1, HIGH);
 while (therm < 1) digitalWrite(LED1, HIGH);

Absolutely, positively not. If acc is greater than 10, the while loop will never end. If acc is less than 1, the while loop will never end.

These should be if statements, not while statements.

if (delay1 < 3000) ;

If delay1 is less than 3000, do nothing (that's what the ; on the end means). Otherwise, do nothing. So, why bother testing?

    if (button == HIGH) ;

Another do nothing test. And, 9 will never equal HIGH. Somewhere, you probably want to read the state of the pin named in button (which is a crappy name for a variable holding a pin number). If you used meaningful names, you would know not to compare buttonPin to HIGH. And, since buttons are for shirts, switchPin is even better as a name.

Hi Paul,

thanks for the reply.

I was using "if" statements initially, but couldnt get the LED's to light up at all. (Simply by substituting the "if" with "while" they started reading). I'll change them back to "if"!

Using:

if (acc > 10) digitalWrite(LED1, HIGH); //this value can be upto 1023 (this is the nalog input converted to digital signal)
delay(3000);
if (acc < 1) digitalWrite(LED1, HIGH);
delay(3000);
if (therm > 10) digitalWrite(LED1, HIGH);
delay(3000);
if (therm < 1) digitalWrite(LED1, HIGH);
delay(3000);

The LED turns on for 3 seconds. But by using "delay" am I correct in thinking that a button/switch press will do nothing until after the delays are finished?

What code would I use so that a button/switch press whilst LED 1 is on reverts it back to monitoring the acc/therm inputs, and if nothing is pressed then LED 2 illuminated after 5 seconds?

Would I use "millis" instead of "delay"?

Get rid of all the delay()s and use the technique in the Blink Without Delay example sketch.

I wrote and extended demo of BWOD in the first post in this Thread. It might provide a model for what you want to do.

...R

Thanks Robin, I'll give it a shot tomorrow and see how it turns out!

Thanks

Joe