Looking for some advice that would possibly make my code better

I am trying to make a arduino project that will do the following:-

  1. Read a rotary encoder,
  2. Store the count value and then divide it by 10 to get a result which it will print on a Adafruit 7segment I2C Display,
  3. Use Pushbuttons to Implement A setpoint count while a “set” switch is pressed and display this while that set button is pressed again dividing the answer x 10 to get the value to be printed.
  4. While a OUTPUT enable pushbutton is pressed it will compare the encoder pulses to the count value (both the raw values) and output only while the encoder pulses are less than the setpoint.

I have got the code working through finding examples for encoder and the adafruit examples on the internet and reading various textbooks. It is functioning correctly but just wondered if there was anything within it that could be written better or structured in a way that would make it perform better,

thanks in advance,

Adam

provisional_tube_bender_display_with_setpoint_ver_1-5.ino (5.69 KB)

Here is the code without having to download it,

#include <Wire.h> // Enable this line if using Arduino Uno, Mega, etc.
//#include <TinyWireM.h> // Enable this line if using Adafruit Trinket, Gemma, etc.
#include "Adafruit_LEDBackpack.h"
#include "Adafruit_GFX.h"

int pulses, A_SIG=0, B_SIG=1;       //encoder inputs pin 2 and 3 
               //reset value switch
int buttonPressCount;
float (result);
float (result2);
const int  buttonPin0 = 11;    //the pin that the pushbutton is attached to +100
const int  buttonPin1 = 10;    //the pin that the pushbutton is attached to +10
const int  buttonPin2 = 7;    //the pin that the pushbutton is attached to -5
const int  buttonPin3 = 13;    //the pin that the pushbutton is attached to Setpoint
const int  buttonPin4 = 12;    //the pin that the pushbutton is attached to reset setpoint
const int  buttonPin5 = 6;    //the pin that the pushbutton is attached to enable output
const int  buttonPin6 = 8;    //the pin that the pushbutton is attached to reset pulses count
const int sol = 9;            // output solenoid





int buttonPressCounter = 0;   //counter for the number of button presses
int buttonState0 = 0;         //current state of the button
int buttonState1 = 0;         //current state of the button
int buttonState2 = 0;         //current state of the button
int buttonState3 = 0;         //current state of the button
int buttonState4 = 0;         //current state of the button
int buttonState5 = 0;         //current state of the button
int buttonState6 = 0;         //current state of the button
int lastButtonState0 = 0;     //previous state of the button
int lastButtonState1 = 0;     //previous state of the button
int lastButtonState2 = 0;     //previous state of the button
Adafruit_7segment matrix = Adafruit_7segment();

void setup(){
  attachInterrupt(0, A_RISE, RISING);
 attachInterrupt(1, B_RISE, RISING);
 pinMode(buttonPin0, INPUT);  //initialize the button pin as a input + 100
 pinMode(buttonPin1, INPUT);  //initialize the button pin as a input + 10
 pinMode(buttonPin2, INPUT);  //initialize the button pin as a input -5
 pinMode(buttonPin3, INPUT);  //initialize the button pin as a input  Setpoint
 pinMode(buttonPin4, INPUT);  //initialize the button pin as a input reset setpoint
 pinMode(buttonPin5, INPUT);  //initialize the button pin as a input enable output
 pinMode(buttonPin6, INPUT);  //initialize the button pin as a input  reset pulses count
 pinMode(sol, OUTPUT);
#ifndef __AVR_ATtiny85__
 Serial.begin(9600);
 Serial.println("7 Segment Backpack Test");
 Wire.begin();
#endif
 matrix.begin(0x70);

 
}

void loop() {
 int val = analogRead(3);
 val = map(val, 0, 1023, 0, 15);
 matrix.setBrightness(val);
 buttonState6 = digitalRead(buttonPin6);
 result = pulses * 0.1;
 matrix.println(result);
 matrix.writeDisplay();
 if (buttonState6 == HIGH && digitalRead(buttonPin5)==LOW) {
 pulses = 0;
 }
 
 
 while(digitalRead(buttonPin3)==HIGH && digitalRead(buttonPin5)==LOW){
   setPoint();
   int val = analogRead(3);
 val = map(val, 0, 1023, 0, 15);
 matrix.setBrightness(val);
 result2 = buttonPressCounter * 0.1;
 matrix.println(result2);
 matrix.writeDisplay();
 
 }
 if(digitalRead(buttonPin4)==HIGH && digitalRead(buttonPin5)==LOW){
   buttonPressCounter = 0;
 }
if(digitalRead(buttonPin5)==HIGH){
   automatic();
   int pulses;
   int buttonPressCounter;
   int val = analogRead(3);
   val = map(val, 0, 1023, 0, 15);
   matrix.setBrightness(val);
   matrix.println(result);
   matrix.writeDisplay();
   }
   else{
     digitalWrite(sol, LOW);
   }
}
void setPoint(){
  buttonState0 = digitalRead(buttonPin0);  //read the pushbutton input pin

 // compare the buttonState to its previous state
 if (buttonState0 != lastButtonState0) {
   // if the state has changed, increment the counter
   if (buttonState0 == HIGH) {
     // if the current state is HIGH then the button
     // went from off to on:
     buttonPressCounter+=100;
   }
 }
 buttonState1 = digitalRead(buttonPin1);  //read the pushbutton input pin
  if (buttonState1 != lastButtonState1) {
   // if the state has changed, increment the counter
   if (buttonState1 == HIGH) {
     // if the current state is HIGH then the button
     // went from off to on:
     buttonPressCounter+=10;
   }
 }
 buttonState2 = digitalRead(buttonPin2);  //read the pushbutton input pin
  if (buttonState2 != lastButtonState2) {
   // if the state has changed, increment the counter
   if (buttonState2 == HIGH) {
     // if the current state is HIGH then the button
     // went from off to on:
     buttonPressCounter-=5;
   }
 }

 lastButtonState0 = buttonState0;  // save the current state as the last state, for next time through the loop
 lastButtonState1 = buttonState1;  // save the current state as the last state, for next time through the loop
 lastButtonState2 = buttonState2;  // save the current state as the last state, for next time through the loop

 

}
void automatic(){
  if (pulses < buttonPressCounter){
   digitalWrite (sol, HIGH);
 }
 if (pulses >= buttonPressCounter){
   digitalWrite (sol, LOW);
 }
}
void A_RISE(){
detachInterrupt(0);
A_SIG=1;

if(B_SIG==0)
pulses++;//moving forward
if(B_SIG==1)
pulses--;//moving reverse

attachInterrupt(0, A_FALL, FALLING);
}

void A_FALL(){
 detachInterrupt(0);
A_SIG=0;

if(B_SIG==1)
pulses++;//moving forward
if(B_SIG==0)
pulses--;//moving reverse

attachInterrupt(0, A_RISE, RISING);  
}

void B_RISE(){
detachInterrupt(1);
B_SIG=1;

if(A_SIG==1)
pulses++;//moving forward
if(A_SIG==0)
pulses--;//moving reverse

attachInterrupt(1, B_FALL, FALLING);
}

void B_FALL(){
detachInterrupt(1);
B_SIG=0;

if(A_SIG==0)
pulses++;//moving forward
if(A_SIG==1)
pulses--;//moving reverse

attachInterrupt(1, B_RISE, RISING);
}

Adam

Please put your code in its own window as seen in other posts. This can be done by placing     [code]  and [/code]  around the code or use the </> icon. This makes it easier for others to read.

How to use this forum

Weedpharma

madoffroader:
in a way that would make it perform better,

What do you mean by "perform better"?

If it is doing what you want I'm not sure how it could "perform better"

Have you looked at Planning and Implementing a Program - in particular the idea of organizing the code into single-purpose functions. It won't necessarily perform better but it should be easier to maintain.

...R

I does appear to do everything correctly just wondered if it was structured correctly, thanks for the reply I will look at the link you posted and see if there is anything in there that I could do to make it neater/easier to work on if I ever need to modify the code.

Regards
Adam

int buttonPressCounter = 0;   //counter for the number of button presses
int buttonState0 = 0;         //current state of the button
int buttonState1 = 0;         //current state of the button
int buttonState2 = 0;         //current state of the button
int buttonState3 = 0;         //current state of the button
int buttonState4 = 0;         //current state of the button
int buttonState5 = 0;         //current state of the button
int buttonState6 = 0;         //current state of the button
int lastButtonState0 = 0;     //previous state of the button
int lastButtonState1 = 0;     //previous state of the button
int lastButtonState2 = 0;     //previous state of the button

How many of these variables (and many others) need to be ints or could many/all of them be byte to save memory ?

From the names of the variables it looks like you could use arrays in your program. At least give teh variables meaningful names to make the code more readable.