need programming help

hey guys just trying to write a simple program and am getting my head all twisted up since i dont really understand what im doing wrong i tring to get a light to blink from the signal from a button and for it to stay on based on the resistance from a pot im good at the hardware part but the software is killing me

so far i have this, can anyone tell me what im doing wrong, all i can do is get a solid led, it wont turn off

int potPin = 2; // select the analog input pin for the potentiometer
int ledPin = 13; // select the pin for the led
int val = 0; // variable to store the value coming from the sensor
int buttonPin = 5; // button reciever input pin

void setup() {
pinMode(ledPin, OUTPUT); // declare the led Pin as an OUTPUT
pinMode(buttonPin, INPUT); // declare the button as an input
}

void loop() {

val = analogRead(potPin); // read the value from the sensor
digitalRead(buttonPin); // recieve signal to pin 5

if (buttonPin = HIGH)
{ digitalWrite(ledPin, HIGH);
delay(val); // stop the program for some time
}
else
{digitalWrite(ledPin, LOW);}

}

thanks in advance to anyone trying to help me sorry for something so simple/trivial to all you programmers out there

You are not storing the value of digitalread and the info gets lost. Create a variable, for example boolean val2; (boolean because there are only 2 states)
Then change this line

val2= digitalRead(buttonPin);

Then use this val2 variable intead of buttonPin to check if statements.

if (val2 == HIGH) and so on... (when you are cheking if both value are the same you use twice ==, correct that part in your code too).

Hi and welcome...

For a start you need this....

 if (buttonPin = HIGH)

.... to be this:

 if (buttonPin == HIGH)

Read the "warning" section here.

edit.... along with what mart said 8)

Also, please use code tags like mart did, makes it muuuuch easier to read code that way.

(The # icon above the :wink: :sweat_smile:)

@mart, bad form to edit and not highlight the == edit since that makes my post redundant and me look like a pillock.

edit... but main thing is of course that the OP's question is answered.

int potPin = 2; // select the analog input pin for the potentiometer

Pin 2 is not an analog pin though? analogRead requires A0, A1 etc.

cjdelphi:
int potPin = 2; // select the analog input pin for the potentiometer

Pin 2 is not an analog pin though? analogRead requires A0, A1 etc.

Analogue pin 2 is an analogue pin.
analogRead does not require A0, A1 etc.

Ah I understand now...

The A is redundant then

cjdelphi:
Ah I understand now...

http://arduino.cc/en/Tutorial/ReadAnalogVoltage

The A is redundant then

No, the A is optional in this case.

JimboZA:
@mart, bad form to edit and not highlight the == edit since that makes my post redundant and me look like a pillock.

edit... but main thing is of course that the OP's question is answered.

Got it.

alright ive put in what was advised (i think) and im still coming up with the same results programing is still too new for me to understand whats going wrong, here are the revisions can anyone point out to me what im still doing wrong? i stored the button as a value and tryed only acting on a high value but ive still got the same result

int potPin = 2; // select the analog input pin for the potentiometer
int led = 13; // select the pin for the led
int button = 9; // pin for the button
int val = 0; // variable to store the value coming from the sensor
int val2 = 0; // variable to store signal from button

void setup() {
pinMode(led, OUTPUT); // declare the led Pin as an OUTPUT
pinMode(button, INPUT); // declare the button as an input
}

void loop() {

val = analogRead(potPin); // read the value from the sensor
val2 = digitalRead(button); //read value from button

if (val2 == HIGH)
{ digitalWrite(led, HIGH);
delay(val); // stop the program for timre based on the pot
}
else
{digitalWrite(led, LOW);}

}

It does actually work, although I made a subtle change in the code below.

You're looking for the switch to go "high" so presumably you have a pulldown resistor to make sure it's low when not high. Anyway, I decided to wire mine with the internal pullup- it's easier than buggering around with tiny resistors. But a pullup means the pin is usually high and so we need to look for a low when it's pressed: the logic is reversed. Either way is ok, it's just easier to use the internal pullup so the logic is upside down.

edit... that might or might not be material to your problem. If you had no pulldown, the pin might not have been low enough to be low: processor might have taken your pin as high all the time.

edit... with pullups the other side of the switch must go to ground not 5V.... it's already high with the pullup, we look for a low.

edit.... I note you moved the button in the code.... did you move it for real on the board?

Then I made the delay 4x val to make it more noticeable for testing.

And I added some debug serial prints so I could see that the "if" was working. edit.... why don't you try them too, before changing to internal pullups and inverted logic, to see where you "if" is landing up.

Have a look at this:

int potPin = 2;    // select the analog input pin for the potentiometer
int led = 13;     // select the pin for the led
int button = 9;     // pin for the button
int val = 0;      // variable to store the value coming from the sensor
int val2 = 0;    // variable to store signal from button



void setup() {
  Serial.begin(9600); // new *****************
  pinMode(led, OUTPUT);  // declare the led Pin as an OUTPUT
  pinMode(button, INPUT_PULLUP); // declare the button as an input with pullup resistor ********
}

void loop() {

  val = analogRead(potPin);    // read the value from the sensor
  val2 = digitalRead(button);   //read value from button

    if (val2 == LOW) //inverted logic now, with pullups *****************
  { 
    Serial.print("pressed"); // new **************
    Serial.print(" ");// new **************
    Serial.print(val);// new **************
    digitalWrite(led, HIGH);
    delay(4 * val);   // stop the program for timre based on the pot now 4x the pot *****
  }
  else
  {
    Serial.println("not pressed"); // new **************
    digitalWrite(led, LOW);
  }

}

Note the use of code tags....

wow thanks for all the help , i did change the location of the button because it was easier to reach it with a short jumper when on my breadboard, and i had no idea i was suppost to use a resistor on the button i assumed it only passed current when pressed makeing it either no current or all the current for the signal input ill try useing it the way you have it set up and see if i can understand whats going on that way, im just getting into the programming world and i ran out of the little flash cards that came with the kit, too bad they dont last to long, so im venturing on to trying writing on my own, thanks again for the help its very appreciated

The purpose of the resistor is to force the pin to either high or low when its not pressed. Let's say the other side of the button is to 5V, so yes the pin goes to 5V when button pushed. But we can't assume the button is low, just because it's not pushed. All we know is, it's not necessarily high, but it might be high... might be low. Apparently pins pick up all sorts of stray electricity in the atmosphere and can go high or low based on what they find. So force it one way or the other with a resistor.

So the two cases are:

  • Connect other side of pin to ground, and pull it high with a pullup. Then when button is pressed, the pin goes low. For convenience, the Arduino pins have pullups internally.
  • Connect other side of pin to 5V, and pull it low with a pulldown. Then when button is pressed, the pin goes high.

Pic below shows connections.