Alarm Project for University

panda.ino (2.78 KB)

To turn it off, the LDR has to detect a value of 200-500 (I’m unsure how to code this).

int ldrVal = analogRead(ldrPin);
if(ldrVal >= 200 && ldrVal <= 500)
{
   // Shut the f**k up!
}

Most of your global variables should NOT be global. Variables that contain pin numbers should be global. Variables that contain pin state should not be global.

 // initialize the pushbutton pin as an input:

While this MIGHT be useful once, it is NOT useful three times.

 // check if the pushbutton is pressed.

The code then checks three switch states. Make the (useless) comments match what the code ACTUALLY does.

int sensorValue = analogRead(A3);
     float tsens =  map(sensorValue, 0, 1023, 0, 70);

map() does NOT return a float.

{ digitalWrite(led2, HIGH);
  delay(100);
  digitalWrite(led2, LOW);
  delay(100);}
  {digitalWrite(led3, HIGH);
  delay(100);
  digitalWrite(led3, LOW);
  delay(100);}// wait for a second }

Useless curly braces AND an incorrect and useless comment

{sensorValue = analogRead(sensorPin); // read the value from the LDR
Serial.println(sensorValue); //prints the values coming from the LDR on the screen
delay(100);
 if (sensorValue == 0-450){
  noTone(16);
     }}

Useless curly braces; incorrect comparison (the value returned by analogRead() will NEVER equal -450; useless comments.

Why doesn’t the pin name describe the KIND of sensor? Why doesn’t the state variable describe the KIND of value? Yeah, the pin has a sensor connected, and the value was read from a sensor. But what KIND of sensor?

For a university project, I expect better code than this. Perhaps your instructor won’t (but I wouldn’t count on that).

eleosa: I just don't understand why you couldn't have been kinder in your response...?

You need to be cautious about being fragile.

The only marginally unkind comment could have been the last line - but in fact many who post here are University instructors. :astonished:

We also presume that any competent instructor who sets assignments specifying Arduinos would in any case be monitoring this very forum, would they not?

I'm inclined to agree with @PaulS.

I am particularly unimpressed by statements from adult students like "lack of guidance in this module by tutors". If you don't understand something then ask your tutor - that's what s/he is paid for. If the problem is that you didn't take the trouble to listen carefully when the instructions were being given then just apologize and ask the tutor again.

At university level it should not be necessary to explain "By the way, A0 = the LDR and A3 and A4 = the two potentiometers". It should be clear in the code for YOUR benefit.

I don't actually understand what the problem is from the words in your Original Post. It sounds like a pretty straightforward task that has not been thought about carefully and systematically. I could understand if you had (say) an LDR and did not know how to get values from it. But I can't understand why you have problems with the overall logic of the task - which is what seems to me to be the case.

I don't understand why you are using float variables when integers should do the job perfectly well and much faster.

I don't understand why the line sensorValue = analogRead(A3); appears more than once in the program.

Have a look at Planning and Implementing a Program

...R