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

0 Members and 1 Guest are viewing this topic.

Offline little_carlosTopic starter

  • Regular Contributor
  • *
  • Posts: 133
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: 1237
  • Country: nz
Re: problems with arduino code
« Reply #1 on: April 01, 2015, 03:00:04 am »
Try
if(val == HIGH){
I approach the thinking of all of my posts using AI in the first instance. (Awkward Irregularity)
 

Offline little_carlosTopic starter

  • Regular Contributor
  • *
  • Posts: 133
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: 1237
  • 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.
I approach the thinking of all of my posts using AI in the first instance. (Awkward Irregularity)
 

Offline Mr.B

  • Supporter
  • ****
  • Posts: 1237
  • 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.
I approach the thinking of all of my posts using AI in the first instance. (Awkward Irregularity)
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf