Pages: [1]   Go Down
Author Topic: State Change Detection  (Read 369 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 4
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello,

This may be quite a simple question, but one that I myself cannot work out.
I wish to use an LDR as a beam break detector and alarm, and to do this I need to compare one value against another. The code I have so far for this is as follows:

Code:
int lightPin = 0;
int variable = 0;
int secondVariable = 0;
int differenceVariable = 0;
int ledPin = 13;
void setup() {
  Serial.begin(9600);
}

void loop() {
  variable = analogRead(lightPin);
  Serial.println(variable);
  delay(500);
  secondVariable = analogRead(lightPin);
  secondVariable-variable = differenceVariable;
  if (differenceVariable > 50) {
    digitalWrite(ledPin, HIGH)
  }
}

however,
Code:
secondVariable-variable
is not the correct syntax for a variable calculation like this.
How should it be written?

Thanks,
4r1
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 4
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I have worked it out, but will leave this post here for others to see my obvious mistake!
4r1

Code:
int lightPin = 0;
int variable = 0;
int secondVariable = 0;
int differenceVariable = 0;
int ledPin = 13;
void setup() {
  Serial.begin(9600);
}

void loop() {
  variable = analogRead(lightPin);
  Serial.println(variable);
  delay(500);
  secondVariable = analogRead(lightPin);
  differenceVariable = variable - secondVariable;
  if (differenceVariable > 50) {
    digitalWrite(ledPin, HIGH);
  }
}
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 4
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Or maybe squaring the difference variable would be better, for want of a way to find absolute values. (is there a sqrt function or similar for arduino?)

Code:
int lightPin = 0;
int variable = 0;
int secondVariable = 0;
int differenceVariable = 0;
int ledPin = 13;
void setup() {
  Serial.begin(9600);
}

void loop() {
  variable = analogRead(lightPin);
  Serial.println(variable);
  delay(500);
  secondVariable = analogRead(lightPin);
  differenceVariable = variable - secondVariable;
  if ((differenceVariable^2) > 50) {
    digitalWrite(ledPin, HIGH);
  }
}
Logged

Austin, TX
Offline Offline
Faraday Member
**
Karma: 71
Posts: 6143
Baldengineer
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
 if ((differenceVariable^2) > 50) {
For the record ^2 doesn't raise differenceVariable to the power of 2.  ^ is a bitwise XOR.  If you want to square a value, you need to use pow():  http://arduino.cc/en/Reference/Pow

Quote
(is there a sqrt function or similar for arduino?)
If you look at the Reference page (http://www.arduino.cc/en/Reference/HomePage), you will see a section on Math.  One of the operators is sqrt().
« Last Edit: October 30, 2011, 09:35:21 am by James C4S » Logged

Capacitor Expert By Day, Enginerd by night.  ||  Personal Blog: www.baldengineer.com  || Electronics Tutorials for Beginners:  www.addohms.com

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 291
Posts: 25884
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The ^ operator is the exclusive or operator in C
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Pages: [1]   Go Up
Jump to: