What's new

C & C++ ARDUINO HELP

k4rm4

Elite
Joined
Dec 1, 2017
Posts
549
Reaction
288
Kapag ang value ng potentiometer ay naging greater than 500 ng tatlong beses sa loob ng 10 secs. Tsaka po iilaw ang LED.
Nalilito po kasi ako sa millis() function. Sana po may makatulong :(

CODE: You do not have permission to view the full content of this post. Log in or register now.
 
Last edited:

FoXOnE23

Established
Nalilito ako, hinde ko rin alam sakto ba to.

C++:
const int ledPin = 2;
const int analogPin = A0;
int peakCount = 0;          
int lastPeakValue = 0;

unsigned long previousTime = 0;
unsigned long interval = 5000;

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

void loop()
{
      int peakValue = analogRead( analogPin);
 
      unsigned long currentTime = millis();
     
      if(peakValue > 500)
      {
          peakCount++;
          previousTime = currentTime;
         
          Serial.print("Time: ");
          Serial.print(previousTime);
          Serial.print(" ");
 
          Serial.print( "Peak!  (");
          Serial.print( peakCount);
          Serial.println( ")");
          if(peakCount>=3 && previousTime<=10000)
          {
             digitalWrite(ledPin, HIGH);
          }    
      }
      lastPeakValue = peakValue;
   
      delay(100);
}
 

k4rm4

Elite
Nalilito ako, hinde ko rin alam sakto ba to.

C++:
const int ledPin = 2;
const int analogPin = A0;
int peakCount = 0;         
int lastPeakValue = 0;

unsigned long previousTime = 0;
unsigned long interval = 5000;

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

void loop()
{
      int peakValue = analogRead( analogPin);
 
      unsigned long currentTime = millis();
    
      if(peakValue > 500)
      {
          peakCount++;
          previousTime = currentTime;
        
          Serial.print("Time: ");
          Serial.print(previousTime);
          Serial.print(" ");
 
          Serial.print( "Peak!  (");
          Serial.print( peakCount);
          Serial.println( ")");
          if(peakCount>=3 && previousTime<=10000)
          {
             digitalWrite(ledPin, HIGH);
          }   
      }
      lastPeakValue = peakValue;
  
      delay(100);
}
Hindi po eh. Kailangan daw po may buffer para mairecord yung events.
 

FoXOnE23

Established
Hindi po eh. Kailangan daw po may buffer para mairecord yung events.
dapat po ba kunin yung peakValue, lastpeakValue, time in ms, tapos yung peakCount? yun lang ba yung kukunin, pano po yun peakValue < 500 irerecord din yun?
 

k4rm4

Elite
dapat po ba kunin yung peakValue, lastpeakValue, time in ms, tapos yung peakCount? yun lang ba yung kukunin, pano po yun peakValue < 500 irerecord din yun?
Yes po kukunin po ang time in ms gamit po ang millis() function
Basta po kapag ang peakValue ay umabot ng 500 pataas 3 times in 5 seconds iilaw po ang LED.
 
Top