Pages: [1]   Go Down
Author Topic: Please explain the code  (Read 666 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Jr. Member
**
Karma: 0
Posts: 50
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Can someone explain this to me.
I am really noob in programming but I want to learn, btw I can understand some of the codes but a bit confuse.
This is for our project pulse oximeter. Using a
 Nellcor MAX-A http://www.medical-supplies-equipment-company.com/product/ppf/id/13446/new_prod_full.asp
where the output of the pulse oximeter is inputed in the arduino decimila.

Another question: How can I change its Aref to higher voltage? just to light a LED.
Thanks in advance!


Here's the code:
Code:
int ledPin = 13;
int sensorPin = 0;

doouble alpha =0.75;
int period = 20;
double change = 0.0;

void setup()
{
pinMode(ledPin,OUTPUT);
Serial.begin(115200);
}

void loop()
{
static double oldValue = 0;
static double oldChange = 0;
int rawValue = analogRead(sensorPin);
double value = alpha * oldValue + (1-alpha)*rawValue;

digitalWrite(ledPin,(change<0.0 && oldChange >0.0));

Serial.println(value);

oldValue= value;
oldChange =change;
delay(period);
}


Moderator edit: [code] [/code] tags added.
« Last Edit: May 09, 2012, 03:05:59 am by Coding Badly » Logged

Arduino Projects Problem!?
Visit my blog! Free download(PDF File) for 170+ projects and I will be adding!
Here: http://badassarduinoprojects

Gosport, UK
Offline Offline
Faraday Member
**
Karma: 21
Posts: 3113
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

What do mean by
Quote
How can I change its Aref to higher voltage?

Change the Aref of what?
Logged

Australia
Offline Offline
Jr. Member
**
Karma: 0
Posts: 59
char CoolThings[] = "Vegemite", "Swimming", "Arduino";
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Its really quite simple to change the AREF of the Arduino (I assume that that's what you're talking about, if not sorry  smiley-confuse).  Put whatever voltage you want the AREF to be into the AREF pin of the Arduino.

Koop
« Last Edit: May 09, 2012, 01:56:12 am by Koop » Logged

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

Quote
Put whatever voltage you want the AREF to be into the AREF pin of the Arduino.
...as the long as it is less than or equal to the supply voltage.
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.

Sydney, Australia
Offline Offline
Edison Member
*
Karma: 33
Posts: 1254
Big things come in large packages
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
// declare a couple of floating point variables (double float).
// Static variables are not destoyed between calls to loop() and will be remembered for the next time
static double oldValue = 0;
static double oldChange = 0;

// declare an integer variable and initialise it witrh a value read from the sensor (0 to 1023)
int rawValue = analogRead(sensorPin);

// declare a floating point variable and do some sort of conversion from the raw sensor input to a value
// this calculaton means something to you if you know what sensor you are using
double value = alpha * oldValue + (1-alpha)*rawValue;

// Write a 0 or 1 to the digital output ledPin. The value written is a boolean that works out to false(0) if change and OldChange don't meet the conditions, and true (>0) if they do. This can be confusing until you get used to it.
digitalWrite(ledPin,(change<0.0 && oldChange >0.0));

Serial.println(value);

// save the values in variable that remain (persist) for the next time that loop is called
oldValue= value;
oldChange =change;

// wait a while and then do it all over again
delay(period);
Moderator edit : Code tags added.

I think there is a mistake in this code as change is never set in the code but is assigned to oldChange. After the first time through both change and oldChange will be 0.0.

Hope this helps.
« Last Edit: May 09, 2012, 04:40:02 am by marco_c » Logged

Arduino libraries http://arduinocode.codeplex.com
Parola hardware & library http://parola.codeplex.com

0
Offline Offline
Shannon Member
****
Karma: 200
Posts: 11672
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Put whatever voltage you want the AREF to be into the AREF pin of the Arduino.
...as the long as it is less than or equal to the supply voltage.

So long as it is between 1.0V and the supply voltage, is a low-impedance source and provided you call analogReference(EXTERNAL) in your setup() before any calls to analogRead().
Logged

[ I won't respond to messages, use the forum please ]

Offline Offline
Jr. Member
**
Karma: 4
Posts: 68
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi,
the following line is a low pass digital filter: it smooths the analog input value by cutting the
high frequencies, "alpha" is the time constant of the filter.

Quote
Code:
// declare a floating point variable and do some sort of conversrion from the raw sensor input to a value
// this calculaton means something to you if you know what sensor you are using
double value = alpha * oldValue + (1-alpha)*rawValue;
Logged

Pages: [1]   Go Up
Jump to: