Pressure reading (Need just the highest reading as output)

Hey guys I´m building a scale that will messure how much you can hit it. Problem is I got pressure sensore but I have no idea how to make arduino use the “highest” value. Since its analog it will go up and down and I need to get highest value. I bet there is fuction for it but I cant find it. I wasted whole day going thru forums etc… Any idea?

I want to press button and smash tenzometer and like those Hammer And Weight Game At Circus. Here is what I have now but its flickering and it will not show highest results. LED brightness will show ressults

int potPin= A0; 
int LEDPin= 3;  // I need to use PWM output
int readValue;  
int writeValue;

void setup() {
  pinMode(potPin, INPUT);  
  pinMode(LEDPin, OUTPUT); 
  Serial.begin(9600);    
}

void loop() {
  
 readValue = analogRead(potPin); 
 writeValue = (255./200.) * readValue; // Some edits that work with what I got now
 analogWrite(LEDPin, writeValue*-1);    // -1 just to make it LOW when nothing hits it
 Serial.println(writeValue);
 delay(100);
 
}

max()?

void loop() {
  
int potPin= A0; 
int LEDPin= 3;  
int readValue;  
int writeValue;
int maxValue;

void setup() {
  pinMode(potPin, INPUT);  
  pinMode(LEDPin, OUTPUT); 
  Serial.begin(9600);    
}

void loop() {
  
 readValue = analogRead(potPin); 
 writeValue = (255./200.) * readValue; // Some edits that work with what I got now
 analogWrite(LEDPin, writeValue*-1);    // -1 just to make it LOW when nothing hits it
maxValue = max(writeValue);
 Serial.println(maxValue);
 delay(5000);
 
}
 
}

Just like this? I mean I need two values and I have only one. I just read about max() and I feel like its just for making range. I need to get highest reading of that 5sec when ever its 10 or 1023.

Using delay(100) in a happening that might be of a much shorter duration than that? Get an oscilloscope and look at the pulse Your project is supposed to catch.

Something like:

uint16_t reading;

void setup() {
  // put your setup code here, to run once:
}

void loop() {
  take_reading();
}

void take_reading() {
  uint16_t temp;
  temp = code_to_read_sensor_here;
  if (temp > reading) {
    reading = temp;
  }
}

I dont need delay at all. Just want to hold that "winning" value there for while. You can edit it how you want if it will work.

Railroader:
Using delay(100) in a happening that might be of a much shorter duration than that? Get an oscilloscope and look at the pulse Your project is supposed to catch.

It doesnt work "exit status 1" :confused: I need to get something like:

spikeValue //that will be just tip of the pulse and it will not be always same. So on scale from 0-1023 once it will be just 250 next 700, etc... Just like on those equalizers where when you seen drop it would skyrocket and stay there for sec. I need just that and stay for idk 10sec.

Hey guys I know its hard with me but Im learning so much. When I arrived here first I knew just how to change delay on blink. Thanks for help.

PerryBebbington:
Something like:

uint16_t reading;

void setup() {
  // put your setup code here, to run once:
}

void loop() {
  take_reading();
}

void take_reading() {
  uint16_t temp;
  temp = code_to_read_sensor_here;
  if (temp > reading) {
    reading = temp;
  }
}

I moved some things to make it easier for me to see clearly quick and easy.

int potPin = A0; 
int LEDPin = 3;  // I need to use PWM output
int readValue, lastRead;  
unsigned long writeValue;  // to get the calculation done better, without using floats

void setup() 
{
  pinMode( potPin, INPUT );  
  pinMode( LEDPin, OUTPUT ); 
  Serial.begin( 115200 );  // change your serial monitor to match
  // faster serial clears the serial output buffer faster, 9600 is dismally slow    
}

void loop() 
{
 readValue = analogRead(potPin); 
 if ( lastRead > readValue )
 {
   readValue = lastRead;
 }
 else
 {
   lastRead = readValue;
 }

 writeValue = readValue; // puts the 16 bit signed value 0-1023 into unsigned long.
 writeValue = writeValue * 255UL / 200UL; // less lossy, 1023 becomes 1304

 Serial.println(writeValue);

 analogWrite(LEDPin, readValue / 4 );    // PWM can be 0-255. -1 may be same as 255

 delay(100); // this is more likely to cause missing the full pulse than not
 
}

There should probably be logic to stop reads once they fall to low value and get rid of the delay. It'd allow lastRead to be made zero and another whack measured.

I made a circuit with a piezo disc as force sensing button years ago. The piezo pulse went through a diode into a BJT gate and that filled a wire with 5V through 2.2K resistor for as long as the flattened by diode pulse kept the gate open (BJT gate draws current, the short pulse opens and closes the gate quickly.

The filled wire was connected to a digital pin that was constantly read at a high rate. A digitalRead eats 1 microamp of current. At over 50K reads/second it emptied the wire pretty fast. The code kept count of consecutive HIGH reads as an analog value of how hard the piezo was touched. On the first LOW read the pin changed to OUTPUT LOW to fully drain the wire and then set back to INPUT watching for the next touch.

It could detect a very light touch as 10 to 50 and a full hard smack with a screwdriver handle as around 3000. That count cycle takes about 1 microsecond, the hard smack took about 3ms to resolve, a very long time to Arduino. A bigger resistor on the 5V line to the BJT collector could cut that time.

If you hit something that caused a wheel to spin you could measure the spin rate to get force, F = MA.

OMG that great but I have little problem :confused: Can it loop for like atleast 10sec maybe more. Not its like doing something for idk 500ms? something like that. I thought its thanks to sensor. I would like if it could loop hold highest value until I reset it with some kind of reset button. I would like not to use Arduino reset since its take a while to boot. I know its kinda much but as I said I have no idea how it works and how to extend that reading time.

TenMike:
It doesnt work “exit status 1” :confused: I need to get something like:

spikeValue //that will be just tip of the pulse and it will not be always same. So on scale from 0-1023 once it will be just 250 next 700, etc… Just like on those equalizers where when you seen drop it would skyrocket and stay there for sec. I need just that and stay for idk 10sec.

Hey guys I know its hard with me but Im learning so much. When I arrived here first I knew just how to change delay on blink. Thanks for help.

GoForSmoke:
I moved some things to make it easier for me to see clearly quick and easy.

int potPin = A0; 

int LEDPin = 3;  // I need to use PWM output
int readValue, lastRead; 
unsigned long writeValue;  // to get the calculation done better, without using floats

void setup()
{
  pinMode( potPin, INPUT ); 
  pinMode( LEDPin, OUTPUT );
  Serial.begin( 115200 );  // change your serial monitor to match
  // faster serial clears the serial output buffer faster, 9600 is dismally slow   
}

void loop()
{
readValue = analogRead(potPin);
if ( lastRead > readValue )
{
  readValue = lastRead;
}
else
{
  lastRead = readValue;
}

writeValue = readValue; // puts the 16 bit signed value 0-1023 into unsigned long.
writeValue = writeValue * 255UL / 200UL; // less lossy, 1023 becomes 1304

Serial.println(writeValue);

analogWrite(LEDPin, readValue / 4 );    // PWM can be 0-255. -1 may be same as 255

delay(100); // this is more likely to cause missing the full pulse than not

}




There should probably be logic to stop reads once they fall to low value and get rid of the delay. It'd allow lastRead to be made zero and another whack measured.

You can code it to do whatever your code can describe, which is why I push beginners to learn basic C. Arduino IDE uses C and C++.

My thought is that the sketch should know when a pulse is detected from the data. If you put a button between your pot and the input pin, you can make pulses with that. When the data goes low, stop printing and the last high is the bottom line on serial monitor, sketch changes lastRead to 0 and printing not resume (need to code to only print reads > minimum) until you press the button.

At some point, the force sensor becomes the input?

But why it will stop reading after few milisec?

GoForSmoke:
You can code it to do whatever your code can describe, which is why I push beginners to learn basic C. Arduino IDE uses C and C++.

My thought is that the sketch should know when a pulse is detected from the data. If you put a button between your pot and the input pin, you can make pulses with that. When the data goes low, stop printing and the last high is the bottom line on serial monitor, sketch changes lastRead to 0 and printing not resume (need to code to only print reads > minimum) until you press the button.

At some point, the force sensor becomes the input?

So Im kinda trying to edit it and yet I failed. But its kinda strange how is it doing this.

Imgur

2nd problem is that its not looping :confused: It will read 17times next time 25 times etc...

TenMike:
But why it will stop reading after few milisec?

Now this is working. Yeeey still need to add button but nice done. Thanks for your help. Feel free to use it.

 int potPin = A0; 
int LEDPin = 3;  // I need to use PWM output
int readValue, lastRead;  
unsigned long writeValue;  // to get the calculation done better, without using floats

void setup() 
{
  pinMode( potPin, INPUT );  
  pinMode( LEDPin, OUTPUT ); 
  Serial.begin( 115200 );  // change your serial monitor to match
  // faster serial clears the serial output buffer faster, 9600 is dismally slow    
}

void loop()  {

  pCt();  
 lOgic();

 
 }

void lOgic()


{
 readValue = analogRead(potPin); 
 if ( lastRead > readValue )
 {
   readValue = lastRead;
 }
 else
 {
   lastRead = readValue;

   delay(100);
 


 
}
 
}


void pCt()  {



 writeValue = readValue; // puts the 16 bit signed value 0-1023 into unsigned long.
 writeValue = writeValue * 255UL / 200UL; // less lossy, 1023 becomes 1304

 Serial.println(writeValue);

 analogWrite(LEDPin, readValue / 4 );    // PWM can be 0-255. -1 may be same as 255

 }

My bad :confused: loop is working just bad arduino clone :frowning:

TenMike:
But why it will stop reading after few milisec?

It will stop printing new numbers. It should never stop reading.

Best thing would be collect and only print the high value once the reads go low.
Or that would be best if you were collecting data from hits and not potentiometer readings.