Author Topic: problems with arduino code  (Read 1629 times)

0 Members and 1 Guest are viewing this topic.

Offline little_carlos

  • Regular Contributor
  • *
  • Posts: 129
problems with arduino code
« on: April 01, 2015, 02:53:04 am »
helo guys, ive done a code for a arduino to display the status of a water pump into a lcd, ive done it so that it reads if the motor is on or off, and display it into a lcd, the problem is that it always read as if the motor is on and it always displays de the message "bomba encendida" (it means pump on in spanish).
i havent done it on physical, just on proteus, what is wrong with the code? when i compile it it says its ok, no error messages

 #include <LiquidCrystal.h>

LiquidCrystal lcd(7, 8, 9, 10, 11 , 12);
int bomba  = 1;
int val = 0;

void setup() {
  lcd.begin(16, 2);
  lcd.setCursor(0, 0);
  pinMode(bomba, INPUT);
 
}

void loop() {

  val = digitalRead(bomba);
if(val = HIGH){
  lcd.write("bomba1 encendida");
}
else{
  lcd.write("bomba1 apagada");
}

}
 

Offline Mr.B

  • Supporter
  • ****
  • Posts: 1036
  • Country: nz
Re: problems with arduino code
« Reply #1 on: April 01, 2015, 03:00:04 am »
Try
if(val == HIGH){
Time is the overseer of all things.
 

Offline little_carlos

  • Regular Contributor
  • *
  • Posts: 129
Re: problems with arduino code
« Reply #2 on: April 01, 2015, 03:06:10 am »
Try
if(val == HIGH){

would "val" work if i use it for more inputs or i have to declare another one?
 

Offline Mr.B

  • Supporter
  • ****
  • Posts: 1036
  • Country: nz
Re: problems with arduino code
« Reply #3 on: April 01, 2015, 03:10:10 am »
Your original code is
if(val = HIGH){

In that line you are assigning the value of HIGH to the variable val.

Your code should look like this
if(val == HIGH){

Note the double =
This is a test to see if 'val equates to HIGH.
basically your C syntax is wrong.
Time is the overseer of all things.
 

Offline Mr.B

  • Supporter
  • ****
  • Posts: 1036
  • Country: nz
Re: problems with arduino code
« Reply #4 on: April 01, 2015, 03:12:14 am »
would "val" work if i use it for more inputs or i have to declare another one?

val is declared as a 'global' variable and can be used anywhere you like.
Yes, you can use it for more inputs, just make sure you are setting the variable correctly before you test it.
Time is the overseer of all things.
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf