Pages: [1]   Go Down
Author Topic: how to combine the two programs together?  (Read 885 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 34
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi peeps, i would like to combine the voltage and the mercury tilt switch program together with the SPO2 program, how do i go about? And i would like the delay for the voltage and mercury tilt program to be 400, whereas the SPO2 program will be 100, is it possible to make two delays separately? Please help....
Voltage and mercury tilt switch program
const int mercPin0 = 2;
const int tiltPin3 = 4;
const int mercuryPin5 = 6;
const int tiltyPin7 = 9;


void setup( )
{
  pinMode(mercPin0, INPUT);
  pinMode(tiltPin3, INPUT);
  pinMode(mercuryPin5, INPUT);
  pinMode(tiltyPin7, INPUT);
 
  Serial.begin (19200);
}


void loop( )
{
 
  float voltage = 0;
  int sensorValue = analogRead (A0);
  voltage = sensorValue * 0.0048; // code width(sensitivity) = 5 / 210  = 4.8mV
  Serial.print (voltage, DEC);
  int states  = digitalRead(mercPin0);
  if(states == HIGH)
    Serial.print(",O");
  else
    Serial.print(",C");
   

    int state  = digitalRead(tiltPin3);
  if (state == HIGH)
    Serial.print("O");
   
  else
    Serial.print("C");
   
   
    int condition  = digitalRead(mercuryPin5);
  if(condition == HIGH)
    Serial.print("O");
  else
    Serial.print("C");
   
     
    int conditions  = digitalRead(tiltyPin7);
  if(conditions == HIGH)
    Serial.println("O");
  else
    Serial.println("C");
   
  delay(400);
}



SPO2 program
#include <NewSoftSerial.h>
NewSoftSerial nss(10,11); // Put your TX and RX pins here, in the correct order
byte vals[3];
int heartRate, oxyLevel;

void setup()
{
  nss.begin(19200);
  Serial.begin(19200);
}
void loop()
{
  if(nss.available() >= 3)
  {
    vals[0] = nss.read(); //represent byte 1
    vals[1] = nss.read(); //represent byte 2
    vals[2] = nss.read(); //represent byte 3
bitWrite(heartRate, 0, bitRead(vals[1], 0)); // Set bit 0
bitWrite(heartRate, 1, bitRead(vals[1], 1)); // Set bit 1
 bitWrite(heartRate, 2, bitRead(vals[1], 2)); // Set bit 2
 bitWrite(heartRate, 3, bitRead(vals[1], 3)); // Set bit 3
 bitWrite(heartRate, 4, bitRead(vals[1], 4)); // Set bit 4
 bitWrite(heartRate, 5, bitRead(vals[1], 5)); // Set bit 5
 bitWrite(heartRate, 6, bitRead(vals[1], 6)); // Set bit 6
 bitWrite(heartRate, 7, bitRead(vals[0], 0)); // Set bit 7(Vals[0])
 bitWrite(heartRate, 8, bitRead(vals[0], 1)); // Set bit8(Vals[0])
   
  bitWrite(oxyLevel, 0, bitRead(vals[2], 0)); // Set bit 0
  bitWrite(oxyLevel, 1, bitRead(vals[2], 1)); // Set bit 1
  bitWrite(oxyLevel, 2, bitRead(vals[2], 2)); // Set bit 2
  bitWrite(oxyLevel, 3, bitRead(vals[2], 3)); // Set bit 3
  bitWrite(oxyLevel, 4, bitRead(vals[2], 4)); // Set bit 4
  bitWrite(oxyLevel, 5, bitRead(vals[2], 5)); // Set bit 5
  bitWrite(oxyLevel, 6, bitRead(vals[2], 6)); // Set bit 6
   
  Serial.print("Heart rate: ");
  Serial.println(heartRate);
  Serial.print("Oxygen level: ");
  Serial.println(oxyLevel);
  }
    delay (100);
}
Logged

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

Can't you just rename the two "loop" functions and call them in a new sketch?
If necessary, use only "delay(100) and call the one that needs running at 400millisecond intervals every fourth time.

This:
Code:
bitWrite(heartRate, 0, bitRead(vals[1], 0)); // Set bit 0
bitWrite(heartRate, 1, bitRead(vals[1], 1)); // Set bit 1
 bitWrite(heartRate, 2, bitRead(vals[1], 2)); // Set bit 2
 bitWrite(heartRate, 3, bitRead(vals[1], 3)); // Set bit 3
 bitWrite(heartRate, 4, bitRead(vals[1], 4)); // Set bit 4
 bitWrite(heartRate, 5, bitRead(vals[1], 5)); // Set bit 5
 bitWrite(heartRate, 6, bitRead(vals[1], 6)); // Set bit 6
 bitWrite(heartRate, 7, bitRead(vals[0], 0)); // Set bit 7(Vals[0])
 bitWrite(heartRate, 8, bitRead(vals[0], 1)); // Set bit8(Vals[0])
    
  bitWrite(oxyLevel, 0, bitRead(vals[2], 0)); // Set bit 0
  bitWrite(oxyLevel, 1, bitRead(vals[2], 1)); // Set bit 1
  bitWrite(oxyLevel, 2, bitRead(vals[2], 2)); // Set bit 2
  bitWrite(oxyLevel, 3, bitRead(vals[2], 3)); // Set bit 3
  bitWrite(oxyLevel, 4, bitRead(vals[2], 4)); // Set bit 4
  bitWrite(oxyLevel, 5, bitRead(vals[2], 5)); // Set bit 5
  bitWrite(oxyLevel, 6, bitRead(vals[2], 6)); // Set bit 6
    
is over-complicated (and over-commented) - the statements involving oxyLevel are completely pointless when a simple assignment would suffice.
Some simple binary arithmetic would sort the other one out.

Code:
Serial.print (voltage, DEC);
  Printing a float to ten decimal places is a little ambitious.
« Last Edit: March 07, 2011, 08:41:42 am by AWOL » 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.

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

how do we call it for every fourth time?
Logged

UK
Offline Offline
Faraday Member
**
Karma: 17
Posts: 2884
Gorm deficient
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
how do we call it for every fourth time?

?

Code:
void loop()
{
  loop_100ms ();  //this has 100ms delay in it
  loop_100ms ();
  loop_100ms ();
  loop_100ms ();
  loop_400ms ();   // this has NO delay in it
}
Logged

Per Arduino ad Astra

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

Thanks for your quick reply. I got a problem though. I attempt to upload this sketch below onto my Arduino Pro Mini via the 6 pins header of the 5V FTDI Basic Breakout Board.

Code:
const int mercPin0 = 2;
const int tiltPin3 = 4;
const int mercuryPin5 = 6;
const int tiltyPin7 = 9;

#include <NewSoftSerial.h>
NewSoftSerial nss(10,11); // Put your TX and RX pins here, in the correct order

byte vals[3];
int heartRate, oxyLevel;



void setup( )
{
  pinMode(mercPin0, INPUT);
  pinMode(tiltPin3, INPUT);
  pinMode(mercuryPin5, INPUT);
  pinMode(tiltyPin7, INPUT);
 
  nss.begin(9600); // Or whatever is appropriate
 
  Serial.begin (9600);
}


void loop( )
{
 
  float voltage = 0;
  int sensorValue = analogRead (A0);
  voltage = sensorValue * 0.0048; // code width(sensitivity) = 5 / 210  = 4.8mV
  Serial.print (voltage, DEC);
  int states  = digitalRead(mercPin0);
  if(states == HIGH)
    Serial.print(",O");
  else
    Serial.print(",C");
   

    int state  = digitalRead(tiltPin3);
  if (state == HIGH)
    Serial.print("O");
   
  else
    Serial.print("C");
   
   
    int condition  = digitalRead(mercuryPin5);
  if(condition == HIGH)
    Serial.print("O");
  else
    Serial.print("C");
   
     
    int conditions  = digitalRead(tiltyPin7);
  if(conditions == HIGH)
    Serial.print("O");
  else
    Serial.print("C");
    if(nss.available() >= 3)
  {
    vals[0] = nss.read(); //represent byte 1
    vals[1] = nss.read(); //represent byte 2
    vals[2] = nss.read(); //represent byte 3
   
    heartRate = 0; // Sets all 8 bits to 0
    oxyLevel = 0; // Sets all 8 bits to 0

    bitWrite(heartRate, 0, bitRead(vals[1], 0)); // Set bit 0
    bitWrite(heartRate, 1, bitRead(vals[1], 1)); // Set bit 1
    bitWrite(heartRate, 2, bitRead(vals[1], 2)); // Set bit 2
    bitWrite(heartRate, 3, bitRead(vals[1], 3)); // Set bit 3
    bitWrite(heartRate, 4, bitRead(vals[1], 4)); // Set bit 4
    bitWrite(heartRate, 5, bitRead(vals[1], 5)); // Set bit 5
    bitWrite(heartRate, 6, bitRead(vals[1], 6)); // Set bit 6
    bitWrite(heartRate, 7, bitRead(vals[0], 0)); // Set bit 7(Vals[0])
    bitWrite(heartRate, 8, bitRead(vals[0], 1)); // Set bit 8(Vals[0])
   
    oxyLevel = vals[2];

    Serial.print(heartRate);
    Serial.println(oxyLevel);
     
    delay(100);
  }
}

However, during uploading, the rx pin of the 5V FTDI Basic Breakout Board keep blinking. What is the problem? I cannot open serial monitor too.
Logged

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

Sorry, can't help I have never used the setup you're using, but it is unlikely to be a problem with your code.
Have you tried uploading any of the example, known-good sketches?

Code:
heartRate = 0; // Sets all 8 bits to 0

It is best if your comments at least match your code.]
"heartRate" is a 16 bit quantity.

Code:
oxyLevel = 0; // Sets all 8 bits to 0
....   
    oxyLevel = vals[2];
If you're going to overwrite it, why bother zeroing it?
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.

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

This is my final program
Code:
const int mercPin0 = 2;
const int tiltPin3 = 4;
const int mercuryPin5 = 6;
const int tiltyPin7 = 9;

#include <NewSoftSerial.h>
NewSoftSerial nss(10,11); // Put your TX and RX pins here, in the correct order

byte vals[3];
int heartRate, oxyLevel;



void setup( )
{
  pinMode(mercPin0, INPUT);
  pinMode(tiltPin3, INPUT);
  pinMode(mercuryPin5, INPUT);
  pinMode(tiltyPin7, INPUT);
 
  nss.begin(9600); // Or whatever is appropriate
 
  Serial.begin (9600);
}


void loop( )
{
 
  float voltage = 0;
  int sensorValue = analogRead (A0);
  voltage = sensorValue * 0.0048; // code width(sensitivity) = 5 / 210  = 4.8mV
  Serial.print (voltage, DEC);
  int states  = digitalRead(mercPin0);
  if(states == HIGH)
    Serial.print(",O");
  else
    Serial.print(",C");
   

    int state  = digitalRead(tiltPin3);
  if (state == HIGH)
    Serial.print("O");
   
  else
    Serial.print("C");
   
   
    int condition  = digitalRead(mercuryPin5);
  if(condition == HIGH)
    Serial.print("O");
  else
    Serial.print("C");
   
     
    int conditions  = digitalRead(tiltyPin7);
  if(conditions == HIGH)
    Serial.print("O");
  else
    Serial.print("C");
    if(nss.available() >= 3)
  {
    vals[0] = nss.read(); //represent byte 1
    vals[1] = nss.read(); //represent byte 2
    vals[2] = nss.read(); //represent byte 3
   
    heartRate = 0; // Sets all 8 bits to 0
    oxyLevel = 0; // Sets all 8 bits to 0

    bitWrite(heartRate, 0, bitRead(vals[1], 0)); // Set bit 0
    bitWrite(heartRate, 1, bitRead(vals[1], 1)); // Set bit 1
    bitWrite(heartRate, 2, bitRead(vals[1], 2)); // Set bit 2
    bitWrite(heartRate, 3, bitRead(vals[1], 3)); // Set bit 3
    bitWrite(heartRate, 4, bitRead(vals[1], 4)); // Set bit 4
    bitWrite(heartRate, 5, bitRead(vals[1], 5)); // Set bit 5
    bitWrite(heartRate, 6, bitRead(vals[1], 6)); // Set bit 6
    bitWrite(heartRate, 7, bitRead(vals[0], 0)); // Set bit 7(Vals[0])
    bitWrite(heartRate, 8, bitRead(vals[0], 1)); // Set bit 8(Vals[0])
   
    oxyLevel = vals[2];

    Serial.print(heartRate);
    Serial.println(oxyLevel);
     
    delay(400);
  }
}


how should i type the loop delay into the prgram if i have to use two delays of 100ms and 400ms...
Logged

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

Like in reply #3.
(uncompiled, untested, still with scope for improvement.)
Code:
const int mercPin0 = 2;
const int tiltPin3 = 4;
const int mercuryPin5 = 6;
const int tiltyPin7 = 9;

#include <NewSoftSerial.h>
NewSoftSerial nss(10,11);

void setup( )
{
  pinMode(mercPin0, INPUT);
  pinMode(tiltPin3, INPUT);
  pinMode(mercuryPin5, INPUT);
  pinMode(tiltyPin7, INPUT);
  
  nss.begin(9600);
  Serial.begin (9600);
}

void loop ()
{
  fn1 ();
  delay (100);

  fn1 ();
  delay (100);

  fn1 ();
  delay (100);

  fn1();
  fn2 ();
  delay (100);
}


void fn1( )
{
  int sensorValue = analogRead (A0);
  float voltage = sensorValue * 0.0048; // code width(sensitivity) = 5 / 210  = 4.8mV
  Serial.print (voltage, 2);

  if( digitalRead(mercPin0))
    Serial.print(",O");
  else
    Serial.print(",C");

  if ( digitalRead(tiltPin3))
    Serial.print("O");
  else
    Serial.print("C");
    
  if(digitalRead(mercuryPin5))
    Serial.print("O");
  else
    Serial.print("C");

  if( digitalRead(tiltyPin7))
    Serial.print("O");
  else
    Serial.print("C");
}

void fn2 ()
{    
  if(nss.available() >= 3)
  {
    int heartMS = nss.read();
    int heartLS = nss.read();
    int oxyLevel = nss.read();
    int heartRate = ((heartMS & 3) << 8) + heartLS;

    Serial.print(heartRate);
    Serial.println(oxyLevel);
  }
}

At some point in the future, you're going to look at this:
Code:
voltage = sensorValue * 0.0048; // code width(sensitivity) = 5 / 210  = 4.8mV
and think "?"

I remember a comment about the futility of this:
Code:
Serial.print (voltage, DEC);


EDIT: Have you external pull-ups/pull-downs on the switch pins?
« Last Edit: March 08, 2011, 10:24:20 am by AWOL » 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.

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

ya i have external pull ups for the switches. Guys, i tried the program written by AWOL. My serial monitor doesn't show my heartrate and oxylevels readings when i connect my Pro Mini to the FTDI Breakout Board and open serial monitor. For my voltage and tilt switches combinations, the readings are super fast, not according to the delay. Why is this happening!?!?! smiley-sad-blue
Logged

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

Quote
my voltage and tilt switches combinations, the readings are super fast, not according to the delay. Why is this happening!?!
Because I got fn1 and fn2 the wrong way around.
« Last Edit: March 09, 2011, 04:05:06 am by AWOL » 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: