Coding fault or Circuit fault....Please help

Hey guys, i am working on a project named Smart Classroom and i have already build the circuit and programmed the arduino board. The program should count the number of people passed through the sensor and turn on the lights according to the row and it also have to set the Fan Speed. The problem is whenever my arduino board pass through the digitalWrite(fan, HIGH); code, it crashes and runs some weird character on my LCD. I dont know whether its my coding fault or my circuit fault.

i have attached my coding. Please help me. Thanks in advance.

HELP.txt (3.44 KB)

Where is your schematic? I see nothing that would cause a crash in the code you posted, so I suspect a wiring/power issue (likely where turning on the fan causes a glitch on the power supply, crashing the arduino - but we can't tell because you didn't post the schematic).

The code you posted is wrong in multiple places - but this shouldn't cause a crash.

fanSpeed = 0.1;
analogWrite(fan, fanSpeed);
digitalWrite(fan, HIGH);

When you digitalWrite() to a pin, it turns off any analogWrite() made to the same pin (and vise versa).

The second argument to analogWrite() is an integer (between 0 and 255) not a float. So your 0.1 gets rounded down to 0, so the analogWrite() line is doing the same thing as digitalWrite(fan,0). See the documentation for analogWrite().

Your code looks weird but harmless to me. You should at least describe what is connected to the fan pin, this output may be overloaded by the attached circuit. Do you use a transistor to drive the fan?

Hi,
Welcome to the forum.

Please read the first post in any forum entitled how to use this forum.
http://forum.arduino.cc/index.php/topic,148850.0.html then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

Thanks.. Tom... :slight_smile:

Hi friends, Thanks for replying. I will upload my schematics in awhile. Yes i did use a transistor to drive the fan.

Hi friends, this is the schematic diagram for my project.

Hi,
thanks. OPs diagram


Thanks.... Tom.. :slight_smile:

OP’s code within XXXX tags !

#include<LiquidCrystal.h>
LiquidCrystal lcd(13,12,11,10,9,8);

#define in 14
#define out 19
#define relay1 2
#define relay2 3
#define relay3 4
#define relay4 5
#define relay5 6

int count=0;
int fan = 7;
int fanSpeed=0;

void IN()
{
    count++;
    lcd.clear();
    lcd.print("Person In Room:");
    lcd.setCursor(0,1);
    lcd.print(count);
    delay(1000);
}

void OUT()
{
    count--;
    lcd.clear();
    lcd.print("Person In Room:");
    lcd.setCursor(0,1);
    lcd.print(count);
    delay(1000);
}

void setup()
{
  lcd.begin(16,2);
  lcd.print("Student Counter");
  delay(2000);
  pinMode(in, INPUT);
  pinMode(out, INPUT);
  pinMode(fan, OUTPUT);
  pinMode(relay1, OUTPUT);
  pinMode(relay2, OUTPUT);
  pinMode(relay3, OUTPUT);
  pinMode(relay4, OUTPUT);
  pinMode(relay5, OUTPUT);
  lcd.clear();
  lcd.print("Person In Room:");
  lcd.setCursor(0,1);
  lcd.print(count);
  lcd.clear();
}

