Go Down

Topic: Code Problems (Read 739 times) previous topic - next topic

Nutkin

Feb 26, 2010, 04:43 pm Last Edit: Feb 26, 2010, 04:59 pm by Nutkin Reason: 1
Hi im trying to write a code that will output a variable PWM to one pin if a switch is on. and to another pin if another switch is on.

when neither switches are on it should light a standby led.
Code: [Select]


int TriggerPinFWD = 9;// MOSFETS on pin 9
int TriggerPinRVS = 10; // MOSFETS on pin 10
int analogPin = 3;   // potentiometer connected to analog pin 3
int val = 0;// variable to store the read value
int buttonPinFWD= 1; //button in for fwd motion
int buttonPinRVS= 2; // button in for backwards motion
int val2 = 0; //variable for FWD switch
int val3 = 0; //variable for RVS switch
int LEDPin = 0; //Output for led pin

void setup()
{
  pinMode(TriggerPinFWD, OUTPUT);  // Setting the IO
  pinMode(TriggerPinRVS, OUTPUT);
  pinMode(LEDPin, OUTPUT);
 pinMode(buttonPinFWD, INPUT);
 pinMode(buttonPinRVS, INPUT);
 
 TCCR2B = (TCCR2B & 0xF8) | 2;


 
}

void loop()
{
 val = analogRead(analogPin);   // read the input pin for PWM
 val2 = digitalRead(buttonPinFWD); //read button switch 1 state
 val3 = digitalRead(buttonPinRVS); //read button switch 2 state
 
 if (val2 == HIGH)
 {
 analogWrite(TriggerPinFWD, val / 4);  //write a value to Pin 9 if the switch 1 is on
 }
   else
   {
 digitalWrite(LEDPin, HIGH);  //Light standby led
 }
 if (val3 == HIGH)
   {
      analogWrite(TriggerPinRVS, val / 4); //write a value to Pin 10 if the switch 2 is on
   }
 else
   {
 digitalWrite(LEDPin, HIGH);  //Light standby led
 }
 
}



The code seems fine to me but i wanted a second set of eyes and since im the only person doing this using arduino and not pic at my uni i thought i'd ask here.

Groove

#1
Feb 26, 2010, 04:53 pm Last Edit: Feb 26, 2010, 04:53 pm by GrooveFlotilla Reason: 1
Can you hit modify on that post please, highlight the code and press the "#" button on the toolbar?

My only comments would be that val, val2 and val3 should not be globals, pin numbers should probably be constants, but apart from that and the eccentric indentation, it look OK.

You realise you're not switching off the standby LEDs?
Per Arduino ad Astra

Nutkin

ahh i have removed the led bit for now just till i can get her working.

its just the fact the circuit does whatever it wants ! it doesnt respond to the switch at all and i have tested it as a working switch so no idea whats going on.

when you say dont set the values as globals what do you mean?

Groove

If it's not working, then I suggest you cut down your sketch to the bare minimum you need to read the switches and output the readings to the serial port.

val, val2, and val3 are not used outside of "loop" - there's no reason they should have global scope, so declare them inside "loop".
It may save a lot of head-scratching later when your program grows.
Per Arduino ad Astra

Nutkin

Code: [Select]
int TriggerPinFWD = 9;// MOSFETS on pin 9
int TriggerPinRVS = 10; // MOSFETS on pin 10
int analogPin = 3;   // potentiometer connected to analog pin 3
const int buttonPinFWD= 1; //button in for fwd motion
const int buttonPinRVS= 2; // button in for backwards motion

int LEDPin = 0; //Output for led pin

void setup()
{
 pinMode(TriggerPinFWD, OUTPUT);  // Setting the IO
 pinMode(TriggerPinRVS, OUTPUT);
 pinMode(LEDPin, OUTPUT);
 pinMode(buttonPinFWD, INPUT);
 pinMode(buttonPinRVS, INPUT);
 pinMode(analogPin, INPUT);
 TCCR2B = (TCCR2B & 0xF8) | 2;
 

 
}

void loop()
{
int val = analogRead(analogPin);   // read the input pin for PWM
int val2 = digitalRead(buttonPinFWD); //read button switch 1 state
int val3 = digitalRead(buttonPinRVS); //read button switch 2 state
 
 if (val2 == LOW)
 {
 analogWrite(TriggerPinFWD, val / 4);  //write a value to Pin 9 if the switch 1 is on
 }
   else
   {
     analogWrite(TriggerPinRVS, val / 4);
 }
}



It does not seem to be looping whatever state the switch is in at the start is what it does. It doesnt respond to the flicking of the switch. Im really not sure why.

AWOL

Maybe your switch pins are floating.
How are they wired?
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Nutkin

Problem solved, couple of 10k resistors and a few lines to turn the switches off and shes working fine.


Also i am ashamed to admit they were at first wired into the analog inputs  ::)

tkbyd

Yes- I see this was solved, but in hopes that sometimes people scan old threads before starting new ones...

That code would be easier to work with if meaningful names were used, e.g. instead of...

val2 = digitalRead(buttonPinFWD); //read button switch 1 state
val3 = digitalRead(buttonPinRVS); //read...

ButtonFWDState = digitalRead(buttonPinFWD);
ButtonRVSState = digitalRead(buttonPinRVS);

Go Up