简体   繁体   中英

LED on Arduino won't turn on / off based on condition

I have an Arduino UNO R3 that reads a specific value from my Web Page.

I have an LED attached to the PIN 13 & GND of my Arduino.

When the Arduino reads 1 from my Web Page, it should turn the LED ON. When it reads 0, it should turn it off.

Following is the code for that:

#include "SIM900.h"
#include <SoftwareSerial.h>
#include "inetGSM.h"


InetGSM inet;

#define ledPin 13

char msg[165];
char store[2];
char a;
char b;

char* disp;

boolean started=false;

void setup()
{
      pinMode(ledPin, OUTPUT);
      digitalWrite(ledPin, LOW);
     //Serial connection.
     Serial.begin(9600);
     Serial.println("GSM Shield testing.");
     //Start configuration of shield with baudrate.
     //For http uses is raccomanded to use 4800 or slower.
     if (gsm.begin(2400)) {
          Serial.println("\nstatus=READY");
          started=true;
     } else Serial.println("\nstatus=IDLE");

     if(started) 
     {
          //GPRS attach, put in order APN, username and password.
          //If no needed auth let them blank.
          if (inet.attachGPRS("TATA.DOCOMO.INTERNET", "", ""))
               Serial.println("status=ATTACHED");
          else Serial.println("status=ERROR");
          delay(1000);



          //TCP Client GET, send a GET request to the server and
          //save the reply.

          //Print the results.


     }
}

void loop()
{
  inet.httpGET("www.boat.esy.es", 80, "/retrieve.php", msg, 165);
  disp = strstr(msg,"\r\n\r\n");
  disp = disp+4;
  a = disp[0];
  b = disp[1];
  Serial.println(b);
  if(b=='1')
  { 
    digitalWrite(ledPin, HIGH);
  }
  if(b=='0');
  {
    digitalWrite(ledPin, LOW);
  }
}

Problem here is, when I disable the digitalWrite(ledPin,LOW) , that is when I comment it out, the LED turns on & stays that way.

But the moment I enable it & load the code on my Arduino, it won't even turn on.

I'm wondering if it's a logical error or something else. Because the turning on & off of the LED depends completely on the conditions being satisfied. And for now, my Web Page returns only 1, hence the LED should stay on. But when I include both digitalWrite(ledPin, HIGH) and digitalWrite(ledPin, LOW) in the same code and run it, it doesn't work. I can see the Serial printing out the messages associated with the LED ON, but I don't see the LED turning ON.

Thank You for your time!!

First of all you have a semicolon that I think should not be in your second if-statement?

    if(b=='0'); <--
    {
      digitalWrite(ledPin, LOW);
    }

Start by trying to remove that and see if there is a difference.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM