Friday, August 19, 2011

Arduino Solar Tracker Using LDRs

I’ve always wanted to make a solar tracker. Watched a few youtube videos and decided to make one myself.

 

The circuitry:

Capture19-8-2011-11.23.42 AM

 

And the sketch itself. I’ve uploaded it to pastebin.com for easy reference.

http://pastebin.com/zPDkng6e

I tried to make a horizontal movement function and a vertical movement function, and call them in the loop function but it keeps failing to call them. Decided to leave the if- statements in the open like that.

Tuesday, August 2, 2011

Help needed to troubleshoot [solved]

This is the first project that I sortof created myself, combining what I’ve learnt so far about Arduino sketching and circuitry.

I am trying to make an on/off switch thingy. One click on the button should turn the LED on (and remain off), another click should turn it off (and remain off). However The code does not seem to stick, and random misconducts appear to happen. Sometimes the first few clicks would function as expected, then the behavior turns erratic. Other times even the first few click also does not turn the LED on (it blinks, or remains on even after a second click). I hope anyone with knowledge in the subject can help me out.

As you can see in this scenario the first few clicks function as expected, then it just doesn’t work well until after a few more clicks later.

The following is the sketch:

int ledPin = 12;
int buttonPin = 2;
int loopcount = 0;

void setup()
{
pinMode(ledPin, OUTPUT);
pinMode(buttonPin, INPUT);
digitalWrite(ledPin, LOW);
}

void loop()
{
int buttonState = digitalRead(buttonPin);
if (buttonState == HIGH)
{
changeState();
}
}

void changeState()
{
loopcount++;
if(loopcount%2 != 0)
{
digitalWrite(ledPin, HIGH);
}
if(loopcount%2 == 0)
{
digitalWrite(ledPin, LOW);
}
}

My concept of making it a switch is based on counting the number of times the button has been pressed( loo[count++ ), calculating whether the n-th time is an even or odd number (loopcount%2 == 0 ), and assigning that when it is odd numbered to turn the LED on (if statement).

The circuitry is exactly the same as the interactive traffic light one, just that I pulled off all the excess LEDs and wires and resistors, leaving the button and the sole LED. This meant that the circuit is valid and closed.

UPDATE: having watched this video, I realise there's much more to think about when it comes to playing with arduino (or any electrical stuff for that matter).

The fixed code:

int switchPin = 8;

int ledPin = 13;

boolean lastButton = LOW;

boolean ledOn = false;

boolean currentButton = LOW;



void setup()

{

pinMode(switchPin, INPUT);

pinMode(ledPin, OUTPUT);

}


boolean debounce(boolean last)

{

boolean current = digitalRead(switchPin);

if (last != current)

{

delay(5);

current = digitalRead(switchPin);

}

return current;

}


void loop()

{

currentButton = debounce(lastButton);

if (lastButton == LOW && currentButton == HIGH)

{

ledOn = !ledOn;

}

lastButton = currentButton;

digitalWrite(ledPin, ledOn);

}


I do not fully understand the boolean parts of the code, so might need help there.

Glad that I finally made it thru making an on/off switch! Though not on my own efforts as initially hoped for.