Hi,
I hope somebody may be able to give me a push in the right direction. It's my first go at programming (bar a little html and BBC Basic back when I was 10!)
I am trying to get a couple of PWM outputs to vary their output based on which inputs are high. By default I need a 50% duty cycle varying to 25%, 37/38%, 50%, 62/63% and 75% depending on which input is high ( Slow or Fast ) and which input direction is selected (up or down).
Below is the first code I was trying which I can get one change out of, the second code doesn't change the value at all.
I'm sure this is a very long winded way of coding it but I thought it should have worked none the less, unfortunately it doesn't!
First attempt:
// Constants - used to set inputs or output pin numbers
const int propleft = 11; //output variable
const int propright = 12; //output variable
const int fast = 2; //Input
const int slow = 3;
const int leftup = 4;
const int leftdown = 5;
const int rightup = 6;
const int rightdown = 7;
const int HeaderBack = 22;
const int HeaderForwards = 23;
const int HeaderLeft = 24;
const int HeaderRight = 25;
const int TransLeftForwards = 26;
const int TransLeftBackwards = 27;
const int TransRightForwards = 28;
const int TransRightBackwards = 29;
const int EStop = 30;
// Variables - can be changed.
// Button presses
int buttonFast = 0;
int buttonSlow = 0;
int buttonLeftUp = 0;
int buttonLeftDown = 0;
int buttonRightUp = 0;
int buttonRightDown = 0;
int buttonHeaderBack = 0;
int buttonHeaderForward = 0;
int buttonHeaderLeft = 0;
int buttonHeaderRight = 0;
int buttonTransLeftForwards = 0;
int buttonTransLeftBackwards = 0;
int buttonTransRightForwards = 0;
int buttonTransRightBackwards = 0;
int buttonEStop = 0;
int propleftamount = 127; // mid position of valve from 0-255.
int proprightamount = 127;
int fastup = 64;
int fastdown = -64;
int slowup = 96;
int slowdown = -32;
void setup() {
// put your setup code here, to run once:
// initialize serial communication at 9600 bits per second:
Serial.begin(9600);
pinMode(propleft, OUTPUT);
pinMode(propright, OUTPUT);
pinMode(fast, INPUT);
pinMode(slow, INPUT);
pinMode(leftup, INPUT);
pinMode(leftdown, INPUT);
pinMode(rightup, INPUT);
pinMode(rightdown, INPUT);
pinMode(HeaderBack, INPUT);
pinMode(HeaderForwards, INPUT);
pinMode(HeaderLeft, INPUT);
pinMode(HeaderRight, INPUT);
pinMode(TransLeftForwards, INPUT);
pinMode(TransLeftBackwards, INPUT);
pinMode(TransRightForwards, INPUT);
pinMode(TransRightBackwards, INPUT);
pinMode(EStop, INPUT);
}
void loop() {
// put your main code here, to run repeatedly:
buttonFast = digitalRead(fast);
buttonSlow = digitalRead(slow);
buttonLeftUp = digitalRead(leftup);
buttonLeftDown = digitalRead(leftdown);
buttonRightUp = digitalRead(rightup);
buttonRightUp = digitalRead(rightdown);
analogWrite(propleft, propleftamount);
analogWrite(propright, proprightamount);
if(digitalRead(fast) == HIGH && digitalRead(leftup) == HIGH) {
propleftamount = 191 ;
}
else
{ propleftamount = 127 ; }
if ((fast == 1) && (leftdown == 1)) {
propleftamount = 63 ;
}
//if ((slow == HIGH) && (leftup == HIGH)); {
// propleftamount = +31 ;
//}
//if ((slow == HIGH) && (leftdown == HIGH)); {
// propleftamount = -31 ;
//}
// print out the state of the button:
Serial.println(buttonFast);
Serial.println(buttonSlow);
Serial.println(buttonLeftUp);
Serial.println(buttonLeftDown);
Serial.println(propleftamount);
delay(1); // delay in between reads for stability
}
Second Attempt:
// Constants - used to set inputs or output pin numbers
const int propleft = 11; //output variable
const int propright = 12; //output variable
const int fast = 2; //Input
const int slow = 3;
const int leftup = 4;
const int leftdown = 5;
const int rightup = 6;
const int rightdown = 7;
const int HeaderBack = 22;
const int HeaderForwards = 23;
const int HeaderLeft = 24;
const int HeaderRight = 25;
const int TransLeftForwards = 26;
const int TransLeftBackwards = 27;
const int TransRightForwards = 28;
const int TransRightBackwards = 29;
const int EStop = 30;
// Variables - can be changed.
// Button presses
int buttonFast = 0;
int buttonSlow = 0;
int buttonLeftUp = 0;
int buttonLeftDown = 0;
int buttonRightUp = 0;
int buttonRightDown = 0;
int buttonHeaderBack = 0;
int buttonHeaderForward = 0;
int buttonHeaderLeft = 0;
int buttonHeaderRight = 0;
int buttonTransLeftForwards = 0;
int buttonTransLeftBackwards = 0;
int buttonTransRightForwards = 0;
int buttonTransRightBackwards = 0;
int buttonEStop = 0;
int propleftamount = 127; // mid position of valve from 0-255.
int proprightamount = 127;
int fastup = 64;
int fastdown = -64;
int slowup = 96;
int slowdown = -32;
void setup() {
// put your setup code here, to run once:
// initialize serial communication at 9600 bits per second:
Serial.begin(9600);
pinMode(propleft, OUTPUT);
pinMode(propright, OUTPUT);
pinMode(fast, INPUT);
pinMode(slow, INPUT);
pinMode(leftup, INPUT);
pinMode(leftdown, INPUT);
pinMode(rightup, INPUT);
pinMode(rightdown, INPUT);
pinMode(HeaderBack, INPUT);
pinMode(HeaderForwards, INPUT);
pinMode(HeaderLeft, INPUT);
pinMode(HeaderRight, INPUT);
pinMode(TransLeftForwards, INPUT);
pinMode(TransLeftBackwards, INPUT);
pinMode(TransRightForwards, INPUT);
pinMode(TransRightBackwards, INPUT);
pinMode(EStop, INPUT);
}
void loop() {
// put your main code here, to run repeatedly:
buttonFast = digitalRead(fast);
buttonSlow = digitalRead(slow);
buttonLeftUp = digitalRead(leftup);
buttonLeftDown = digitalRead(leftdown);
buttonRightUp = digitalRead(rightup);
buttonRightUp = digitalRead(rightdown);
analogWrite(propleft, propleftamount);
analogWrite(propright, proprightamount);
// Proportional LEFT
if(digitalRead(fast) == HIGH && digitalRead(leftup) == HIGH)
{ propleftamount = 191 ; }
else
{ propleftamount = 127 ; }
if(digitalRead(fast) == HIGH && digitalRead(leftdown) == HIGH)
{ propleftamount = 63 ; }
else
{ propleftamount = 127 ; }
if(digitalRead(slow) == HIGH && digitalRead(leftup) == HIGH)
{ propleftamount = 159 ; }
else
{ propleftamount = 127 ; }
if(digitalRead(slow) == HIGH && digitalRead(leftup) == HIGH)
{ propleftamount = 95 ; }
else
{ propleftamount = 127 ; }
// Proportional RIGHT
if(digitalRead(fast) == HIGH && digitalRead(rightup) == HIGH)
{ proprightamount = 191 ; }
else
{ proprightamount = 127 ; }
if(digitalRead(fast) == HIGH && digitalRead(rightdown) == HIGH)
{ proprightamount = 63 ; }
else
{ proprightamount = 127 ; }
if(digitalRead(slow) == HIGH && digitalRead(rightup) == HIGH)
{ proprightamount = 159 ; }
else
{ proprightamount = 127 ; }
if(digitalRead(slow) == HIGH && digitalRead(rightup) == HIGH)
{ proprightamount = 95 ; }
else
{ proprightamount = 127 ; }
// print out the state of the button:
Serial.println(buttonFast);
Serial.println(buttonSlow);
Serial.println(buttonLeftUp);
Serial.println(buttonLeftDown);
Serial.println(propleftamount);
Serial.println(proprightamount);
delay(1000); // delay in between reads
}
The first attempt that did sort of work also seemed to take a long time to return to 127 when all input go back LOW even with the delay set to 1ms. Is the cycle time quite slow on Arduino's?
Many thanks in advance,
Richard