Need help with my code using color sensor and pressure sensor with Serial monitor

Hello guys! I am using aa color sensor and force sensitive resistor. This is my code for now, but I don't understand why I get this in the monitor. First off, here's my code:

#include <LiquidCrystal.h>
const int color = 2;
int LED = 7;
int colorStat = 0;
const int FSR = A0; //* Force Sensitive Resistors
int pressSense = 0; //* Force Sensitive Resistor's Statistics

unsigned long startTime;
unsigned long elapsedTime;

void setup() {
pinMode(color, INPUT);
pinMode(LED, OUTPUT);
pinMode(FSR, INPUT); //* The Force Sensitive Resistor gives information to the arduino NANO (constantly)

delay(10);
digitalWrite(LED, LOW);
Serial.begin(9600);
}

void loop() {
colorStat = digitalRead(color);
pressSense = analogRead(FSR);

if ((pressSense > 500) && (colorStat == HIGH))

{

//while(digitalRead(color) == LOW);
startTime = millis();
delay(10);
// while(digitalRead(color) == HIGH);
elapsedTime = millis() - startTime;
elapsedTime = elapsedTime / 1000;
Serial.println("Elapsed Time:");
Serial.println(elapsedTime);

Serial.println("Press Sense:");
Serial.println(pressSense);
delay(1000);

}
else
{
Serial.println("No report");
Serial.println("Elapsed Time:");
Serial.println(elapsedTime);
//startTime = 0;
Serial.println("PressSense:");
Serial.println(pressSense);
delay(10);
}

if ((pressSense > 500) && (colorStat == LOW))
{
Serial.println("Elapsed Time:");
Serial.println(elapsedTime);
Serial.println("PressSense:");
Serial.println(pressSense);
delay(10);
}

else {
startTime = 0;
Serial.println("Elapsed Time:");
Serial.println(startTime);
Serial.println("PressSense:");
Serial.println(pressSense);
delay(10);
}

if ((pressSense < 500) && (colorStat == LOW))
{

    Serial.println(elapsedTime);
  
  
  Serial.println("Elapsed Time:");
  Serial.println(elapsedTime);
  Serial.println("PressSense:");
  Serial.println(pressSense);
  delay(10);
}

else {
Serial.println("Elapsed Time:");
Serial.println(elapsedTime);
Serial.println("PressSense:");
Serial.println(pressSense);
delay(10);
}

if ((pressSense < 500) && (colorStat == HIGH))
{
startTime = 0;

  Serial.println("Elapsed Time:");
  Serial.println(elapsedTime);
  Serial.println("PressSense:");
  Serial.println(pressSense);
  delay(10);
}

}

And I keep getting this:
No report
Elapsed Time:
0
PressSense:
0
Elapsed Time:
0
Elapsed Time:
0
Press Sense:
840
Elapsed Time:
0
PressSense:
840
Elapsed Time:
0
PressSense:
840
Elapsed Time:
0
Press Sense:
865
Elapsed Time:
0
PressSense:
865
Elapsed Time:
0
PressSense:
865

My pressure sensor is working, but not my stopwatch. How do I create a stopwatch that starts when the code needs it too and starts when the code asks it to?

Thanks in advance for any help I get on this topic.

Please always post your code inside the </> tags.. makes it much easier for people to help.

    startTime = millis();
    delay(10);
    // while(digitalRead(color) == HIGH);
    elapsedTime = millis() - startTime;
    elapsedTime = elapsedTime / 1000;

This is the only code that sets elapsedTime

It will briefly have a value of around 10 (as per your delay statement)... and then you divide by 1000... so it will be zero.

Everywhere you print it out.. you need to recalculate it... otherwise you are just printing zero.

So how do I create a sort of stopwatch within it?

A stop watch is essentially the difference between 2 times.

This is the first time:
startTime = millis();

This is the second time:

    elapsedTime = millis() - startTime;
    elapsedTime = elapsedTime / 1000;

At the moment you are doing both of these at the same time (one after the other)... you need to calculate the elapsedTime later, when you actually need it.

const int color = 2;
int LED = 7;
int colorStat = 0;
const int FSR = A0; //* Force Sensitive Resistors
int pressSense = 0; //* Force Sensitive Resistor's Statistics

unsigned long startTime;
int elapsedTime;
unsigned long stopTime;

void setup() {
pinMode(color, INPUT);
pinMode(LED, OUTPUT);
pinMode(FSR, INPUT); //* The Force Sensitive Resistor gives information to the arduino NANO (constantly)

delay(10);
digitalWrite(LED, LOW);
Serial.begin(9600);
}

void loop() {
colorStat = digitalRead(color);
pressSense = analogRead(FSR);

if (pressSense > 100)

{
while((digitalRead(color) == LOW) && (analogRead(pressSense) <100));
startTime = millis();
}
if ((pressSense <100) && (colorStat == HIGH))
{

 while(digitalRead(color) == HIGH);
 stopTime = millis();

elapsedTime = stopTime - startTime;
elapsedTime = elapsedTime/1000;
Serial.println("Elapsed Time:");
Serial.println(elapsedTime);

Serial.println("Press Sense:");
Serial.println(pressSense);
delay(1000);

}

}

I tired like this as you recommended. still the elapsed time till continues to increase from the start of the program. I want the elapsed time according to the presssense and colorstat input. please help me correct the code.

Elapsed Time:
3
Press Sense:
0
Elapsed Time:
4
Press Sense:
0
Elapsed Time:
5
Press Sense:
0
Elapsed Time:
16
Press Sense:
0
Elapsed Time:
0
Press Sense:
0
Elapsed Time:
1
Press Sense:
0
Elapsed Time:
4
Press Sense:
0
Elapsed Time:
5
Press Sense:
0

what are you trying to measure?

I am trying to measure the time in between when the color sensor goes from LOW to HIGH

Please comply with the first request in reply #2, in order to obtain further help.

Hi, @adharsh2008

To add code please click this link;

Thanks.. Tom... :smiley: :+1: :coffee: :australia:

why not

#undef MyHW     // values for my hardware
#ifdef MyHW
const int color = A1;

#else
const int color = 2;
#endif

byte colorState;

unsigned long   startTime;
int             elapsedTime;

void loop (void)
{
    unsigned long msec = millis();

    byte  col = digitalRead (color);
    if (colorState != col)  {
        colorState = col;

        if (LOW == col)
            startTime = msec;
        else  {
            elapsedTime = msec - startTime;
            Serial.print   ("Elapsed Time:");
            Serial.println (elapsedTime);
        }
    }
}

void setup (void)
{
    Serial.begin (9600);
    pinMode (color, INPUT_PULLUP);
    colorState = digitalRead (color);
}

May I ask why you gave two integers for color? (A1 and 2)

the #if/#else/#endif are C preprocessor directives that allows blocks of code to effectively be commented out.

the input (button) on my hardware uses different pins, selected by the "MyHW" define

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.