question abount pressure sensor and time HELP ME!!!!!PLEASE!!!!!!

I want to work this programming....

What I want to do is

  1. I want to print out the time when I press pressure sensor in Arduino.
  2. If the pressure sensor is not pressed, 0 is output, and if pressed again, it is output again from 1.

This is the coding for the circuit connected now. However, it only checks the pressure sensor value.

I would appreciate it if you refer to this for coding.
const int pressSensor = A1;

void setup() {
Serial.begin(9600);
}
void loop() {
int value = analogRead(pressSensor);
Serial.println(value);
delay(1000);
}

  1. I want to print out the time when I press pressure sensor in Arduino.
  2. If the pressure sensor is not pressed, 0 is output, and if pressed again, it is output again from 1.

Sorry, that is not clear, Try to explain again.

sorry... i'm not good at english....

In other words, I want to have "time to press pressure sensor" printed on the serial monitor.
If the pressure sensor is not pressed, the output value is 0.
If the pressure sensor is pressed for 1 second, the output value is 1.
If the pressure sensor is pressed for 5 seconds, the output value is 5.

Yes, but when do you want the output values to be printed. For example, if nobody is using it, the pressure sensor is not pressed. Do you want it to keep printing 0, or just print 0 once? Same question for the 1 and 5 seconds...

Also, after the sensor is pressed for 5 seconds and it outputs 5, then what? Should it remain at 5, or change back to 0?

In other words, can you explain the sequence of events in time?

What is the purpose of this experiment?

Ok, so just to be clear, you want to print the time with the value of the pressure sensor when a button is pressed? Do you need to run other code in the background?

If so and I understand what you are asking, here is a non-blocking pseudo block you could use and tinker with:

//replace the number 5 with whatever digital input you are using
#define BUTTON_PIN 5

//getting immediate value is faster because it doesn't have to indirect
#define P_SENSOR_PIN A1

bool buttonIsPressed, previousState = false;

void setup() {
  //don't forget your pinModes!!!
  pinMode(BUTTON_PIN, INPUT);
  pinMode(P_SENSOR_PIN, INPUT);
  
  Serial.begin(9600);
}

void loop() {
  //get value and current position of button
  int value = analogRead(P_SENSOR_PIN);
  buttonIsPressed = digitalRead(BUTTON_PIN);

  //test if button was previously pressed already
  //also test if we weren't just pushing the button
  if (buttonIsPressed && !previousState) {
    previousState = true;
    Serial.print("Time: ");
    Serial.print(millis());
    Serial.print(", pressure: ");
    Serial.print(value);
  }

  //if we aren't pushing the button, default previous state to false
  if (!buttonIsPressed) previousState = false;

  //delay for debounce, but delays are bad for non-blocking code
  //ideally, you would use a timer
  delay(50);
}

ok!! i understand.
i want to keep printing 0 when the pressure sensor is not pressed.
and i want to this.
If I press from 1 second to 5 seconds, the serial monitor will output "1 2 3 4 5",
and If I don't press for 5 seconds, it will be output as "0 0 0 0 0 0". (by switching lines)

sorry..
it's "0 0 0 0 0 "
and Think of it as a timer!!

Supernovali !!

I don't understand "digital input." Does the circuit require anything other than pressure sensors?

Something like (compiles, untested):

#define PRESS_THRESHOLD     200         //whatever ADC counts == press (assumes a press == higher ADC counts)

const uint8_t pressSensor = A0;         //or whatever pin you have it connected to

uint32_t
    timeSensor,
    timeNow;

uint16_t
    seconds;
    
void setup() 
{
    Serial.begin(9600);
    pinMode( pressSensor, INPUT );      //not really required but good form
    
}//setup

void loop() 
{
    timeNow = millis();
    
    if( (timeNow - timeSensor) >= 1000ul )
    {
        timeSensor = timeNow;
        
        //read the sensor
        uint16_t sensorADC = analogRead(pressSensor);
        if( sensorADC >= PRESS_THRESHOLD ) 
        {
            if( seconds == 0 )       
                Serial.write( '\n' );
            seconds++;
        }//if
        else
        {
            if( seconds != 0 )
                Serial.write( '\n' );
            seconds = 0;            
            
        }//else

        Serial.print( seconds ); Serial.write( ' ' );
        
    }//if
       
}//loop

Blackfin very thank you!!
but, No matter what code I write, I only get a pressure sensor value, not time.
Since the pressure sensor is an analog sensor, is it impossible to get a time value other than the sensor value?

I want this programming.

  1. If the pressure sensor has a value of 20 or more, 1 is output on the serial monitor.
  2. If the pressure sensor’s value is less than 20, a zero is output on the serial monitor.

However, only the pressure sensor value is displayed on the serial monitor. What’s wrong?

const int pressSensor = A1;
void setup() {
Serial.begin(9600);
}

void loop() {
int value = analogRead(pressSensor);
if (analogRead(pressSensor)>20)
{Serial.println(“1”);}
else
{Serial.println(“0”);}
delay(1000);
}

remove all the !!!!!!! in your head line
post the code as a code-section

post the serial output you are getting

and I will start answering

Programming is a bit more than just hsout at alexa "switch on the light"

I recommend working through this tutorial
Arduino Programming Course

It is easy to understand and has a good mixture between explaining important concepts and example-codes to get you going. So give it a try and report your opinion about this tutorial.

const int pressSensor = A1;
void setup() {
Serial.begin(9600);
}

void loop() {
int value = analogRead(pressSensor);
if (analogRead(pressSensor)>20)
{Serial.println("1");}
else
{Serial.println("0");}
delay(1000);
}

I get

0

0

237

0

278

319

303

105

110

0

0

38

147

0

9

17

from this......

Yes, millis() will give you the time since the arduino started in milliseconds. Be aware that this clock is not very accurate as it is affected by interrupts. If you need an accurate measure of time, using an RTC (real time clock) module is necessary unless your system on chip has an RTC available like the SAMD controllers.

So you can print the millis() for milliseconds (use caution, the millis() returns an (unsigned long) value). To convert to seconds, divide by 1000, to convert to minutes, divide by 60000, to convert to hours, divide by 3600000.

What’s wrong?

The code you posted isn’t the code producing the output you posted.

Please mark the topic “SOLVED”, and please remember to use code tags when posting code

@madisonyun

Why did you post the same question again ?

Cross-posting is against the rules of the forum. The reason is that duplicate posts can waste the time of the people trying to help. Someone might spend 15 minutes (or more) writing a detailed answer on this topic, without knowing that someone else already did the same in the other topic.

Repeated cross-posting will result in a timeout from the forum.

In the future, please take some time to pick the forum board that best suits the topic of your question and then only post once to that forum board. This is basic forum etiquette, as explained in the sticky "How to use this forum - please read." post you will find at the top of every forum board. It contains a lot of other useful information. Please read it.

Thanks in advance for your cooperation.

HELP ME!!!!!PLEASE!!!!!!

Very rude.

larryd:
Very rude.

How so?
S/he said “please”

:grinning:

Please remember to use the code block as well, it makes your questions, code, and debugger output a lot easier to read. Hope you can figure this out! the language barrier seems to be causing confusion.