A little help with some code please


#1

I’m helping my son with a science project. He wanted to test batteries in his xbox controller to see how long each brand or type lasted. This would take more time than we have because the batteries usually last him a week. So I got the batteries to be tested and a flash light. The batteries are lasting longer than expected so I can’t monitor them full time. I setup my photon with the photo resistor to log the light from the flash light. It’s a lot more accurate than doing it by the clock. My issue is with the code. I’m not sure how many entries the console saves for me to review and if it dies and goes on for hours then I might not be able to see where it died at. I would really like to have the code stop publishing when the sensor reads zero. I’m a newbie so please forgive me. Currently I hope it is outputting the code every 2 seconds and if it is zero it goes to an hour? I know it outputs every 2 seconds now but I’m not sure what happens after that. Any help would be greatly appreciated.

int photoResistorOuput = A5;
int photoResistorReading = A0;
int light;

void setup() {
	pinMode(photoResistorOuput, OUTPUT);
	pinMode(photoResistorReading, INPUT);
	
	digitalWrite(photoResistorOuput, HIGH);
	
	Particle.variable("analogvalue", photoResistorReading);
	
}	

void loop() {
	photoResistorReading = analogRead(A0);
	float photoResistorReading = analogRead(A0);
	light = (int)(photoResistorReading/4095*100);
	if (light == 0)
	{
		Particle.publish("light", String(light) + "%");
		delay(3600000);
	}
	Particle.publish("light", String(light) + "%");
	delay(2000);
	
}

#2

Hi Matt,

What happens if you would refactor the following lines:

if (light == 0)
{
Particle.publish(“light”, String(light) + “%”);
delay(3600000);
}
Particle.publish(“light”, String(light) + “%”);
delay(2000);

into:

// if less than 5%, would it work for what you are trying to do?
if (light < 5)
{
  Particle.publish(“light”, String(light) + “%”);
  delay(3600000);
} else {
  Particle.publish(“light”, String(light) + “%”);
  delay(2000);
}

Would that work for what you are trying to do?
Gustavo.


#3

I’m sure that would help. Less than 5% should account for the light being dead. Or close enough. I would really like to stop the publishing but wasn’t sure how. If the light>5 then publish. If not stop publishing.


#4

@jm_middleton, this is slightly off-topic to your question since it looks like @gusgonnet gave a great suggestion to help with your code.

One way to control the time of your test is to choose a different wattage (power) of light bulb. The greater the watts of the light bulb, the faster it will drain the battery. However, you do need to choose carefully. Too high of wattage will cause the light bulb to generate a lot of heat (don’t get burned). Also, draining the battery faster than it is capable may also heat up the battery internally and cause some permanent damage to it. Maximum battery drain capacity is usually expressed in Amps; you can calculate that from watts or vice versa. Don’t worry, looking that up will not interfere with your test, since your son’s project will give you the battery storage capacity (Amp-Hours).

Have fun!


#5

I wanted to say thank you to everyone who replied. @cyclin_al your comment made me start thinking outside the box. Should be able to help my son create an awesome science project.


#6

FYI, the console does not store any data. If the page is refreshed, all data is gone.
You could use IFTTT to store the values in a Google spreadsheet, which has the added benefit of allowing you to plot the intensity over time and thus the battery decline.


#7

Please let us know how it goes, and good luck!
Come back anytime :wink:
Gustavo.


#8

I logged when we started and have checked it periodically to collect some data. If you have any links to set this up to output to a spreadsheet that would be very helpful. It would look really good in his log book as well.


#9

Have a look at this. It was pretty easy for me to output my data here with a webhook on the Particle Console.

If you need some extra guidance, you can refer to this tutorial. It even contains an example for Thingspeak :grinning: Alternatively, platforms like Losant and Ubidots can be used, which do have a much nicer interface, but for simply outputting some values in a spreadsheet Thingspeak should be fine.


#10

I ended up using @Moors7 suggestion. I used ifttt to store the values in a google spread sheet. It actually was very easy to setup. Didn’t require any changes to the code. I wish I would have known this before I waited all night to discharge the first battery. I will have much better data on this battery.
The hard part is figuring out how to explain how we obtained the data without making it look like dad helped to much.


#11

you could always say that your son asked the right questions in a helping community! :globe_with_meridians::world_map: