Arduino connected to bolt -2 analog sensors

@vinayak.joshi Sir,
I did with 2 CSV value and it is working fine with push data command, but I also want to control brightness of an LED. Since that is done using command handler, I tried receiving 2 analog values and controlling an LED using the code shown in the previous post. That is why I declared 3 CSV, please guide in my doing this.

Hi,

You don’t need the 3rd CSV value for that. The CSV value is just the data that gets sent from the Arduino to the Bolt Cloud.

For controlling the LED, you have to send data from the Bolt Cloud to the Arduino. This can be done using the serialWrite function in your code javascript code.

singleButton({ name : "turn on", action :"serialWrite", arguments : ["TON"], align : "left"});
singleButton({ name : "turn off", action :"serialWrite", arguments : ["TOFF"], align : "right"});

In your Arduino code, you will have to add the following code to the setup function.

boltiot.setCommandString("TON", turnOnLED);
boltiot.setCommandString("TOFF", turnOffLED);

Also, you will have to add 2 new functions to your Arduino code as follows:

String turnOnLED(String *data){
    //Add code here to turn on the LED.
    return "";
}


String turnOffLED(String *data){
    //Add code here to turn off the LED.
    return "";
}

Once done, you will see 2 buttons in your device view. One will turn on the LED and the other will turn off the LED.

@vinayak.joshi
Dear Sir,
I am still getting the readings in graph, but LED is not switching on using the buttons.

Hi @swethatsunil.mec,

Please go through the following reference sheet for functions available on the Arduino IDE and how they are supposed to be used.

You have used analogWrite in an attempt to control pin A5.
There are 2 problems with your approach.
analogWrite only works for pin 3,5,6, 9,10 and 11 on the Arduino UNO, or the Boltduino. These pins can also be identified by the ‘~’ mark next to them on the board. The other pins will be like 7, or 8. But these pins will be printed at ~3 or ~6.

So analogWrite does not work with pin A5.

Even if it did work on pin A5, analogWrtie takes a number in the range of 0 to 255 to set the voltage on the given pin.
0 for 0 v and 255 for 5v.
In Arduino ‘LOW’ translates to 0, which is correct for you, where as ‘HIGH’ translates to a 1, which means that even if analogWrite worked for pin A5, you would be setting a voltage of about 0.02v to the pin, which would not be enough to turn on an LED.

The correct way of doing this, is to use the instruction

digitalWrite(A5, HIGH);

To turn the LED on, and

digitalWrite(A5, LOW);

to turn the LED off.

Please read up on the functions available on the Arduino documentation here before trying them out.

@vinayak.joshi
Dear Sir,
Will the buttons work only after 5 min, that is if I have selected data transfer to be done every 5 minutes in configuration of product ?

Hi @swethatsunil.mec,

Did you test the changes I mentioned in the Arduino code?

The buttons should work immediately. The 5 minutes interval that you set in the hardware configurations is only meant for collecting data from the Bolt WiFi module.

@vinayak.joshi
Dear Sir,
I made the changes that you said, but the buttons are not working always. It is working only after an interval of 5 minutes.

Hi @swethatsunil.mec,

Please do not post screenshots of your arduino code. Please post the actual code that you wrote. This way I can make the required edits to your code and reply back to you. It is not possible for me to copy the code from your screenshots.

I just realised what is causing the issue. In an earlier attempt you had used the handleCommandString to set the pushData fucntion for the command RD\r and were returning the 2 analogReadings via the pushData function. You can handle commands being sent via the Bolt Cloud, “only if” you are doing this.

YOU SHOULD NOT USE THE handleCommand AND THE processPushDataCommand IN THE SAME CODE. THEY INTERFERE WITH EACH OTHER.

Right now, you will need to remove the ‘processPushDataCommand’ function call from your code.

Add the String pushData(String *data) function which you had used earlier to send back 2 analog readings, and use the boltiot.setCommandString("RD\r",pushData); instruction in your setup function.

Your code will start working as expected.

