3 seperate circuits, trouble integrating code

So, been having fun doing all the tutorials with my new UNO board. I'm a novice to arduino so bare with me. I have devised a simple circuit consisting of a buzzer on pin13, 2 led's on pins 11 and 9, the circuit for the button tutorial and a photoresister sensor circuit using a voltage divider circuit. Pretty basic stuff, all of the circuits work separately but I'm trying to integrate the circuits together. I'm a little rough with the programming aspect. "IF" statements are a nightmare for me to figure out on my own. I'll list the circuit run down and how I would like it to work. If anyone can give me a direction to head or a new command I'm not familiar with. Thanks guys.

Laser trip wire with a arm and disarm button. Serial read the Analog input, if photoresistor low and if arm indicator LED is high then the buzzer should sound.

So far what I have is below.

const int sensor = A0; // Photoresister read pin

int switchPin = 2; // switch is connected to pin 2 int armLED = 11; // Red LED Pin int disarmLED = 9; // Green LED Pin int alarm = 13; // Buzzer Pin

int val; // variable for reading the pin status int val2; // variable for reading the delayed/debounced status int buttonstate; // variable to hold the button state

int systemMode = 0; // Is the light on or off?

int sensorValue = 0; //photoresister pin

void setup() { pinMode(switchPin, INPUT); // Set the switch pin as input

pinMode(armLED, OUTPUT); pinMode(disarmLED, OUTPUT);

Serial.begin(9600); // Set up serial communication at 9600bps buttonstate = digitalRead(switchPin); // read the initial state }

void loop(){ int sensorValue = analogRead(A0);

val = digitalRead(switchPin); // read input value and store it in val delay(10); // 10 milliseconds is a good amount of time val2 = digitalRead(switchPin); // read the input again to check for bounces if (val == val2) { // make sure we got 2 consistant readings! if (val != buttonstate) { // the button state has changed! if (val == LOW) { // check if the button is pressed if (systemMode == 0) { // Armed or Disarmed systemMode = 1; // ARMED!! digitalWrite(armLED, HIGH); digitalWrite(disarmLED, LOW); } else { systemMode = 0; // Disarmed! digitalWrite(armLED, LOW); digitalWrite(disarmLED, HIGH); } } } buttonstate = val; } }

I’m looking at the “while” sketch and I’m completely confused. Would this be the proper application for this. While checking to see if the systemMode is 0 or 1. read analog to see if < 500, if systemMode is 1 and sensorValue is < 500 then buzzer Pin HIGH. Ughhh I have such a hard time with structuring my own code.

The code you posted does something. You want it to do something. If those two things were the same, it is unlikely that you would be posting. So, I think that it is safe to assume that the code does not do what you want.

What it does do, and how that differs from what you want it to do, though, is not at all clear.

Ughhh I have such a hard time with structuring my own code.

Write the comments first. Describe what you want the program to do.

Then, insert some code. Rewrite the comments, or add more, to describe what decisions were made, why, and what still, or now, needs to be done.

This way, at least when you get stuck, your thought processes are clear, and we can help you clarify your thoughts or make the code do what the comments say.

Well that helped me....