Errors in uploading with my code.

Hello !

[RESOLVED IN #6]

I’m actually on a Alarm project, and I would like to show the battery voltage on my LCD screen (Nokia 5110). The voltage appears but some errors appears in the console during upload.

Errors:

Before unless answers, YES I unplugged D0 from my bluetooth module.

Here is the code that makes me appears errors:

void Alarm::BatteryCheck()
{
 unsigned int raw_bat = analogRead(A0);
 float real_bat = ((raw_bat * (5.0 / 1024) * 4.65));
 
 String vBat;
 vBat = String(real_bat);
 
 screen.SendString("Bat: ", 3);
 const char * v = vBat.c_str();
 screen.SendString(const_cast<char *>(v), 3, 4*8);
 screen.SendString("v", 3, 8*8);
 
 Serial.print("analogRead: ");
 Serial.println(analogRead(A0));
 Serial.print("real_bat: ");
 Serial.println(real_bat);
 Serial.print("vBat: ");
 Serial.println(vBat);
}

If i comment this code, I have not errors.

Let me explain:
The measure works on Analog 0 port with a voltage divider (with electrical resistance) which reduces the voltage (9.6v battery), Next I do a calculation for converting AnalogRead to float voltage.

I set the float value to a string, and i set it to a const char with c_str(). Next I convert it to a char * value (for my LCD screen).

Next I do some prints, but I have errors without them.

There is a problem in my code ? :l

Some times the errors are random:

That’s all :slight_smile:
Thanks in advance !!

Sincerely, Damien.

You guessed that I’m French ? :smiley:

http://pastebin.com/pzpXcX46

Throwing your code and errors in the rubbish bin is a waste of time.

There is a problem in my code ?

Obviously.

You are talking about pastebin ? Its more readable :)

Its more readable

Than what?

Your request has been blocked by Xxxxxx Information Security

Hi,
What arduino are you using?
Have you got it selected in the IDE before you upload.

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

Can you please post a complete copy of your sketch, using code tags?
They are made with the </> icon in the reply Menu.
See section 7 http://forum.arduino.cc/index.php/topic,148850.0.html

Use REPLY rather than QUICK REPLY and it has an attachment facility, so you can post your files as an attachment.

Tom… :slight_smile:

Hi,
I can’t show you the complete code because I use 3 library (2 were created by me) and my main library is 400 lines …

Here is my INO file:

#include <Arduino.h>

#include <IRremote.h>
#include <Alarm.h>
#include <NokiaLCD.h>

#include <SoftwareSerial.h>

const int receivePin = 8;
IRrecv irrecv(receivePin);
decode_results results;

SoftwareSerial BT(0, 1);

Alarm myAlarm;

int secondsEllapsed;
int sA_Securite;
int sA_Intrusion;

void setup() {
  irrecv.enableIRIn();
  Serial.begin(57600);
  
  myAlarm.Calibrate();
  
  BT.begin(9600);
}

void loop() {
  if(irrecv.decode(&results))
  {
    //Serial.println(results.value);
    IRdecode(results.value);
    irrecv.resume();
  }
  if(BT.available())
  {
    char data;
    data = (BT.read());
    if(myAlarm.menuStatus == ASK_PWD)
    {
      int number;
      number = data - '0';
      Serial.println(number);
      myAlarm.AddToPassword(number);
    }
    switch(data)
    {
      case 'a': // OFF
      {
        if(myAlarm.status == OFF)
        {
          myAlarm.SwitchOn();
          BT.write("Alarm switched On");
          break;
        }
        else
        {
          myAlarm.SwitchOff();
          BT.write("Alarm switched Off");
          break;
        }
      }
      case 'b':
      {
        myAlarm.AskArm();
        break;
      }
      case 'c':
      {
        myAlarm.AskDisarm();
        break;
      }
      case 'd':
      {
        myAlarm.VerifyPassword();
        break;
      }
    }
  }
        
  if(millis()/1000 > secondsEllapsed) // Toutes les secondes
  {
    secondsEllapsed = millis()/1000;
    myAlarm.BatteryCheck();
    myAlarm.EllapsedTime();
    if(myAlarm.status == ARMED)
    {
      if(digitalRead(9) == HIGH)
      {
        //sA_Securite += 1;
        //if(sA_Securite == 2) {
          BT.write("ALARM");
          myAlarm.ActiveAlarm(INTRUSION);
        //}
      }
      else
        sA_Securite = 0;
    }
    if(myAlarm.menuStatus == ASK_PWD)
      myAlarm.sEllapsed_ASK_PWD += 1;
    if(myAlarm.sEllapsed_ASK_PWD >= 15)
    {
      myAlarm.StopPassword();
    }
    
//    Serial.print("s_"); // Status
//    Serial.print(myAlarm.status);
//    Serial.print("_");
//    Serial.print("t_e_"); // Time Ellapsed
//    Serial.print(millis());
//    Serial.print("_");
//    Serial.print("t_a_"); // Time at last arm
//    Serial.print(myAlarm.timeArmed);
//    Serial.print("_");
//    Serial.println("s_"); // Status
//    Serial.println(myAlarm.status);
//    Serial.println("_");
//    Serial.println("t_e_"); // Time Ellapsed
//    Serial.println(millis());
//    Serial.println("_");
//    Serial.println("t_a_"); // Time at last arm
//    Serial.println(myAlarm.timeArmed);
//    Serial.println("_");
  }
}

