LED program in C++

Im running into some problems trying to write a program which takes accelerometer data and makes LEDs light up. There are 3 LED's, 1,2,3, and when the accelerometer gives a certain value, say 5, no LED's should come on, If the value is less than 5 the 1st led should light up. Then when the acceleromter goes back to its reference point, 5 in this case, no LED is on. When the value is greater than 5 the 3rd LED should turn on. Any suggestions how to start a C++ code to do this?
1
2
3
4
5
6
int main()
{


return 0;
};


Last edited on
1
2
3
4
5
6
7
8
9
while (Accelero = GetPosition()
{
   if (Accelero < 5)
    LightFirstLight();

   if (Accelero > 5)
    LightThirdLight();

}
Last edited on
Thank you. But how do you get tell the program to light a LED like the first light or third light. Do you have to write a command in the beginning to tell it so? Im sorry, im just really bad at programming and i am trying to teach myself and learn.
Depends on the hardware.
closed account (D80DSL3A)
In a C++ program, turning a light on or off simply amounts to assigning a value to some bool variable representing the lamp state.
Ex;
1
2
3
4
5
6
7
8
9
10
11
static bool LED1 = false, LED2 = false, LED3 = false;// all lamps initially off

while ( Accelero = GetPosition() )
{
   if (Accelero < 5)    
    LED1 = true;
   else if (Accelero > 5)
    LED2 = true;
   else
      LED1 = LED2 = false;
}

You could display the values of LED1, LED2 and LED3 to the console, save it in a file, etc...

To turn an actual physical LED on and off requires that you are programming a microcontroller of some kind.

The timing on this thread is amazing. I just got my Arduino UNO 4 days ago and have been tinkering with it furiously ever since!
The command to turn an LED on or off in a program for the arduino would be:
digitalWrite( 2, HIGH );// turn the LED connected to output pin 2 ON
or
digitalWrite( 2, LOW );// turn the LED connected to output pin 2 OFF

EDIT: corrected arduino model fromDuo
Last edited on
Yes I am using a micro controller also. The micro controller has to be programmed in C++. It's similar to the arduino UNO. I'm still trying to tinker with it. Thanks for the help guys. Every little bit is a ton of help for me.
Last edited on
closed account (D80DSL3A)
I'm sorry if the manufacturer for your board doesn't supply software and tutorials for you.
Do they not have a web site?
No wiki, blogs or forums there?

This is how I am learning to use my UNO.
It's more of a new company I'm guessing. A knock off arduino. Most of their documentation is in German or something. I've downloaded te software and everything so I can compile and upload code to the board.
Last edited on
Topic archived. No new replies allowed.