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.

common792

Junior Member level 3
Joined
Aug 20, 2011
Messages
25
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,486
Hi Everyone

I am doing a placement and I am trying to learn a new IDE called Arduino. My Supervisor have asked me to modifiy this function called Void getLocalData() , but my issue that i do not understand it at all and i do not know where/how to start.
I am freaking out!:)


Here is the function

void getLocalData()
{
// Declare and generate random numbers.
short int val1 = getRndNumber(100);
short int val2 = getRndNumber(200);
short int val3 = getRndNumber(300);
short int val4 = getRndNumber(400);
short int val5 = getRndNumber(500);
short int val6 = getRndNumber(600);
short int val7 = getRndNumber(700);
short int val8 = getRndNumber(800);

// Local char strings are needed to store----------> instructions
// the random data.

char cVal1[7]=? ;
char cVal2[7]= ;
char cVal3[7]= ;
char cVal4[7]= ;
char cVal5[7]= ;
char cVal6[7]= ;
char cVal7[7]= ;
char cVal8[7]= ;


// Convert and store the random data.
itoa(val1, cVal1, 1);
itoa(val2, cVal2, 2);
itoa(val3, cVal3, 3);
itoa(val4, cVal4, 4);
itoa(val5, cVal5, 5);
itoa(val6, cVal6, 6);
itoa(val7, cVal7, 7);
itoa(val8, cVal8, 8);


// Construct the string that will store----------------->Instructions
// the final stream to be transmitted.
strcpy(cOutData,cVal1);
strcat(cOutData,ASCII_DELIM);
strcat(cOutData,cVal2);
strcat(cOutData,ASCII_DELIM);
strcat(cOutData,cVal3);
strcat(cOutData,ASCII_DELIM);
strcat(cOutData,cVal4);
strcat(cOutData,ASCII_DELIM);
strcat(cOutData,cVal5);
strcat(cOutData,ASCII_DELIM);
strcat(cOutData,cVal6);
strcat(cOutData,ASCII_DELIM);
strcat(cOutData,cVal7);
strcat(cOutData,ASCII_DELIM);
strcat(cOutData,cVal8);
strcat(cOutData,ASCII_DELIM);


}

Can someone help me please?

Thank you
 

The underlying compiler for Arduino is a open source C language compiler with the addition of numerous library function which simply the task of programming by hiding many of the gritty details.

Do you have any C programming experience?

BigDog

---------- Post added at 17:24 ---------- Previous post was at 17:20 ----------

What modification do you need to make to the code?
 
  • Like
Reactions: FvM

    FvM

    Points: 2
    Helpful Answer Positive Rating
Hi Sir,

Thank you for replying me....

I need to be able to send some data as a stream.
Do i need to need to add something to this line?
"char cVal1[7]= ;"

Would you like to see the whole code?
 

Would you like to see the whole code?

Yes, please zip up your sketch/project and upload it.

I need to be able to send some data as a stream.
Do i need to need to add something to this line?
"char cVal1[7]= ;"

What type of stream and where to? To the PC via USB/Serial Port?

Can you tell me if you have any C programming experience?

BigDog
 
  • Like
Reactions: FvM

    FvM

    Points: 2
    Helpful Answer Positive Rating
yes sorry, I have C Programming experience.

We sending data to a database(Host) via GPRS Board.
I have attached the code, saved as a note pad document.(sorry for my english, i am french)
 

Attachments

  • Code.txt
    27.5 KB · Views: 54

Looking at your code, Serial3 appears to be attached to the GPRS board. Do you want to stream the data through serial3?
 

I would recommend creating another function, use a for or while loop, the cVal1[] array and the Serial3.print() along with any HTTP formatting required.

It does appear you are outputting all data in HTTP format. Am I wrong?

After calling the getLocalData() you can call sendLocalData() which is the function discussed above.

BigDog
 
No you are right!

So you think that the function getLocalData is fine, it does not need to be modified.
To send the data i used the function Senddata()
 

So you think that the function getLocalData is fine, it does not need to be modified.