void loop()
{  
  while(digitalRead(in) == LOW)
  {
  IN();
  digitalRead(out) == 0;
  delay(600);
  break;
  }
  while(digitalRead(out) == LOW)
  {
  OUT();
  digitalRead(in) == 0;
  delay(600);
  break;
  }
  
  if(count<=0)
  {
    lcd.clear();
    digitalWrite(relay1, LOW);
    digitalWrite(relay2, LOW);
    digitalWrite(relay3, LOW);
    digitalWrite(relay4, LOW);
    digitalWrite(relay5, LOW);
    digitalWrite(fan, LOW);
    fanSpeed = 0;
    analogWrite(fan, fanSpeed);
    lcd.clear();
    lcd.print("Nobody In Room");
    lcd.setCursor(0,1);
    lcd.print("Light&Fan Is OFF");
    delay(200);
    count = 0;
  }
  
  if(count >= 1 && count <= 4)
  {
    lcd.clear();
    digitalWrite(relay1, HIGH);
    fanSpeed = 0.1;
    analogWrite(fan, fanSpeed);
    digitalWrite(fan, HIGH);
    lcd.clear();
    lcd.print("Fan Speed 20%");
    lcd.setCursor(0,1);
    lcd.print("R1 ON");
    delay(200);
  }

  if(count >= 5 && count <= 8)
  {
    lcd.clear();
    fanSpeed = 40;
    analogWrite(fan, fanSpeed);
    //digitalWrite(fan, HIGH);
    digitalWrite(relay1, HIGH);
    digitalWrite(relay2, HIGH);
    lcd.clear();
    lcd.print("Fan Speed 40%");
    lcd.setCursor(0,1);
    lcd.print("R1,R2 ON");
    delay(200);
  }

  if(count >= 9 && count <= 12)
  {
    lcd.clear();
    fanSpeed = 60;
    analogWrite(fan, fanSpeed);
    //digitalWrite(fan, HIGH);
    digitalWrite(relay1, HIGH);
    digitalWrite(relay2, HIGH);
    digitalWrite(relay3, HIGH);
    lcd.clear();
    lcd.print("Fan Speed 60%");
    lcd.setCursor(0,1);
    lcd.print("R1,R2,R3 ON");
    delay(200);
  }

  if(count >= 13 && count <= 16)
  {
    lcd.clear();
    fanSpeed = 80;
    analogWrite(fan, fanSpeed);
    //digitalWrite(fan, HIGH);
    digitalWrite(relay1, HIGH);
    digitalWrite(relay2, HIGH);
    digitalWrite(relay3, HIGH);
    digitalWrite(relay4, HIGH);
    lcd.clear();
    lcd.print("Fan Speed 80%");
    lcd.setCursor(0,1);
    lcd.print("R1,R2,R3,R4 ON");
    delay(200);
  }

  if(count >= 17 && count <= 20)
  {
    lcd.clear();
    fanSpeed = 100;
    analogWrite(fan, fanSpeed);
    digitalWrite(fan, HIGH);
    digitalWrite(relay1, HIGH);
    digitalWrite(relay2, HIGH);
    digitalWrite(relay3, HIGH);
    digitalWrite(relay4, HIGH);
    digitalWrite(relay5, HIGH);
    lcd.clear();
    lcd.print("Fan Speed 100%");
    lcd.setCursor(0,1);
    lcd.print("ALL ON");
    delay(200);
  }

  else
  lcd.clear();
  lcd.print("Class is FULL");
  
}

Obviously, You’ve never tried to compile or run this block of code.
There are some very ‘odd’ constructs - which was mentioned earlier.

digitalRead(in) == 0; does nothing
Also - since you name the relays 1,2,3 etc Why not name them by their function?

My very first comment will be -
WATCH OUT FOR THE MAINS WIRING AND SAFETY/ISOLATION

hey lastchancename, thanks for the correction. =)

my wiring is ok. But when ever i try to compile the code and upload into arduino, it will be perfect for the 0 person in the room. When i make the count = 1, it reads the digitalWrite(fan, HIGH) code, then there will be some glitch on my LCD.

Without telling you exactly what’s wrong, or trying to replicate problems without having the same hardware...
One of the first / easiest tricks is to sprinkle Serial.print() calls throughout your code - so you can see what code is executing, and the value of variables in real time.

You’ll see the printed output on the Serial Monitor in the IDE

Glitches can often be traced to power supply issues if they occur semi randomly at similar points in the operation.

Sounds like a power supply issue, but no obvious candidate based on the schematic posted.

How is the power supply done exactly?

wvmarle, i am giving 6 volt from AC/DC adjustable adapter to the circuit and for the arduino board im using power from laptop USB cable.

lastchancename, Thanks for the tips bro...i will update my codings and troubleshoot again and let you know what is the problem

A protection diode (freewheeling diode) across the relay coil may also help.

indren94:
wvmarle, i am giving 6 volt from AC/DC adjustable adapter to the circuit and for the arduino board im using power from laptop USB cable.

Then you should amend your circuit diagram as I see a 5V and a 12V power supply there, no 6V.

Have you controlled the 5V level, before and after the error?

Hi,

RLY1 will not work the way you have it connected for a start.
The coil should be in the collector circuit of Q1, and the relay coil should as suggested earlier have a free wheel diode across it.

Can you post a picture of your project so we can see your component layout?

Thanks.. Tom.. :slight_smile:

Hi TomGeorge, thanks for pointing me the correction....i will update after the correction..