First Example

From Pinguino
Revision as of 17:05, 18 December 2014 by Skc (talk | contribs) (Basic english)
Jump to: navigation, search


Example for functions: pinMode digitalWrite delay toggle

Cycles a LED connected to pin 0 with a 1 second cycle - 500 ms on and 500 ms off.

Pinguino programs must have at least asetup function, which is run once when the board is powered up or after a reset, and a loop function, which, you guessed it, loops forever and is run when the setup function exit

// first test with Pinguino

void setup(void)
{
        pinMode(0,OUTPUT); //Set pin 0 as an output
}

void loop(void)
{
        digitalWrite(0,HIGH); // Turn LED on
        delay(500); // Wait for 500 milliseconds
        digitalWrite(0,LOW); // Turn LED off
        delay(500);
}

This can be simplified making use of the Pinguino's toggle function - and itmakes code shorter and more readable:


// second test with Pinguino

void setup(void)
{
        pinMode(0,OUTPUT); //Set pin 0 as an output
        digitalWrite(0,LOW); // Turn LED off
}

void loop(void)
{
        delay(500); // Wait for 500 milliseconds
        toggle(0); // Toggle the LEDs state       
}