I don't know, you tell me. Does getLocalData() sucessfully generate the random data that you require and store this data in the correct string variable cOutData?

To send the data i used the function Senddata()

Or should getLocalData() store the values in arrays cValX[] in the string variables sDataFieldX which the Senddata() uses to construct the HTTP sentence before transmitting them.


BigDog
 
  • Like
Reactions: FvM

    FvM

    Points: 2
    Helpful Answer Positive Rating
GetLocalData() store the value in arrays cValx[] int the string variables sDataFieldsx but I was wondering( i do not know) if they are already converted into a string or I have to do it?
 

Try this:

sDataField1 = String('cVal1');

If you need to concatenate and additional text, you can try this:

sDataField1 = String('cVal1' + "other text");

It appears you could replace these statements with the example above:

Code:
  String sDataField1=String('cVal1');
  String sDataField2=String('cVal2');
  String sDataField3=String('cVal3');
  String sDataField4=String('cVal4');
  String sDataField5=String('cVal5');
  String sDataField6=String('cVal6');
  String sDataField7=String('cVal7');
  String sDataField8=String('cVal8');

or with this:

Code:
  sDataField1=String('cVal1');
  sDataField2=String('cVal2');
  sDataField3=String('cVal3');
  sDataField4=String('cVal4');
  sDataField5=String('cVal5');
  sDataField6=String('cVal6');
  sDataField7=String('cVal7');
  sDataField8=String('cVal8');



And possibly do away with this block of statements entirely:

Code:
// Construct the string that will store
  // the final stream to be transmitted.
  strcpy(cOutData,cVal1);  
  strcat(cOutData,ASCII_DELIM);    
  strcat(cOutData,cVal2);  
  strcat(cOutData,ASCII_DELIM);    
  strcat(cOutData,cVal3);  
  strcat(cOutData,ASCII_DELIM);    
  strcat(cOutData,cVal4);  
  strcat(cOutData,ASCII_DELIM);    
  strcat(cOutData,cVal5);  
  strcat(cOutData,ASCII_DELIM);    
  strcat(cOutData,cVal6);  
  strcat(cOutData,ASCII_DELIM);    
  strcat(cOutData,cVal7);  
  strcat(cOutData,ASCII_DELIM);    
  strcat(cOutData,cVal8);  
  strcat(cOutData,ASCII_DELIM);

Unless it is needed somewhere else in the code.

Let me know if the modification work as required.

BigDog
 
Last edited:
  • Like
Reactions: FvM

    FvM

    Points: 2
    Helpful Answer Positive Rating
Hi
Sorry I had to leave my home yesterday.
Yes this what I thought I will try tomorrow morning. I will keep you updated

Thank you
 

Hi common792,

No problem. I would be interested to know what is the actual purpose of the design. I was intrigued after looking over your code again.

BigDog
 

Hi,

How are you today?
I am working on a project which one involves 3 sensors located in the toilets of a airport.
Thoses sensor are set up on a panel. the first sensor is on the top(toilet are clean) the middle one for :the toilet are dirty and the bottom one for counting the number of people have been in the toilet. Each person who has been in the toilet needs to swap their hand through on of those sensor(top or middle).

So the sensor store all this data and once every 3 ou 10 minutes(I need to be able to change the timing) send them to a database(Smallint type) as a stream so the staff of the airport can send someone to clean it.

Before changing the code I have sent tou you 2 days ago,I need to make a code able to send thoses data as a stream and after a certain amont of time. But my data are sent like this.

34,000000000,0,1(to simulate the people)
163(Good=clean)
,178(bad=dirty)

Here is my code:

//#include "TimerOne.h"

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


void setup() {
Serial.begin(9600); //At 9600 bits of data per second

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)
Timer1.initialize(500000);
Timer1.pwm(9, 512);
}
void loop()