void IRdecode(long data)
{
  switch(data)
  {
    if(myAlarm.menuStatus == ASK_PWD)
    {
      case 2155841655: // 0
      {
        myAlarm.AddToPassword(0);
        break;
      }
      case 2155864095: // 1
      {
        myAlarm.AddToPassword(1);
        break;
      }
      case 2155847775: // 2
      {
        myAlarm.AddToPassword(2);
        break;
      }
      case 2155831455: // 3
      {
        myAlarm.AddToPassword(3);
        break;
      }
      case 2155860015: // 4
      {
        myAlarm.AddToPassword(4);
        break;
      }
      case 2155843695: // 5
      {
        myAlarm.AddToPassword(5);
        break;
      }
      case 2155827375: // 6
      {
        myAlarm.AddToPassword(6);
        break;
      }
      case 2155868175: // 7
      {
        myAlarm.AddToPassword(7);
        break;
      }
      case 2155851855: // 8
      {
        myAlarm.AddToPassword(8);
        break;
      }
      case 2155835535: // 9
      {
        myAlarm.AddToPassword(9);
        break;
      }
    }
    case 2155855935: // Power
    {
      if(myAlarm.status == OFF)
      {
        myAlarm.SwitchOn();
        break;
      }
      else
      {
        myAlarm.SwitchOff();
        break;
      }
    }
    case 2155813095: // Menu
    {
      //Serial.println("**** MENU ****");
      break;
    }
    case 2155817175: //EPG
    {
      //Serial.println("**** EPG ****");
      break;
    }
    case 2155819215: // OK
    {
      //Serial.println("**** OK ****");
      if(myAlarm.menuStatus == ASK_PWD)
        myAlarm.VerifyPassword();
      break;
    }
    case 2155821255: // Gauche
    {
      //Serial.println("**** Gauche ****");
      break;
    }
    case 2155809015: // Bas
    {
      //Serial.println("**** Bas ****");
      break;
    }
    case 2155806975: // Droite
    {
      //Serial.println("**** Droite ****");
      break;
    }
    case 2155811055: // Haut
    {
      //Serial.println("**** Haut ****");
      break;
    }
    case 2155829415: // Vert
    {
      //Serial.println("**** Bouton vert ****");
      myAlarm.AskDisarm();
      break;
    }
    case 2155845735: // Rouge
    {
      //Serial.println("**** Bouton rouge ****");
      myAlarm.AskArm();
      break;
    }
  }
}

Alarm.h

#include "Arduino.h"

#define MAX_PWD_SIZE	8

// Alarm status
#define OFF				0
#define READY			1
#define ARMED			2
#define DISARMED	3
#define WARMED		4 // WaitToArm

// Menu status
#define NONE			0
#define ASK_PWD		1

// Reason
#define SECURITE		0
#define INTRUSION	1
#define INCENDIE		2

class Alarm
{
	public:
		
		int status;
		int menuStatus;
		
		int sEllapsed_ASK_PWD;
		
		int timeArmed = -1;
		
		Alarm();
		
		void Calibrate();
		
		void BatteryCheck();
		void EllapsedTime();
		
		void SwitchOff();
		void SwitchOn();
		void ResetScreen();
		
		void AskDisarm();
		void AskArm();
		
		void AddToPassword(int value);
		void VerifyPassword();
		void StopPassword();
	
		void WaitToArm();
		
		void ActiveAlarm(int reason);
	private:
		int tmp_pwd[MAX_PWD_SIZE];
		int tmp_pwd_size = 0;
		
		int nextStatus;
};

A piece of Alarm.cpp

