Programming UV-Sensor

Hi There,
I’m relatively new to Arduino programmming and already have a nice project.

I want to read data from a adafruit guva-s12sd UV Sensor, calculate the energy in mW/cm2 and display it on a 7 digits serial display (Sparkfun).
Here’s my error code. It says:

Arduino: 1.6.5 (Windows 7), Platine: “Arduino Duemilanove or Diecimila, ATmega328”

_20150909_Programm_UV-Sensor.ino: In function ‘void loop()’:
_20150909_Programm_UV-Sensor:25: error: ‘disp’ was not declared in this scope
_20150909_Programm_UV-Sensor:26: error: ‘setDecimals’ was not declared in this scope
_20150909_Programm_UV-Sensor:28: error: expected ‘;’ before ‘}’ token
‘disp’ was not declared in this scope

The code is:

#include <SoftwareSerial.h>

void setup()
{
// These are the Arduino pins required to create a software seiral
// instance. We’ll actually only use the TX pin.
const int softwareTx = 8;
const int softwareRx = 7;

SoftwareSerial disp(softwareRx, softwareTx);
// Must begin s7s software serial at the correct baud rate.
// The default of the s7s is 9600.
Serial.begin(9600);// open serial port, set the baud rate to 9600 bps
disp.begin(9600);

// Clear the display, and then turn on all segments and decimals clearDisplay(); // Clears display, resets cursor

}

void loop()
{
int UV;
UV = analogRead(A0);//connect UV sensors to Analog 0
int Watt = (UV * 0.0042 * 0,4778);
disp.print(Watt); // Displays Watt Value
setDecimals(0b00000100);
delay(200)
}

In the attachment, you will find the wiring. What I am doing wrong?

Best regards,

Robert

SoftwareSerial disp(softwareRx, softwareTx);

This is a local variable. It goes out of scope at the end of setup(). Move it before setup() to make it global, so it can be accessed in loop().

  setDecimals(0b00000100);

You have not defined this function. You can’t call undefined functions.

  delay(200)

Missing something? ;;;;;;;;;;;;;;;;;;;;;;;;;

I get an additional error:

xTest.ino:24:27: warning: left operand of comma operator has no effect [-Wunused-value]

for the line

int Watt = (UV * 0.0042 * 0**,**4778);

You should change the 0,4778 to a 0.4778.

OK, many thanks. There is no error code with this:

#include <SoftwareSerial.h>
const int softwareTx = 8;
const int softwareRx = 7;
SoftwareSerial disp(softwareRx, softwareTx);

void setup()
{

Serial.begin(9600);// open serial port, set the baud rate to 9600 bps
disp.begin(9600);
}

void loop()
{
int UV;
UV = analogRead(A0);//connect UV sensors to Analog 0
int Watt = (UV * 0.0042 * 0.4778);
disp.print(Watt); // Displays Watt Value
Serial.println(UV);
delay(200);
}

BUT, my Serial Display only shows “0000”, wheras the serial monitor shows the correct analog readout.

Anything wrong with the printout at the serial display?

  disp.print(Watt);  // Displays Watt Value
  Serial.println(UV);

It does not make sense to print two different things, as a way of assuring that the code is working.

Anonymous printing sucks. Identify what you are printing!

OK, please explain what your'e meaning! I'm a total noob to this!
Thanks,
Rob

OK, please explain what your'e meaning!

Why are you printing one value to the display and another to the serial monitor?

  disp.print(Watt);  // Displays Watt Value

  Serial.print("UV = ");
  Serial.println(UV);
  Serial.print("Watt = ");
  Serial.println(Watt);

This is dumb:

int UV;
  UV = analogRead(A0);//connect UV sensors to Analog 0

Do it like a real programmer:

  int UV = analogRead(A0);//connect UV sensors to Analog 0

OK, understand what you mean... thanks.
But still, my 7-segment-serial display shows "0000"
Why?

RobNie:
my Serial Display only shows "0000", wheras the serial monitor shows the correct analog readout.

What is displayed?

Watt = (UV * 0.0042 * 0.4778)
Watt = UV * 0.00200676

so UV has to be around 500 to make Watt 1.

How about using milli-Watt?

mWatt = UV * 2.00676

even mWatt = UV * 2 should only create a small error, if any.

BTW is this your display unit?

Yes, this is the display unit!

Now I changed the code to:

#include <SoftwareSerial.h>
const int softwareTx = 8;
const int softwareRx = 7;
SoftwareSerial disp(softwareRx, softwareTx);

void setup()
{
disp.begin(9600);
Serial.begin(9600);
clearDisplay(); // Clears display, resets cursor
disp.print("-HI-"); // Displays -HI- on all digits
setDecimals(0b111111); // Turn on all decimals, colon, apos
clearDisplay(); // Clears display, resets cursor
}

void loop()
{
clearDisplay(); // Clears display, resets cursor
int UV = analogRead(A0);//connect UV sensors to Analog 0
float mW = (UV*9/1024);
disp.print(mW); // Displays Watt Value
Serial.print("UV = ");
Serial.println(UV);
Serial.print("mW = ");
Serial.println(mW);
delay(1000);
}

void setDecimals(byte decimals)
{
disp.write(0x77);
disp.write(decimals);
}

void clearDisplay()
{
disp.write(0x76); // Clear display command
}

At the serial monitor I get this:

mW = 1.00
UV = 128
mW = 1.00
UV = 129
mW = 1.00

There seems something wrong with the float variable, if I use a Calculator, I get 1,1338 mW

But still nothing on the display…

You are right, there is somthing wrong with the float variable.

For the conversion I suggested the result is mW expressed in integer.
What do you want to use the microWatt part for?
To display it in 4 digits?
What should be displayed for a value of 1,1338?

Why did you change the conversion factor from 2.00676 to 0,0087890625 (9/1024)?

Yes, I want to display the mW on the 4 digits display.
The conversion factor changed, because I found an error in my calculation. This is the right conversion factor now.

int UV = analogRead(A0);//connect UV sensors to Analog 0   
  float mW = (UV*9/1024);

You do NOT want to be doing integer arithmetic.

 float mW = (UV * 9.0/1024.0); // White space added so you can actually read stuff

Yeah, right... I tried integer and got no decimal places. Now it works, I tried it with:

float mW = (UV * 9.00/1024.00);

Also, my display is now showing digits :slight_smile: :slight_smile: :slight_smile:

The only Problem I have now is, ther are no decimal points on the display. Actually, there are decimal points, but the code did not use them...

Thanks!

RobNie:
The only Problem I have now is, ther are no decimal points on the display. Actually, there are decimal points, but the code did not use them...

Thanks!

YOU (as a creator of your code) did not advise the display to show them.

RTFM Basic Usage · sparkfun/Serial7SegmentDisplay Wiki · GitHub.