{

if(sensorValuePot=!0)
{
sensorValuePot = analogRead(56); //Variable to store the resistance value
}
Switch1 = digitalRead(54); //Variable to store the resistance value

if(Switch1==HIGH)
{
digitalWrite(5, HIGH); // Set this pin High,LED turns on
count=count++;
Serial.print(sensorValuePot, DEC); Serial.print(","); // To print this info as a decimal value
Serial.print (Switch1, DEC);Serial.print(","); // To print this info as a decimal value
Serial.println (Switch2, DEC); // To print this info as a decimal value
Serial.println (count, DEC);Serial.print(",");
Serial.println (countbad, DEC);
digitalWrite(5, LOW);
}
else{
digitalWrite(5, LOW); //digitalWrite(38, LOW);
}

Switch2 = digitalRead(55); //Variable to store the resistance value
if(Switch2==HIGH)
{
digitalWrite(2, HIGH); // Set this pin High,LED turns on
countbad=countbad++;
Serial.print(sensorValuePot, DEC); Serial.print(","); // To print this info as a decimal value
Serial.print (Switch1, DEC);Serial.print(","); // To print this info as a decimal value
Serial.println (Switch2, DEC); // To print this info as a decimal value
Serial.println (count, DEC);Serial.print(",");
Serial.println (countbad, DEC);
digitalWrite(2, LOW);
}
else{
digitalWrite(2, LOW);
}
}




Do you have an idea?
 

How are you today?

Fine.

I am working on a project which one involves 3 sensors located in the toilets of a airport.
Thoses sensor are set up on a panel. the first sensor is on the top(toilet are clean) the middle one for :the toilet are dirty and the bottom one for counting the number of people have been in the toilet. Each person who has been in the toilet needs to swap their hand through on of those sensor(top or middle).

So the sensor store all this data and once every 3 ou 10 minutes(I need to be able to change the timing) send them to a database(Smallint type) as a stream so the staff of the airport can send someone to clean it.

Before changing the code I have sent tou you 2 days ago,I need to make a code able to send thoses data as a stream and after a certain amont of time. But my data are sent like this.

34,000000000,0,1(to simulate the people)
163(Good=clean)
,178(bad=dirty)

Do you have an idea?

Is this not the correct format?

Code:
34,000000000,0,1(to simulate the people)
163(Good=clean)
,178(bad=dirty)

Or rather like this:

Code:
34,000000000,0,1,163,178

Can you explain this portion of the data sentence:

Code:
34,000000000,0,1

How does the above portion of the data sentence represent people exactly?

If you could provide the exact format of the data and what each field represents, I maybe able to help you.

BigDog
 
  • Like
Reactions: FvM

    FvM

    Points: 2
    Helpful Answer Positive Rating
The first data is a number,the second should be the number of people saying they the toilet are clean,the third one is for the number of people saying that the toilet are dirty, and the fourth one is the number of people have been in the toilet. A fifth and a sixth (I am using them as a switch simulating the people, when switch1 is on,someone has said that the toilet are clean etc.. ) I need to have 2 more data (0,0)as spare for future improvement the order is not really important .

Does that make sense?
 

Does that make sense?

Yes, I believe so.

The first data is a number,the second should be the number of people saying they the toilet are clean,the third one is for the number of people saying that the toilet are dirty, and the fourth one is the number of people have been in the toilet. A fifth and a sixth (I am using them as a switch simulating the people, when switch1 is on,someone has said that the toilet are clean etc.. ) I need to have 2 more data (0,0)as spare for future improvement the order is not really important.

34,000000000,0,1,163,178

Toilet Number: 34
Votes Clean: 000000000
Votes Dirty: 0
Usage Count: 1
Switch Clean?: 163
Switch #2?: 178
============= Add for future use
Spare #1: 0
Spare #2: 0

Is this correct? You could use the individual bits in the Switch value, each bit represents the status of a switch.

BigDog
 
  • Like
Reactions: FvM

    FvM

    Points: 2
    Helpful Answer Positive Rating
That's right!

Toilet number:34
Switch1: 0 or 1(0: no one close to the sensor, 1:someone is close to sensor("people saying it is clean" incrementes))
switch2: 0 or 1(0: no one close to the sensor, 1:someone is close to sensor("people saying it is dirty" incrementes))
Usage count: 1
people saying it is clean:
People saying it is dirty:
Spare 1:0
Spare 2:0
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top