Could someone check effeciency of this sketch?

I’m starting on a robot, and the first things I wanted to get out of the way are making it able to record distance from a object via a ultrasonic sensor and if the distance suddenly drops to or below 0 inches or Centimeters, it throws an error. Secondly, I want to be able to record the temperature inside the robot chassis continuously to make sure everything will run smoothly.

Edit: I forgot that these forums used bbcode, so I added in the code tag.

#define THERMISTORPIN A0
#define THERMISTORNOMINAL 10000
#define TEMPERATURENOMINAL 25
#define NUMSAMPLES 5
#define BCOEFFICIENT 3950
#define SERIESRESISTOR 10000

int samples[NUMSAMPLES];

//LED Pin
int LedPin1 = 7;
int LedPin2 = 8;

//Ultrasonic Sensor Pins
int trigPin = 11;    //Trig Pin
int echoPin = 12;    //Echo Pin
long duration, cm, inches;

void setup(void)
{
	Serial.begin(9600);
	analogReference(EXTERNAL);

	pinMode(trigPin, OUTPUT);
	pinMode(echoPin, INPUT);
	analogReference(EXTERNAL);

}

void loop(void)
{
	uint8_t i;
	float average;


	for (i = 0; i < NUMSAMPLES; i++)
	{
		samples[i] = analogRead(THERMISTORPIN);
		delay(10);
	}


	average = 0;
	for (i = 0; i < NUMSAMPLES; i++)
	{
		average += samples[i];
	}
	average /= NUMSAMPLES;

	/*Serial.print("Avg analog reading ");
	Serial.println(average);*/


	average = 1023 / average - 1;
	average = SERIESRESISTOR / average;


	float steinhart;
	steinhart = average / THERMISTORNOMINAL;     // (R/Ro)
	steinhart = log(steinhart);                  // ln(R/Ro)
	steinhart /= BCOEFFICIENT;                   // 1/B * ln(R/Ro)
	steinhart += 1.0 / (TEMPERATURENOMINAL + 273.15); // + (1/To)
	steinhart = 1.0 / steinhart;                 // Invert
	steinhart -= 273.15;                         // convert to C



	digitalWrite(trigPin, LOW);
	delayMicroseconds(5);
	digitalWrite(trigPin, HIGH);
	delayMicroseconds(10);
	digitalWrite(trigPin, LOW);

	pinMode(echoPin, INPUT);
	duration = pulseIn(echoPin, HIGH);

	cm = (duration / 2) / 29.1;
	inches = (duration / 2) / 74;
	Serial.print("Thermistor resistance ");
	Serial.println(average);
	Serial.print("Temperature: ");
	Serial.print('\n');
	Serial.print(steinhart);
	Serial.println(" degrees C");

	//convert to Fahrenheit
	float steinhart1 = (steinhart * 9.0 / 5.0) + 32.0;
	Serial.print(steinhart1);
	Serial.println(" degrees F");

	Serial.print("Distance in inches:  ");
	Serial.print(inches);
	Serial.print("  Distance in cm:  ");
	Serial.print(cm);
	Serial.println();
	if (inches <= 0);
	{
		digitalWrite(LedPin1, HIGH);
		digitalWrite(LedPin2, LOW);
	}
	if (inches >= 1);
	{
		digitalWrite(LedPin1, LOW);
		digitalWrite(LedPin2, HIGH);
	}
	delay(2000);
}
  1. Always post code in code tags

  2. If this code is supposed to deal with a range finder why is it doing temp readings?

3 Never use delay!

Mark

It's doing temp readings, as it's going into a robot that is most likely going to be prone to overheating, and I want to protect the delicate hardware going into it. What can I do to improve though?

Please read the "how to use the forum" and "Read this before posting a programming question" stickies.

I see a couple things at first look. How many pins does your Arduino have? More than 255? If not more than 255, why use int for pin numbers? Use byte and save some SRAM. Will the pin numbers change at runtime? if not, make them const byte and save more SRAM by putting them in flash. Efficient programs do not use delay() except where necessary. DelayMicroseconds is OK where it is used, but I would replace the delay(2000) with the blink without delay method. That 2 seconds might be needed when your code grows. Your robot is effectively dead to the world during the 2 seconds.

groundfungus: Please read the "how to use the forum" and "Read this before posting a programming question" stickies.

I see a couple things at first look. How many pins does your Arduino have? More than 255? If not more than 255, why use int for pin numbers? Use byte and save some SRAM. Will the pin numbers change at runtime? if not, make them const byte and save more SRAM by putting them in flash. Efficient programs do not use delay() except where necessary. DelayMicroseconds is OK where it is used, but I would replace the delay(2000) with the blink without delay method. That 2 seconds might be needed when your code grows. Your robot is effectively dead to the world during the 2 seconds.

The pin numbers will most likely change, depending on which motor driver I will be using, as most require 3 or more pins. How would I put them in flash? I've never done anything like that before, and I'll look into the blink without delay method, as you're right, the robot is effectively dead to the world during those 2 seconds.

I’m not going to study your code unless you post it here.

…R

Robin2: I'm not going to study your code unless you post it here.

...R

https://ghostbin.com/paste/cffa7

Never modify a post after it it has been replied to! Removing your code from your first post is NOT the same as posting it in code tags, if you want people here to read your code NEVER post it on shitbin or the like!

Checking the internal temp is stupid the parts are not delicate but they will burn out long before that temp sensor sees a problem.

Cut the most stupid thing is

distance from a object via a ultrasonic sensor and if the distance suddenly drops to or below 0 inches or Centimeters

If the distance to an object is 0 you have hit it!

And the distance can't be negative!

Mark

For some reason, I have had a negative distance reading before. And I'd like to know how hot the motor drivers are running atleast.

Have you tested the sketch?

aarg:
Have you tested the sketch?

Yes, and it works nicely.
Although, I wish I knew how to simply update the values in serial monitor, instead of printing repeatedly.
I know it involves newvalue != oldvalue, or something like that.

The first thing I see is the use of defines for items that should really be constant variables. Defines will generally work but may result in type conflicts while constants are clearly typed. Your pin variable assignments should also be constants.

Jimmy60: The first thing I see is the use of defines for items that should really be constant variables. Defines will generally work but may result in type conflicts while constants are clearly typed. Your pin variable assignments should also be constants.

Alright, thanks. I'll work on that.

ArduinoDogs: https://ghostbin.com/paste/cffa7

By 'here' I meant on this website.

And I suspect you knew that.

...R

Robin2: By 'here' I meant on this website.

And I suspect you knew that.

...R

Yea, sorry. I forgot the site used bbcode for the forums. Also, I am re-writing this script from scratch bases of your thread, and it should be TONS more effecient. It's a really nice guide. Also, is there a better method for Arduino Multi-tasking then this that'd possibly be easier, or improve effeciency? https://www.arduino.cc/en/Tutorial/MultipleBlinks

Have you looked at Several Things at a Time - it is an extended example of the Blink Without Delay example.

...R

Robin2: Have you looked at Several Things at a Time - it is an extended example of the Blink Without Delay example.

...R

No I haven't, I'll look into that, thanks! I'll also be sure to save it so I can look at it offline when needed.

groundfungus: If not more than 255, why use int for pin numbers? Use byte and save some SRAM.

Because pin numbers are ints in Arduino. Hint: sometimes it needs a negative number to represent an invalid pin.

Bytes would be upgraded to ints every time you do a digitalRead() or digitalWrite(). You have a choice of saving processor cycles or memory.