#include "Alarm.h"
#include "NokiaLCD.h"

LCD screen;

Alarm::Alarm()
{
	screen.SendString("Alarm ON", 0);
	
	for(int i; i < MAX_PWD_SIZE; i++)
		tmp_pwd[i] = 0;
	tmp_pwd_size = 0;
	
	 pinMode(2, OUTPUT); // Ecran
	 pinMode(12, OUTPUT);
	 pinMode(13, OUTPUT);
	 
	digitalWrite(2, HIGH);
	digitalWrite(12, LOW);
	digitalWrite(13, LOW);
	
	pinMode(9, INPUT); // PIR
	pinMode(10, OUTPUT); // Buzzer
	pinMode(11, OUTPUT); // Relais
	
	digitalWrite(11, HIGH);

	screen.SendString("** READY **", 1);
	status = READY;
	nextStatus = NONE;
	menuStatus = NONE;
}

void Alarm::Calibrate()
{
	screen.SendString("Calibrating", 1);
	for(int i; i < 30; i++)
	{
		for(int index; index < i/6; index++)
			screen.SendString(".", 2, index);
		
		delay(1000);
	}
	ResetScreen();
}

void Alarm::BatteryCheck() // bugged function
{
	unsigned int raw_bat = analogRead(A0);
	float real_bat = ((raw_bat * (5.0 / 1024) * 4.55));
	
	String vBat;
	vBat = String(real_bat);
	
	screen.SendString("Bat: ", 3);
	const char * v = vBat.c_str();
	screen.SendString(const_cast<char *>(v), 3, 4*8);
	screen.SendString("v", 3, 8*8);
}

void Alarm::EllapsedTime()
{
	int s = (millis() / 1000) % 60;
	int m = (millis() / 1000) / 60;
	if(m >= 60)
	{
		if(s % 60 == 0) // Aucun reste, donc minute passée
		{
			screen.ClearLine(5);
			m %= 60;
			int m_d = m / 10;
			int m_u = m % 10;
			
			int h = m / 60;
			int h_d = h / 10;
			int h_u = h % 10;
			
			screen.SendInt(h_d, 5);
			screen.SendInt(h_u, 5, 1*8);
			screen.SendString("h", 5, 2*8);
			screen.SendInt(m_d, 5, 3*8);
			screen.SendInt(m_u, 5, 4*8);
			screen.SendString("m", 5, 5*8);
		}
	}
	else if(m > 9 && m < 60)
	{
		
		screen.ClearLine(5);
	
		int m_d = m / 10;
		int m_u = m % 10;
		screen.SendInt(m, 5);
		screen.SendInt(m_u, 5, 8);
		screen.SendString("m", 5, 2*8);
		
		int s_d = s / 10;
		int s_u = s % 10;
		screen.SendInt(s_d, 5, 3*8);
		screen.SendInt(s_u, 5, 4*8);	
		screen.SendString("s", 5, 5*8);
	}
	else
	{
		
		screen.ClearLine(5);
	
		screen.SendInt(m, 5);
		screen.SendString("m", 5, 1*8);
		
		int s_d = s / 10;
		int s_u = s % 10;
		screen.SendInt(s_d, 5, 2*8);
		screen.SendInt(s_u, 5, 3*8);	
		screen.SendString("s", 5, 4*8);
	}
	
}

Tell me if you have any question about my code :slight_smile:

I’m using Arduino Uno r2 and i was selected it in IDE (Arduino 1.6.5). I compile with AVRISP mkll, this is the default programmer selected in the IDE.

I am doing my best for getting help in english section whereas i’m french :slight_smile:
No answer in french section …
Thanks.

Hi !

I’ve resolved my problem. I created a float function for showing the float value on the screen.

There is it:

void LCD::SendFloat(float floatToWrite, int line, int pos)
{
	if(floatToWrite >= 0.0 && floatToWrite <= 9.99)
	{
		int u = int(floatToWrite); // Number before decimal point
		float tmp = floatToWrite*100;
		int decimal = int(tmp)-u*100; // Number after decimal point (.00)
		int _d = decimal / 10; // tenth of a second
		int _c = decimal % 10; // hundredth of a second
		this->SendInt(u, line, pos); // Write a int on my screen
		this->SendString(".", line, pos+8);
		this->SendInt(_d, line, pos+(2*8));
		this->SendInt(_c, line, pos+(3*8));
	}
//To be completed for >10.0 compatibility :)
}

Out is: 4.56 for exemple.

Arduino doesn’t like to print or manipulate float values…

Thanks at all !