Códigos Kit USK4 Mega2560

De IFSC
Revisão de 14h23min de 8 de maio de 2019 por imported>Fargoud (→‎Buzzer passivo)
Ir para navegação Ir para pesquisar

Códigos do kit

Maiores informações, circuitos e explicações na apostila

LED RGB

// Define Pins
#define BLUE 3
#define GREEN 5
#define RED 6

void setup()
{
pinMode(RED, OUTPUT);
pinMode(GREEN, OUTPUT);
pinMode(BLUE, OUTPUT);
digitalWrite(RED, HIGH);
digitalWrite(GREEN, LOW);
digitalWrite(BLUE, LOW);
}

// define variables
int redValue;
int greenValue;
int blueValue;

// main loop
void loop()
{
#define delayTime 10 // fading time between colors

redValue = 255; // choose a value between 1 and 255 to change the color.
greenValue = 0;
blueValue = 0;

// this is unnecessary as we've either turned on RED in SETUP
// or in the previous loop ... regardless, this turns RED off
// analogWrite(RED, 0);
// delay(1000);

for(int i = 0; i < 255; i += 1) // fades out red bring green full when i=255
{
redValue -= 1;
greenValue += 1;
// The following was reversed, counting in the wrong directions
// analogWrite(RED, 255 - redValue);
// analogWrite(GREEN, 255 - greenValue);
analogWrite(RED, redValue);
analogWrite(GREEN, greenValue);
delay(delayTime);
}

redValue = 0;
greenValue = 255;
blueValue = 0;

for(int i = 0; i < 255; i += 1) // fades out green bring blue full when i=255
{
greenValue -= 1;
blueValue += 1;
// The following was reversed, counting in the wrong directions
// analogWrite(GREEN, 255 - greenValue);
// analogWrite(BLUE, 255 - blueValue);
analogWrite(GREEN, greenValue);
analogWrite(BLUE, blueValue);
delay(delayTime);
}

redValue = 0;
greenValue = 0;
blueValue = 255;

 for(int i = 0; i < 255; i += 1) // fades out blue bring red full when i=255
 {
 // The following code has been rearranged to match the other two similar sections
 blueValue -= 1;
 redValue += 1;
 // The following was reversed, counting in the wrong directions
 // analogWrite(BLUE, 255 - blueValue);
 // analogWrite(RED, 255 - redValue);
 analogWrite(BLUE, blueValue);
 analogWrite(RED, redValue);
 delay(delayTime);
 }
}

Entradas digitais

int ledPin = 5;
int buttonApin = 9;
int buttonBpin = 8;

byte leds = 0;

void setup() 
{
  pinMode(ledPin, OUTPUT);
  pinMode(buttonApin, INPUT_PULLUP);  
  pinMode(buttonBpin, INPUT_PULLUP);  
}

void loop() 
{
  if (digitalRead(buttonApin) == LOW)
  {
    digitalWrite(ledPin, HIGH);
  }
  if (digitalRead(buttonBpin) == LOW)
  {
    digitalWrite(ledPin, LOW);
  }
}

Buzzer ativo

int buzzer = 12;//the pin of the active buzzer
void setup()
{
  pinMode(buzzer,OUTPUT);//initialize the buzzer pin as an output
}

void loop()
{
  unsigned char i;
  while(1)
 {
  //output an frequency
  for(i=0;i<80;i++)
  {
   digitalWrite(buzzer,HIGH);
   delay(1);//wait for 1ms
   digitalWrite(buzzer,LOW);
   delay(1);//wait for 1ms
   }
   //output another frequency
    for(i=0;i<100;i++)
     {
       digitalWrite(buzzer,HIGH);
       delay(2);//wait for 2ms
       digitalWrite(buzzer,LOW);
       delay(2);//wait for 2ms
     }
 }
} 

Buzzer passivo

Biblioteca: pitches.h

#include "pitches.h"
 
// notes in the melody:
int melody[] = {
 NOTE_C5, NOTE_D5, NOTE_E5, NOTE_F5, NOTE_G5, NOTE_A5, NOTE_B5, NOTE_C6};
int duration = 500;  // 500 miliseconds
 
void setup() {
 
}
 
void loop() {  
 for (int thisNote = 0; thisNote < 8; thisNote++) {
   // pin8 output the voice, every scale is 0.5 sencond
   tone(8, melody[thisNote], duration);
     
   // Output the voice after several minutes
   delay(1000);
 }
   
 // restart after two seconds 
 delay(2000);
}