Go Down

Topic: Arduino Uno + Wireless proto shield + Wifly module (Read 15 times) previous topic - next topic

sponder

Hey guys,

Sorry i've been away from these forums lately.
Nice to read others found it usefull :)
To update you guys: I've been cracking away on a simple script where i can (once connected through telnet) send a string and let the arduino process it, ie:
00; //turn led off
01; //turn led on
02;<column>;<row>;string //post the string on my lcd on the correct position..
03; // read value from a LDR and map it's value from 0 - 100 %, update the LCD with this value, as well as write the value to Serial.

I have the WiFly configured through serial, so no need to configure the WLAN stuff in the sketch, which is nice.

The sketch is working, well sorta.. It has a few bugs in it.
Would you guys be interested in the sketch just to check it out, or maybe even help fix the bugs? (warning, i'm still a newb in c.. :P)
If there is interest in it, i will post it tonight when i get home.

dhunt


sponder

I re-wrote the sketch again and it seems this one works without bugs!  :)
The getBuffer function i found somewhere on the web, but i don't remember where, so credits go to anonymous  :smiley-zipper:
Any hints or comments on how to improve on this code / my coding are greatly appreciated!

Code: [Select]

#include <LiquidCrystal.h>

   LiquidCrystal lcd ( 12, 11, 5, 4, 3, 2 );
   int led = 13;
   int ldr = 0;
   int ldrValue = 0;
   int ldrPercentage = 0;
   char buffer[30];
   int bufferSize;
   
   void setup()
   {
      pinMode(led, OUTPUT);
      pinMode(ldr, INPUT);
      Serial.begin(9600);
      lcd.begin( 20, 2);
   }
   
   void GetLdr()
   {
      ldrValue = analogRead(ldr);
      ldrPercentage = map(ldrValue, 0, 1023, 0, 100);
   }
   
   void UpdateLcd(String msg, int col, int row)
   {
      lcd.setCursor( col, row );
      lcd.print(msg);
   }
   
   void GetIp()
   {
      //WiFly wants 250 ms minimum before and after the $$$ when entering command mode
      delay(300);
      Serial.write("$$$");
      delay(300);
      Serial.println("get ip a");

      Serial.print("exit");
      Serial.println("\n");
      GetBuffer("\0");
      Serial.write(buffer);
     
   }
   
   char GetBuffer(char* This)
   {
      int index = 0;
      while(Serial.available() > 0)
      {
         delay(2);
         buffer[index] = Serial.read();
         index++;
         buffer[index] = '\n';
         bufferSize = index;
      }
         
         if(strcmp(buffer, This) == 0)
         {
            for(int i=0;i<30;i++)
            {
               buffer[i] = 0;
            }
            index = 0;
            return(0);
         }else{
            return(1);
         }
   }
   
   void ParseBuffer()
   {
      char cCmd[3] = {buffer[0], buffer[1], '\0'};
      int cmd = atoi(cCmd);
      switch(cmd)
      {
         case 0:
         {
           //turn led off:
           digitalWrite(led, LOW);
           Serial.write("led is OFF!");
         }
         break;
         case 1:
         {
            //turn led on:
            digitalWrite(led, HIGH);
            Serial.write("led is ON!");           
         }
         break;
         case 2:
         {
            GetIp();
         }
         break;
      }
   }
   
   void loop()
   {
      delay(2);
      if(Serial.available() > 0)
      {
         GetBuffer("\0");
         ParseBuffer();
      }else{
         GetLdr();
         lcd.print("LDR Value: ");
         lcd.setCursor(0,1);
         lcd.print(ldrValue);
         lcd.setCursor(0,0);
         delay(200);
      }
   }

mc63

Heey sponder,

Are you able to get the desired output i.e turning on and off LED and writing on the LCD from this code?. Am asking this because I realized you are using Serial instead of spiSerial.

Also how do you clear the LCD every time you receive data from the wifly? I tried to put lcd.clear() after spiserial.available() statement but only the first letter of the newly received message is displayed.

Thanks.

mc63

#54
Mar 10, 2012, 08:03 am Last Edit: Mar 10, 2012, 08:07 am by mc63 Reason: 1
Just to make the second point above clear, what I meant is that when I first send a message to the LCD it gets printed out correctly but when I send a another but of shorter length then this message plus the last few characters of the previous message are printed out. For example I send "Helooo"  then I send "Hey" I get this on the LCD "Heyooo" meaning that the previous message wasnt cleared before printing the new message.

I tried to put lcd.clear() but only the first characters of the messages sent are displayed. Below is my code.

Code: [Select]


#include "WiFly.h" // We use this for the preinstantiated SpiSerial object.
#include <LCD4Bit_mod.h>
LCD4Bit_mod lcd = LCD4Bit_mod(2);

 
void setup() {
 lcd.init();
 lcd.clear();
 lcd.cursorTo(1, 0);
 SpiSerial.begin();
 
}

void loop() {
 lcd.cursorTo(1, 0);
 // Terminal routine
 // Always display a response uninterrupted by typing
 // but note that this makes the terminal unresponsive
 // while a response is being received.
 while(SpiSerial.available() > 0) {
   // lcd.clear();
    lcd.print(SpiSerial.read());
}
}



I want to clear the LCD on every TCP connection so that the new message will be clearly displayed.

Go Up