I game, I code, I break things
Technical, constructive, fun.
Menu

Jumping Jack Lightning Flash! January 11, 2013

I take a relaxed approach to the project development I do, because I have a full time job and a home and my health to care for. This is why I dedicate at least an hour a week to the game development, to coax it into action. Over the holidays it has been a bit stagnant due to being away from the computer. So it amazes me even more as to the advancement we can do with just a small amount of time.

It's rainy jumpy man!

Here we have a moveable character, with a platform, collision, particles and, though you cannot hear it, background music.

So pleased.

Comments Off on Jumping Jack Lightning Flash!

Self Calibrating Potentiometer December 31, 2012

Update: Code edited for clarity and make sure you read pbrook‘s comment

So I have a dial connected to an Arduino, a potentiometer (pot), it’s the typical item you might use for volume or brightness control, turning it all the way up to 11. Here’s the problem though, you’re reading in the analogue values of the potentiometer and you think it goes from 0 to 1023, possibly higher if it glitches a bit (though this isĀ dependentĀ on how it is handled in your code).

However, what you’re controlling, only goes from 0 to 10, maybe. What do you do?

Well one idea, is that you convert it to a percentage before making the comparison, I think this is effectively ‘normalising‘ the values – statistically speaking. This is really useful. So in this code I’m doing two things, one is that I am normalising the values but the other is that I am calibrating the values that I am reading.

Why should I be calibrating? Well, this code is then portable (the mathematics could apply to anything similar, as I see it), not only that but if the ‘pot’ needs replacing, then if it is placed in the ‘maximum’ value position for a while, then it’ll be accurate (well enough so).

#include "math.h"

int sensorPin = A0;
int maxVal = 0;
float sensorValue = 0.0f;
float percentile = 0.0f;
float temp = 0.0f;

void setup() {
 Serial.begin(9600);
}

void loop() {
 sensorValue = analogRead(sensorPin);
 percentile = (sensorValue / maxVal) * 100;
 temp = fmod(percentile,10.0);
 if (temp > 0.5)
 {
     percentile += 0.5;
 }
 if (percentile > 100.4)
 {
     maxVal++;
 }
 else
 {
     Serial.println((int)percentile);
 }
}

The code utilises the C++ Math library, and rounds the value from the pot up (I think, I should probably double check it) using the function ‘fmod‘. If the pot is set at the maximum it is capable of being at, it will increase the ‘maxVal’ variable to work out the correct 100% value to calculate with.

If anyone has feedback or corrections on this, please comment!

Comments Off on Self Calibrating Potentiometer
Categories: arduino code