New to posting and Arduino so if I miss anything out please bare with me. We have a machine at work which only has a potentiometer to control speed and no digital readout of how many parts it's making per an amount of time. I'm trying to use a pro mini to read signals over a period of 12 seconds then multiply that by 5 which would give the amount per minute. I'm running into some problems though. For the sake of trying to get the program working instead of having it attached to the machine I'm using a push button to simulate the signal I would get from the proximity sensor on the machine. The program I have attached will count them correctly but when the startTime reaches 12000ms then it freezes, wont count anymore and also doesn't go back to start the counting again. I've also realised that this way of doing it isn't very efficient as there's only a small window where the actual value would be shown and the rest of the time I would just have a rapidly rising number. Think I've worked myself into a little hole and would welcome some advice. I need the value to be the amount of signals per minute but it needs to be an updating value if the speed is changed so it cant just be an average. Let me know what you think and I can take a little picture of the test rig if needed.
#include <Adafruit_SSD1306.h>
#include <splash.h>
#include <gfxfont.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SPITFT.h>
#include <Adafruit_SPITFT_Macros.h>
#include <Wire.h>
#define SCREEN_WIDTH 128 //OLED display width, in pixels
#define SCREEN_HEIGHT 64 //OLED display height, in pixels
// declare an SSD1306 display object connected to I2C
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, 4);
unsigned long startTime=0;
unsigned long timeWindow=12000;
int counter=0;
int previousCounter=0;
int simButtonPin=12;
int simButtonState=0;
int previousSimButtonState=0;
int rpmValue=0;
void setup() {
pinMode(simButtonPin, INPUT_PULLUP);
Serial.begin(9600);
// put your setup code here, to run once:
// initialize OLED display with address 0x3C for 128x64
if (!display.begin(SSD1306_SWITCHCAPVCC, 0X3C)) {
Serial.println(F("SSD1306 allocation failed"));
for(;;);
}
display.display();
delay(2000); //wait for initializing
display.clearDisplay(); //clear display
display.setTextSize(1); //text size
display.setTextColor(SSD1306_WHITE); // text color
display.setCursor(0, 0); //position to display
display.println("Hello world!"); //text to display
display.display(); //show on OLED
display.startscrollright(0x00, 0x0F);
delay(2000);
display.stopscroll();
display.clearDisplay();
}
void loop() {
// put your main code here, to run repeatedly:
startTime=millis();
if (startTime<timeWindow){
simButtonState=digitalRead(simButtonPin);
if (simButtonState==LOW){
counter++;
display.clearDisplay(); // Clear display
display.setTextSize(1); //text size
display.setTextColor(SSD1306_WHITE); // text color
display.setCursor(0, 0); //position to display
display.println(counter*5); //display value
display.display(); //show on OLED
delay(50); // Stop Bounce
}
}
if (startTime>=timeWindow){
startTime=0;
counter=0;
}
previousCounter=counter;
simButtonState=previousSimButtonState;
}
You may want to use elapsed time instead of just a starting time.
if ( (millis() - startTime) < timeWindow)
{
do the things when elapsed time is less than time window
} else // time window exceeded.
{
// get a new start time or some other thing do's.
startTIme=millis(); // reset start time
}
ok so I've changed those and uploaded it but now the counter just goes up and up without a 12 second period. Any idea how it can store the press signal for 12 seconds and then delete it to give a running value?
#include <Adafruit_SSD1306.h>
#include <splash.h>
#include <gfxfont.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SPITFT.h>
#include <Adafruit_SPITFT_Macros.h>
#include <Wire.h>
#define SCREEN_WIDTH 128 //OLED display width, in pixels
#define SCREEN_HEIGHT 64 //OLED display height, in pixels
// declare an SSD1306 display object connected to I2C
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, 4);
unsigned long startTime=0;
unsigned long timeWindow=12000;
int counter=0;
int previousCounter=0;
int simButtonPin=12;
int simButtonState=0;
int previousSimButtonState=0;
int rpmValue=0;
void setup() {
pinMode(simButtonPin, INPUT_PULLUP);
Serial.begin(9600);
// put your setup code here, to run once:
// initialize OLED display with address 0x3C for 128x64
if (!display.begin(SSD1306_SWITCHCAPVCC, 0X3C)) {
Serial.println(F("SSD1306 allocation failed"));
for(;;);
}
display.display();
delay(2000); //wait for initializing
display.clearDisplay(); //clear display
display.setTextSize(1); //text size
display.setTextColor(SSD1306_WHITE); // text color
display.setCursor(0, 0); //position to display
display.println("Hello world!"); //text to display
display.display(); //show on OLED
display.startscrollright(0x00, 0x0F);
delay(2000);
display.stopscroll();
display.clearDisplay();
startTime=millis();
}
void loop() {
// put your main code here, to run repeatedly:
if ((millis()-startTime)<timeWindow){
simButtonState=digitalRead(simButtonPin);
if (simButtonState==LOW){
counter++;
display.clearDisplay(); // Clear display
display.setTextSize(1); //text size
display.setTextColor(SSD1306_WHITE); // text color
display.setCursor(0, 0); //position to display
display.println(counter*5); //display value
display.display(); //show on OLED
delay(50); // Stop Bounce
}
}
else{
startTime=millis();
}
previousCounter=counter;
simButtonState=previousSimButtonState;
}
If the pulse from the machine is always coming at the same rate, then your scheme will work. If the pulse timing varies, then you do not know how many pulses per minute you have because you do not know when to start or stop.
Perhaps consider a one minute timer in code and count the pulses in that time period.
Paul