Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Issue to understand a code in arduino

Status
Not open for further replies.
First things, I do not know how to send those data as a stream(all the data in the same line separated by ",") and how can i include a timer(to send the data every 3,4,10 mins)?
 

You have seveal options when considering delays in the 3 to 10 minute range and longer.

1. Repeated use of the timer, setting and clearing overflow, to generate the necessary interval. Inconjunction with either using polling or interrupts to handle the event.

2. Utilize a Real Time Clock module, a small serial access device which keeps relatively accurate time to calculate the required interval.

3 Off board source such as NTP, GPS or possibly GSM/GPRS module if the feature is provided.

Which option seems viable, for your application? Option three maybe the simplest if your GSM/GPRS provides this feature. You may want to check its documentation.

BigDog
 
I think the first one would be fine.
I am using an ATMEGA2560.
 

Sorry for the delay.

So your using the Arduino MEGA 2560.

You maybe tempted to use the available delay() functions, however below is the cautionary note concerning its use:

While it is easy to create a blinking LED with the delay() function, and many sketches use short delays for such tasks as switch debouncing, the use of delay() in a sketch has significant drawbacks. No other reading of sensors, mathematical calculations, or pin manipulation can go on during the delay function, so in effect, it brings most other activity to a halt. For alternative approaches to controlling timing see the millis() function and the sketch sited below. More knowledgeable programmers usually avoid the use of delay() for timing of events longer than 10's of milliseconds unless the Arduino sketch is very simple.

Have a look at the following link, which covers a simple timer library, which can time events:

SimpleTimer Library for Arduino

There are other options available, however this may do the job.

BigDog
 

my piece of code needs to be at the place of "repeat me()"? and how can I send data as a stream?
 

my piece of code needs to be at the place of "repeat me()"?

Correct. You'll want to place your data streaming function name in place of repeatMe.


Code:
#include <SimpleTimer.h>

// the timer object
SimpleTimer timer;

// a function to be executed periodically
void myDataStream() {
    ....
    ....
    ....
}

void setup() {
    Serial.begin(9600);
    timer.setInterval(1000, myDataStream);
}

void loop() {
    timer.run();
}

The delay interval accepts a long integer, so a delay of 4,294,967,295 mS, 4294967.295 S, or 71582.78825 minutes can be implemented. If I understand the documentation correctly.

and how can I send data as a stream?

What is wrong with the way you have been sending it? I'm not sure I know the meaning of "send data as a stream" in context with the Arduino.

Is there some particular formatting required other than that discussed above?

BigDog
 

I think i get it for the timer.

I mean,when I open "serial monitor" in arduino, the data are not like that: X1,X2,X3,X4 etc.
And this what i want.
The database where I am dumping the data into are "smallint" type
 

I think i get it for the timer.

Très bien!

I mean,when I open "serial monitor" in arduino, the data are not like that: X1,X2,X3,X4 etc.

Your database will not accept comma delimited? Or am I miss understanding, X1,X2,X3,X4 etc, is correctly formatted?

If you can specify it, we most likely can format it correctly.

And this what i want.
The database where I am dumping the data into are "smallint" type

The term smallint is a relative term, especially when comparing database servers and MCUs.

How many bytes or bits does the data type need to be in length?

BigDog
 
  • Like
Reactions: FvM

    FvM

    Points: 2
    Helpful Answer Positive Rating
"très bien"

Nice!(so you can speak french?)

The screen on serial monitor displays this:

34,000000000,0,1
163
,178)

instead of
34,0,0,1,163,178.

My supervisor advised me to use integer
 

The screen on serial monitor displays this:

34,000000000,0,1
163
,178)

instead of
34,0,0,1,163,178.

Ok, I understand. Yes, it can be formatted as you have requested. We'll use a routine to format it correctly before sending the data.

My supervisor advised me to use integer

Two bytes or four bytes? The Arduino type "int" is two bytes, while the type "long" is four bytes.

I need to take a break soon. Why don't you experiment with the SimpleTimer Library, turn on and off the LED on the Arduino with different time durations. We can continue this either later today or in your morning. It must be getting late in France?

BigDog
 
  • Like
Reactions: FvM

    FvM

    Points: 2
    Helpful Answer Positive Rating
Ok I will try tomorrow morning.

yes it is 0.20 am. It is bedtime. Good night or should i say have a good day!

i'll catch you later
 

Hi BigDog!

I am little bit tired tonight, I am going straight in my bed!
I hope to catch you tomorrow, I will leave you the code i have run this morning, it works but not as I want it to work.

See you tomorrow(or tonight ;-)
 

Hi,

Can you post or upload your the code , so that I might have a look at it today?

It will help prepare me for your questions.

BigDog
 

Sorry BigDog,I was sleeping already

here is the code,

#include <SimpleTimer.h>

SimpleTimer timer;

int sensorValuePot; // variable to store the value from the potentiometer
int Switch1;
int Switch2;
int count;
int countbad;

String sDataField1 = "0";
String sDataField2 = "0";
String sDataField3 = "0";
String sDataField4 = "0";
String sDataField5 = "0";
String sDataField6 = "0";
String sDataField7 = "0";
String sDataField8 = "0";

void myDataStream()
{
if(sensorValuePot=!0)
{
sensorValuePot = analogRead(56); //Variable to store the resistance value

}
Switch1 = digitalRead(54); //Variable to store the resistance value
Switch2 = digitalRead(55); //Variable to store the resistance value


// Field 1 - Random Number
sDataField1 = String(34, DEC);

// Field 2 - Switch one
if(Switch1==HIGH)
{
count=count++;
digitalWrite(2, HIGH);
sDataField2 = String(1, DEC);
sDataField4 = String (1, DEC);

}else
{
digitalWrite(2, LOW);
sDataField2 = String(0, DEC);
sDataField4 = String (0, DEC);
}
// Field 3 - Switch two
if(Switch2==HIGH)
{
countbad=countbad++;
digitalWrite(5, HIGH);
sDataField3 = String(1, DEC);
sDataField5 = String (1, DEC);
}else
{
digitalWrite(5, LOW);
sDataField3 = String(0, DEC);
sDataField5 = String (0, DEC);
}

// Field 6 - POT
sDataField6 = String(sensorValuePot, DEC);
// Field 7 - Good
sDataField7 = String(count,DEC);
// Field 8 - Bad
sDataField8 = String(countbad,DEC);
Serial.print(""+sDataField1+","+sDataField2+","+sDataField3+","+sDataField4+","+sDataField5+","+sDataField6+","+sDataField7+","+sDataField8+"\n");
sDataField1 = 0;
sDataField2 = 0;
sDataField3 = 0;
sDataField4 = 0;
sDataField5 = 0;
sDataField6 = 0;
sDataField7 = 0;
sDataField8 = 0;
}
void setup() {
Serial.begin(9600); //At 9600 bits of data per second
timer.setInterval(5000 , myDataStream);
pinMode(54, INPUT); // Set the pin as Analog input
pinMode(55, INPUT); // Set the pin as Analog input
pinMode(56, INPUT); // Set the pin as Analog input
pinMode(5, OUTPUT); // Set the pin as Analog Output(LED 1)
pinMode(2, OUTPUT); //" " (LED 2)
pinMode(3, OUTPUT); //" " (Value)
}
void loop()

{

timer.run();
}


It is working but I want it to keep counting during the timer and at the end of the counting,it send the stream.
Because there, it is just sending the stream every x mins.

Do you know what I mean?
 

Hi,

So, when the SimpleTimer routine is running the count is suspended. Is this correct?

I thought the SimpleTimer may utilize an interrupt, apparently it does not.

You will have to implement a interrupt routine to handle the timer and message transmission.

BigDog
 

ok I will try tomorrow then,
Can I ask you an other question?
It will be the last one!

I do not want to bother you more
 

Here is the code
 

Attachments

  • code1.txt
    27.9 KB · Views: 38

I've downloaded your code. Is this the section which is not performing correctly?

void ReadSensors()
{
Switch1 = digitalRead(54); //Variable to store the resistance value
Switch2 = digitalRead(55);

if(Switch1==HIGH)
// if (analogRead(Good) > 300) // Good Sensor Activated
{
digitalWrite(2,HIGH); // Green LED On
GoodCounter++; // Increment Good Counter
sDataField1 = String(1, DEC);
}
else
{
digitalWrite(2,LOW);
sDataField1 = String(0, DEC);
}

if(Switch2==HIGH)
//if (analogRead(Bad) > 300) // Bad Sensor Activated
{
digitalWrite(5,HIGH); // Red LED On
BadCounter++; // Increment Bad Counter
sDataField2 = String (1, DEC);
}
else
{
digitalWrite(5, LOW);
sDataField2 = String(0, DEC);
}
...
...
...

BigDog
 
yes, when swtich 2==1 nothing happens on the stream(In the Datbase)

Have I written this line right?
"
sDataToSend = sDataField1+ASCII_DELIM+sDataField2+ASCII_DELIM+sDataField3+ASCII_DELIM+sDataField4+ASCII_DELIM+sDataField5+ASCII_DELIM+sDataField6+ASCII_DELIM+sDataField7+ASCII_DELIM+sDataField8+"; "
 

Hi BigDog,


How are you?
Sorry I was away for fews days...
Did you have the time to have a look at my code?
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top