Here is the latest update.
How do I make sure that the value of eventcount doesn't go over 1? If I'm not mistaken, with the eventcount++ it adds one to the last count, but in the ReadingRPM Playground, it acts like after 20 it resets. With my statement of if(eventcount >= 1) will that limit it to 1 and only 1? Do I even need to include eventcount in my (time-timeold)/eventcount if its always one?
///
///
///
///
//-----------------------------------------------
volatile unsigned int eventcount; // Assinging the Count Feature
unsigned int rt; // Rotation Time
unsigned int degree; // Position of Piston
unsigned int rpm; // RPM
unsigned long timeold; // Previous time Hall Sensor Picked up
const int So17Pin = 3; // Solenoids 1 and 7 assigned to Pin 3
const int So35Pin = 4; // Solenoids 3 and 5 assigned to Pin 4
const int So28Pin = 5; // Solenoids 2 and 8 assigned to Pin 5
const int So46Pin = 6; // Solenoids 4 and 6 assigned to Pin 6
const int buttonPin = 13; // Button For Switching "Valve Open Time" Percent
const int button2Pin = 14; // Button For Start Mode Intake 1+7
const int button3Pin = 15; // Button For Start Mode Intake 3+5
int buttonPushCounter = 0; // counter for the number of button presses--- used with button for "Valve Open Time" Percent
int buttonState = 0; // current state of the button
int button2State = 0; // current state of the button 2
int button3State = 0; // current state of the button 3
int lastButtonState = 0; // previous state of the button
int lastButton2State = 0; // previous state of the button 2
int lastButton3State = 0; // previous state of the button 3
void setup()
{
Serial.begin(9600); // Communicate to Serial Port
pinMode (So17Pin, OUTPUT); // Solenoid 1+7 as Output
pinMode (So35Pin, OUTPUT); // Solenoid 3+5 as Output
pinMode (So28Pin, OUTPUT); // Solenoid 2+8 as Output
pinMode (So46Pin, OUTPUT); // Solenoid 4+6 as Output
pinMode(buttonPin, INPUT); // Button for "Valve Open Time" as Input
pinMode(button2Pin, INPUT); // Button For Start Mode 1+7 Intake
pinMode(button3Pin, INPUT); // Button For Start Mode 3+5 Intake
attachInterrupt(0, event_count, RISING); // Interrupt 0 is Pin 2 Hall Effect Sensor
}
void loop()
{ if(eventcount >=1)
// Calculate position of piston
rt = (micros() - timeold)/ eventcount; // Time Between Each Hall Pulse
rt = rt/360; // Time Between Each Hall Pulse Divided by ( 360 Degrees)--- Time for 1 degree of rotation
rt = abs(rt); // Absolute value of found time--- To make sure every degree is a positive number ( with time-timeold and deceleration of rotation speed, there will be a negative number produced)
timeold = micros();
eventcount = 0;
// Calculate RPM
rpm = (micros() - timeold) / eventcount; // Time Between Each Hall Pulse
rpm = 1000000/rpm; // (Microseconds to Second) Divided by (Time Between Each Hall Pulse)
rpm = rpm*60; //((Microseconds to Second) Divided by (Time Between Each Hall Pulse)) Times (Seconds to Minutes)
timeold = micros();
eventcount = 0;
Serial.println(rpm,DEC); // Print RPM on Serial
//-------------------------------------------------------------------------------------- START MODE
if (rpm = 0) // If Device is not spinning then Enable the Button's 2 and 3. If it is spinning Disable the Button's 2 and 3
{
if (button2State != lastButton2State) // Start Mode Button2 Pressed--- Intake 1 and 7 Exhaust 4 and 6
{
if (button2State == HIGH) {
digitalWrite(So17Pin, HIGH); // Fire Intake 1 and 7
digitalWrite(So46Pin, HIGH); // Fire Exhaust 4 and 6
Serial.println("Start In 1+7"); // Display Via Serial "Start In 1+7"
}
}
{
if (button3State != lastButton3State) // Start Mode Button3 Pressed--- Intake 3 and 5 Exhaust 2 and 8
{
if (button2State == HIGH) {
digitalWrite(So35Pin, HIGH); // Fire Intake 3 and 5
digitalWrite(So28Pin, HIGH); // Fire Exhaust 2 and 8
Serial.println("Start In 3+5"); // Display Via Serial "Start In 1+7"
}
}
}
//------------------------------------------------------------------------------------ RUN MODE
{
}
// Open for 100% Stroke Intake (1 and 7) Exhaust (4 and 6)
if (degree <= ( rt*179) || (degree >= rt*1) ) // If the piston is inbetween 1 to 179 degrees of revolution then...
{
digitalWrite(So17Pin, HIGH); // Fire Solenoid 1 and 7 (Intake)
digitalWrite(So46Pin, HIGH); // Fire Solenoid 4 and 6 (Exhaust)
Serial.println("Intake (1,7) Exhaust (4,6) Fired 100%"); // Print Status and Percent of Valve Open
}
else {
digitalWrite(So17Pin, LOW); // Don't Fire Solenoid 1 and 7 (Intake)
digitalWrite(So46Pin, LOW); // Don't Fire Solenoid 4 and 6 (Exhaust)
}
// Open for 100% Stroke Intake (3 and 5) Exhaust (2 and 8)
if (degree <= (rt*359) || (degree >= (rt*181)) ) // If the piston is inbetween 181 to 359 degrees of revolution then...
{
digitalWrite(So35Pin, HIGH); // Fire Solenoid 3 and 5 (Intake)
digitalWrite(So28Pin, HIGH); // Fire Solenoid 2 and 8 (Exhaust)
Serial.println("Intake (3,5) Exhaust (2,8) Fired 100%"); // Print Status and Percent of Valve Open
}
else {
digitalWrite(So35Pin, LOW); // Don't Fire Solenoid 3 and 5 (Intake)
digitalWrite(So28Pin, LOW); // Don't Fire Solenoid 2 and 8 (Exhaust)
}
}
//------------------------------------------ Button Control--- Allows the Change of How Long Valve Stays Open Per Revolution
if (buttonPushCounter <= 9){
if (buttonState != lastButtonState) // "Percent of "Valve Open Time" " Button Pressed
{
if (buttonState == HIGH) {
buttonPushCounter++; // Update Button Counter
Serial.print("number of button pushes: "); // Display Via Serial "number of button pushes: "
Serial.println(buttonPushCounter, DEC); // Display Via Serial Button Push Count
}
else {
lastButtonState = buttonState; // Stay at last button state if Button is not Pressed
}
}
else {
buttonPushCounter = 0; // Resets to 0 if it has been pressed more than 9 times
}
//---------------- 90% Valve Open
if (buttonPushCounter == 1) // If button is pressed once
{
Serial.println("90%");
// Open for 90% Stroke Intake (1 and 7) Exhaust (4 and 6)
if (degree <= ( rt*161.1) || (degree >= rt*1) ){
digitalWrite(So17Pin, HIGH);
digitalWrite(So46Pin, HIGH);
Serial.println("Intake (1,7) Exhaust (4,6) Fired 90%");
}
else {
digitalWrite(So17Pin, LOW);
digitalWrite(So46Pin, LOW);
}
// Open for 90% Stroke Intake (3 and 5) Exhaust (2 and 8)
if (degree <= (rt*342.1) || (degree >= (rt*181)) ){
digitalWrite(So35Pin, HIGH);
digitalWrite(So28Pin, HIGH);
Serial.println("Intake (3,5) Exhaust (2,8) Fired 90%");
}
else {
digitalWrite(So35Pin, LOW);
digitalWrite(So28Pin, LOW);
}
}
//---------------- 80% Valve Open
if (buttonPushCounter == 2) {
Serial.println("80%");
// Open for 80% Stroke Intake (1 and 7) Exhaust (4 and 6)
if (degree <= ( rt*143.2) || (degree >= rt*1) ){
digitalWrite(So17Pin, HIGH);
digitalWrite(So46Pin, HIGH);
Serial.println("Intake (1,7) Exhaust (4,6) Fired 80%");
}
else {
digitalWrite(So17Pin, LOW);
digitalWrite(So46Pin, LOW);
}
// Open for 80% Stroke Intake (3 and 5) Exhaust (2 and 8)
if (degree <= (rt*324.2) || (degree >= (rt*181)) ){
digitalWrite(So35Pin, HIGH);
digitalWrite(So28Pin, HIGH);
Serial.println("Intake (3,5) Exhaust (2,8) Fired 80%");
}
else {
digitalWrite(So35Pin, LOW);
digitalWrite(So28Pin, LOW);
}
}
//---------------- 70% Valve Open
if (buttonPushCounter == 3) {
Serial.println("70%");
// Open for 70% Stroke Intake (1 and 7) Exhaust (4 and 6)
if (degree <= ( rt*125.3) || (degree >= rt*1) ){
digitalWrite(So17Pin, HIGH);
digitalWrite(So46Pin, HIGH);
Serial.println("Intake (1,7) Exhaust (4,6) Fired 70%");
}
else {
digitalWrite(So17Pin, LOW);
digitalWrite(So46Pin, LOW);
}
// Open for 70% Stroke Intake (3 and 5) Exhaust (2 and 8)
if (degree <= (rt*306.3) || (degree >= (rt*181)) ){
digitalWrite(So35Pin, HIGH);
digitalWrite(So28Pin, HIGH);
Serial.println("Intake (3,5) Exhaust (2,8) Fired 70%");
}
else {
digitalWrite(So35Pin, LOW);
digitalWrite(So28Pin, LOW);
}
}
//---------------- 60% Valve Open
if (buttonPushCounter == 4) {
Serial.println("60%");
// Open for 60% Stroke Intake (1 and 7) Exhaust (4 and 6)
if (degree <= ( rt*107.4) || (degree >= rt*1) ){
digitalWrite(So17Pin, HIGH);
digitalWrite(So46Pin, HIGH);
Serial.println("Intake (1,7) Exhaust (4,6) Fired 60%");
}
else {
digitalWrite(So17Pin, LOW);
digitalWrite(So46Pin, LOW);
}
// Open for 60% Stroke Intake (3 and 5) Exhaust (2 and 8)
if (degree <= (rt*288.4) || (degree >= (rt*181)) ){
digitalWrite(So35Pin, HIGH);
digitalWrite(So28Pin, HIGH);
Serial.println("Intake (3,5) Exhaust (2,8) Fired 60%");
}
else {
digitalWrite(So35Pin, LOW);
digitalWrite(So28Pin, LOW);
}
}
//---------------- 50% Valve Open
if (buttonPushCounter == 5) {
Serial.println("50%");
// Open for 50% Stroke Intake (1 and 7) Exhaust (4 and 6)
if (degree <= ( rt*89.5) || (degree >= rt*1) ){
digitalWrite(So17Pin, HIGH);
digitalWrite(So46Pin, HIGH);