@vinayak.joshi
Dear Sir,
Thank you for your response, the buttons are now working fine, but I want the Led to be turned on using an FSR. That is when the FSR is clicked once, the LED glows, one more click and it should go off. The following is the code that I have written for the same. But it is not working.

#include <BoltIoT-Arduino-Helper.h>
#ifndef API_KEY
#define API_KEY “–”
#endif
#ifndef DEVICE_ID
#define DEVICE_ID “–”
#endif
void setup(){
boltiot.begin(Serial);
pinMode(A0,INPUT);
pinMode(A1,INPUT);
pinMode(3,OUTPUT);
boltiot.setCommandString(“TON”, turnOnLED);
boltiot.setCommandString(“TOFF”, turnOffLED);
boltiot.setCommandString(“RD\r”,getAnalogData);
boltiot.setCommandString(“GetAnalogData”,getAnalogData);
}
String getAnalogData(String *data){
String retval="";
retval=retval+analogRead(A0)+",";
retval=retval+analogRead(A1);
return retval;
}
String turnOnLED(String *data){
analogWrite(3,255);
return “”;
}
String turnOffLED(String *data){
analogWrite(3,0);
return “”;
}
int count=0;
void loop(){
boltiot.handleCommand();
int ldr=analogRead(A0);
int fsr=analogRead(A1);
if (fsr>=4){
count++;
if (count%2!=0){
turnOnLED;
}
else{
turnOffLED;
}
}
}

@vinayak.joshi please reply soon

Try replacing the following:

if (fsr>=4){
       count++;
       if (count%2!=0){
           turnOnLED;
        }
        else{
           turnOffLED;
       }
}

with

bool touched = fsr>=4;
switch(count){
	case 0:
		if(touched){
			turnOnLED();
			count =1;
		}
	break;
	case 1:
		if(!touched){
			count =2;
		}
	break;
	case 2:
		if(touched){
			turnOffLED();
			count =3;
		}
	break;
	case 3:
		if(!touched){
			count =0;
		}
	break;
}

The idea here is that you will use count to know what was the previous states.
if count takes the value
0: The user had touched left the sensor in the last loop, or was not touching the sensor in the last loop, if in this loop, the user touches the sensor, the led should be turned on.
1: The user had touched the sensor in the last loop. The code is waiting for the user to let go of the FSR.
2: The user had touched left the sensor in the last loop, or was not touching the sensor in the last loop. If the user touches the sensor in this loop, the led should be turned off.
3: The user had touched the sensor in the last loop, and the code is waiting for the user to let go of the sensor.

I have not tested this code myself, but I thought it would be a good idea to explain to you the logic required so that you can fix the code if it does not work as expected.

Dear Sir,

I tried the code, but it is also not working. But, I have got a doubt in it. When I call the turnOnLED function as

turnOnLED()

an error arises stating-

too few arguments to function ‘String turnOnLED(String*)’

. So, I replaced it with

turnOnLED

alone; without brackets. Now the error is not coming but still it’s not working.

Hi,

You are getting the error too few arguments to function due to the function definition as shown below:

String turnOffLED(String *data){

String turnOnLED(String *data){

Remove the String *data from the function definition so that the function declarations look as below.

String turnOffLED(){
String turnOnLED(){

Please make the above updates to your code and try to run it.

1 Like

@pranav.kundaikar.inv Thank you so much Sir.

1 Like

new1111

new1

not getting :pensive: :innocent: :innocent: :innocent: :joy: :joy:

Hi,

Please power up the device and click on “push data to cloud” button.

Also, which Arduino are you using?

IM using arduino uno…

Hi @edwin.jose,

I have invited @vinayak.joshi to help you out on this thread Not able to get data from Arduino uno

Do let me know in case you need further assistance.

Thanks for the support…

i got it :+1: :+1: :+1: :+1:

Hi @edwin.jose,

I just realised what issue you had,

You used the processPushDataCommand function, which does not require you to have 6 CSV values.

If you had used the CheckPoll function, you would have had to set 6 CSV values.