Difference between revisions of "First Example"

From Pinguino
Jump to: navigation, search
m (Basic english)
m
 
Line 5: Line 5:
 
Cycles a LED connected to pin 0 with a 1 second cycle - 500 ms on and 500 ms off.
 
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 a''setup'' 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
+
Pinguino programs must have at least a ''setup'' 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
  
 
<source>
 
<source>

Latest revision as of 16:06, 18 December 2014


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 a setup 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       
}