Can't get my code to work right

Hi i want to have 2 buttons the run the motor at different speeds on each press 1 button from 1-5 and the other just stop and backwards 1 speed but the forward button is going at random on each press depending how long i hold it down i tried to use de-bounce but doesn't seem to work unless i'm putting it in the wrong way can anyone help me? also have a 7 seg but that's going ok

const int forward = 11; 
const int reverse = 10;
const int button = 12; 
const int button2 = 13; 
const int fan = 9; 
int potPin = A1;      
int buttonState = 0;
int button2State = 0;
int buttonPushCounter = 0;
int lastbuttonState = 0;
int lastbutton2State = 0;
int forwardState = HIGH;

void setup()                    
{
pinMode(forward, OUTPUT);
pinMode(button, INPUT);
pinMode(button2, INPUT);
pinMode(fan, OUTPUT);
pinMode(2, OUTPUT);  
pinMode(3, OUTPUT);
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
pinMode(6, OUTPUT);
pinMode(7, OUTPUT);
pinMode(8, OUTPUT);
Serial.begin(9600);
}
void loop()
{   
{
  int PWMoutput, potReading;
  potReading = analogRead(potPin);
  PWMoutput = map(potReading, 0, 1023, 0, 255);
  analogWrite(fan, PWMoutput);
}
int reading = digitalRead(button);
buttonState = digitalRead(button); 
{
    buttonState = reading;
if (buttonState != lastbuttonState){
if (buttonState == HIGH){
forwardState = !forwardState;
}
buttonPushCounter++;
analogWrite(forward, 95);
analogWrite(reverse, 0);
digitalWrite(2, 0);
digitalWrite(3, 1);
digitalWrite(4, 1);
digitalWrite(5, 0);
digitalWrite(6, 0);
digitalWrite(7, 0);
digitalWrite(8, 0);
Serial.println(buttonPushCounter);
delay(500); //types 1 on the 7 seg
}}
{
int reading = digitalRead(button);
buttonState = digitalRead(button); 
if (buttonState != lastbuttonState){
if (buttonState == HIGH){
forwardState = !forwardState;
}
if (buttonPushCounter == 2);{
analogWrite(forward, 135);
analogWrite(reverse, 0);
digitalWrite(2, 1);
digitalWrite(3, 1);
digitalWrite(4, 0);
digitalWrite(5, 1);
digitalWrite(6, 1);
digitalWrite(7, 0);
digitalWrite(8, 1);
Serial.println(buttonPushCounter);
delay(500); //types 2 on the 7 seg
}}
{
int reading = digitalRead(button);
buttonState = digitalRead(button); 
if (buttonState != lastbuttonState){
if (buttonState == HIGH){
forwardState = !forwardState;
}
if (buttonPushCounter == 3);{
analogWrite(forward, 175);
analogWrite(reverse, 0);
digitalWrite(2, 1);
digitalWrite(3, 1);
digitalWrite(4, 1);
digitalWrite(5, 1);
digitalWrite(6, 0);
digitalWrite(7, 0);
digitalWrite(8, 1);
Serial.println(buttonPushCounter);
delay(500); //types 3 on the 7 seg
}}}
{
int reading = digitalRead(button);
buttonState = digitalRead(button); 
if (buttonState != lastbuttonState){
if (buttonState == HIGH){
forwardState = !forwardState;
}
if (buttonPushCounter == 4);{
analogWrite(forward, 215);
analogWrite(reverse, 0);
digitalWrite(2, 0);
digitalWrite(3, 1);
digitalWrite(4, 1);
digitalWrite(5, 0);
digitalWrite(6, 0);
digitalWrite(7, 1);
digitalWrite(8, 1);
Serial.println(buttonPushCounter);
delay(500); //types 4 on the 7 seg
}}}
{
int reading = digitalRead(button);
buttonState = digitalRead(button); 
if (buttonState != lastbuttonState){
if (buttonState == HIGH){
forwardState = !forwardState;
}
if (buttonPushCounter == 5);{
analogWrite(forward, 255);
analogWrite(reverse, 0);
digitalWrite(2, 1);
digitalWrite(3, 0);
digitalWrite(4, 1);
digitalWrite(5, 1);
digitalWrite(6, 0);
digitalWrite(7, 1);
digitalWrite(8, 1);
Serial.println(buttonPushCounter);
delay(500); //types 5 on the 7 seg
}}}
}
button2State = digitalRead(button2);
if (button2State != lastbutton2State){
if(button2State == HIGH)
analogWrite(forward, 0);
analogWrite(reverse, 0);
delay(50);
digitalWrite(2, 1);
digitalWrite(3, 1);
digitalWrite(4, 1);
digitalWrite(5, 0);
digitalWrite(6, 1);
digitalWrite(7, 1);
digitalWrite(8, 0);
delay(500); //types n on the 7 seg
}
button2State = digitalRead(button2);
if (button2State != lastbutton2State){
if(button2State == HIGH)
analogWrite(forward, 0);
analogWrite(reverse, 100);
delay(50);
digitalWrite(2, 1);
digitalWrite(3, 0);
digitalWrite(4, 0);
digitalWrite(5, 0);
digitalWrite(6, 1);
digitalWrite(7, 1);
digitalWrite(8, 0);
delay(500); //types r on the 7 seg
}
  }

Its for a college project so i really need your help thanks

Let's start with something simple

  int reading = digitalRead(button);
  buttonState = digitalRead(button);
  {
    buttonState = reading;

Why read the button state twice then make the variables holding the state equal. What else could they already be but equal ?

Actually, why read the state of the button 10 times in the program when once would do ?
There are fundamental flaws in your program, not least that you never set lastbuttonState equal to buttonState so you will never know whether it has changed.

we weren't taught code very well its kinda learn it yourself kinda thing i obviously need help with it

@TommyDunne180, Please modify your post and use the code button </> so your code looks like this and is easy to copy to a text editor. See How to use the Forum Your code is too long to study quickly without copying to a text editor.

Have a look at Planning and Implementing a Program

...R

Hi,
What model arduino are you using?
What are you using to drive the motor?
You also have a pot input, what does it do?

How have you got your buttons wired?
You do not have internal PULL-UPs activated in your sketch, your sketch looks like you are pulling the button digital input HIGH with the button.
If so you will need 10K resistors on each input, connected from the input to gnd so that the input pin is not floating with the button open circuit.
Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

Divide you project program up into sections and complete each before going onto the next.

  • I think you need to start with just a simple sketch that senses when your button state has changed from LOW to HIGH, not when it IS HIGH.
  • Make each press increment a counter and serial.print it.
  • A sketch that controls your motor speed with counter value.
  • A sketch that controls your motor fwd,stop,rev with direction button.
  • Combine each segment, one at a time, each time getting it to work.

Tom.... :slight_smile:

I'm using arduino uno
my motors are just powered off the arduino because my components got delayed in the post and i didn't receive them yet
The pot is controlling a second motor it works perfect
The motors are just small 5v dc motors
i have pull up resistors on my buttons and the counter is working perfect i have that in already
not sure how to add in my circuit to this i'm new to this site but i know the circuit is fairly right i've problems with my code
And ok i'll take your advice and start over again try to get it to work

Hi,
Use REPLY rather than QUICK REPLY and it has an attachment facility, so you can post your files as attachments.
If your circuit is in CAD you should be able to export it as jpg or png to post.

Have you got flywheel diodes on your motor to protect the arduino?
BUT I would not power a motor from the 5V at the arduino, it is regulated from your USB port, or from a regulator on the PCB which cannot supply the currents needed to power a motor.

What other components are you waiting for, motor driver pcb or MOSFETs.

Thanks.. Tom... :slight_smile:

yeah i have a motor driver chip coming diodes and transistors and some other things they will be here monday thats when my project is due so i was trying to do them without them now im only running the motor for a few seconds so its ok from the usb its only for show not to do anything really

I started again and i seem to have figured it out thanks everyone for your help