[code] having trouble with figuring this code writing out , I am new to arduino and trying to output a pulse modulated voltage from 0-5v out of pin 6
I start at 128 right in the middle and I want to be able to push one button and increase the value and push another button and decrease the value. It is pretty much my first attempt at writing code so I dont know whats wrong but It actually works but the numbers for the variable (motorspeed) go way beyond 255 they just keep going up and up and down below zero as well can someone help me understand what I am doing wrong and how to fix it.
/*
Wireless pin detail:
Button Arduino Digital Pin Wireless Receiver
A 4 D3
B 3 D2
C 2 D1
D 1 D0
A: Speed up
B: Slow down
*/
const int Abutton = 8;
const int Bbutton = 9;
int motorspeed = 128;
int buttonAState = 0;
int buttonBState = 0;
void setup() {
pinMode(6, OUTPUT); // pulse width modulation output voltage to the motor controller in place of the potentiometer
pinMode(8, INPUT); //A button
pinMode(9, INPUT); //B button
Serial.begin(9600);
}
void loop() {
int buttonAState = digitalRead (Abutton);
delay(50);
if (buttonAState == HIGH){
analogWrite (6, motorspeed++);
}
else
{
analogWrite (6, motorspeed);
}
int buttonBState = digitalRead (Bbutton);
delay(50);
if (buttonBState == HIGH) {
analogWrite (6, motorspeed--);
}
else
{
analogWrite (6, motorspeed);
}
Serial.println(motorspeed);
if((motorspeed)>254);
analogWrite (motorspeed, 254);
if((motorspeed)<10);
analogWrite (motorspeed, 10);
}
[/code]