Arduino para 4º de ESO
Ejemplos
Encendido de cuatro ledes
/************************************************* *************************************************
* OPEN-SMART Rich Shield Lesson 1: Run four LED
* You can learn how to turn on and turn off the LED.
*
* The following functions are available:
*
* led.on(uint8_t num);//num can be 1, 2, 3, 4 that is consistent with the LED number
* led.off(uint8_t num);
*
************************************************** *******************************************************/
#include <Wire.h>
#include "RichShieldLED.h"
#define LED1 7
#define LED2 6
#define LED3 5
#define LED4 4
LED led(LED1,LED2,LED3,LED4);
void setup() {
}
void loop() {
for(uint8_t i=1;i < 5; i++)
{
led.on(i);//turn on LED i
delay(500);
led.off(i);//turn off it.
}
}
Zumbador sonando
/************************************************* *************************************************
* OPEN-SMART Rich Shield Lesson 2: Passive buzzer to play tone of Chinese Double Tiger
* You can learn how to use a passive buzzer to play simple songs according to the notation.
*
* The following functions are available:
*
* playTone(long note, long millisecond); //The first parameter note is the frequency of note, unit is HZ;
* //millisecond is the duration of this tone, unit is ms.
*
************************************************** *******************************************************/
#include <Wire.h>
#include "RichShieldPassiveBuzzer.h"
#define PassiveBuzzerPin 3//the SIG pin of the module is connected with D3 of OPEN-SMART UNO R3
//There is no white waterproof sticker on the passive buzzer
PassiveBuzzer buz(PassiveBuzzerPin);
//------------------------------------
//This is a macro definition of bass, midrange, and treble frequencies
#define NOTE_L1 262
#define NOTE_L2 294
#define NOTE_L3 330
#define NOTE_L4 349
#define NOTE_L5 392
#define NOTE_L6 440
#define NOTE_L7 494
#define NOTE_M1 523
#define NOTE_M2 587
#define NOTE_M3 659
#define NOTE_M4 698
#define NOTE_M5 784
#define NOTE_M6 880
#define NOTE_M7 988
#define NOTE_H1 1046
#define NOTE_H2 1175
#define NOTE_H3 1318
#define NOTE_H4 1397
#define NOTE_H5 1568
#define NOTE_H6 1760
#define NOTE_H7 1976
//-------------------------------------------
//musical notation of Two Tigers
const static int doubleTiger[] = {
NOTE_M1,NOTE_M2,NOTE_M3,NOTE_M1,
NOTE_M1,NOTE_M2,NOTE_M3,NOTE_M1,
NOTE_M3,NOTE_M4,NOTE_M5,
NOTE_M3,NOTE_M4,NOTE_M5,
NOTE_M5,NOTE_M6,NOTE_M5,NOTE_M4,
NOTE_M3,NOTE_M1,
NOTE_M5,NOTE_M6,NOTE_M5,NOTE_M4,
NOTE_M3,NOTE_M1,
NOTE_M2,NOTE_L5,
NOTE_M1,0,
NOTE_M2,NOTE_L5,
NOTE_M1,0
};
//-------------------------------------------
////note duration of Two Tigers, 2 is half a beat, 1 is a beat.
const static int tempo[] = {
2,2,2,2,
2,2,2,2,
2,2,1,
2,2,1,
2,2,2,2,
1,1,
2,2,2,2,
1,1,
1,1,
1,1,
1,1,
1,1
};
void setup(void)
{
//sing the tunes
sing();
}
void loop()
{
}
void sing() {
// iterate over the notes of the melody:
int size = sizeof(doubleTiger) / sizeof(int);
for (int thisNote = 0; thisNote < size; thisNote++) {
int noteDuration = 500 / tempo[thisNote];//a beat for 0.5 second
buz.playTone(doubleTiger[thisNote], noteDuration);
// to distinguish the notes, set a minimum time between them.
// the note's duration + 30% seems to work well:
int pauseBetweenNotes = noteDuration * 0.30;
delay(pauseBetweenNotes);
}
}
/*********************************************************************************************************
The end of file
*********************************************************************************************************/
Sensor de luz
/************************************************* *************************************************
* OPEN-SMART Rich Shield Lesson 3: Light Sensor
* You can learn how to know the ambient light is dark or bright. And display illuminance on the digital tube.
*
* The following functions are available:
*
* lightsensor.getRes();//it return the resistance of the light sensor, the darker the ambient light, the larger the resistance.
*
************************************************** ************************************************** *****/
#include <math.h>
#include <Wire.h>
#include "RichShieldLightSensor.h"
#include "RichShieldTM1637.h"
#define CLK 10//CLK of the TM1637 IC connect to D10 of OPEN-SMART UNO R3
#define DIO 11//DIO of the TM1637 IC connect to D11 of OPEN-SMART UNO R3
TM1637 disp(CLK,DIO);
#define LIGHTSENSOR_PIN A2//SIG pin of Rocker Switch module connect to A0 of IO Shield, that is pin A2 of OPEN-SMART UNO R3
LightSensor lightsensor(LIGHTSENSOR_PIN);
void setup() {
Serial.begin(9600); //Set baud rate of serial port to be 9600bps, burn the program and open the serial monitor
//on the upper right corner of Arduino IDE to check the sensor data.
disp.init();//The initialization of the display
}
void loop() {
float Rsensor = lightsensor.getRes();//if Rsensor is larger than 40 KOhm, the ambient light is very dark.
//if Rsensor is smaller than 10 KOhm, the ambient light is bright.
Serial.println("The resistance of the Light sensor is ");
Serial.print(Rsensor,1);
Serial.println(" KOhm");
float lux;
lux = 325*pow(Rsensor,-1.4);
disp.display(lux);//Illuminance
Serial.print("Illuminance is almost "); // may be it is not exactly right
Serial.print(lux,1);
Serial.println(" lux");
delay(1000);
}
/*********************************************************************************************************
The end of file
*********************************************************************************************************/
Mostrar
/************************************************* *************************************************
* OPEN-SMART Rich Shield Lesson 3: Light Sensor
* You can learn how to know the ambient light is dark or bright. And display illuminance on the digital tube.
*
* The following functions are available:
*
* lightsensor.getRes();//it return the resistance of the light sensor, the darker the ambient light, the larger the resistance.
*
************************************************** ************************************************** *****/
#include <math.h>
#include <Wire.h>
#include "RichShieldLightSensor.h"
#include "RichShieldTM1637.h"
#define CLK 10//CLK of the TM1637 IC connect to D10 of OPEN-SMART UNO R3
#define DIO 11//DIO of the TM1637 IC connect to D11 of OPEN-SMART UNO R3
TM1637 disp(CLK,DIO);
#define LIGHTSENSOR_PIN A2//SIG pin of Rocker Switch module connect to A0 of IO Shield, that is pin A2 of OPEN-SMART UNO R3
LightSensor lightsensor(LIGHTSENSOR_PIN);
void setup() {
Serial.begin(9600); //Set baud rate of serial port to be 9600bps, burn the program and open the serial monitor
//on the upper right corner of Arduino IDE to check the sensor data.
disp.init();//The initialization of the display
}
void loop() {
float Rsensor = lightsensor.getRes();//if Rsensor is larger than 40 KOhm, the ambient light is very dark.
//if Rsensor is smaller than 10 KOhm, the ambient light is bright.
Serial.println("The resistance of the Light sensor is ");
Serial.print(Rsensor,1);
Serial.println(" KOhm");
float lux;
lux = 325*pow(Rsensor,-1.4);
disp.display(lux);//Illuminance
Serial.print("Illuminance is almost "); // may be it is not exactly right
Serial.print(lux,1);
Serial.println(" lux");
delay(1000);
}
/*********************************************************************************************************
The end of file
*********************************************************************************************************/
Tecla y display
/************************************************* *************************************************
* OPEN-SMART Rich Shield Lesson 5: Press key and display
* You can learn about detecting keys, digital increase, reduce, clear operation of the digital display
*
* The following functions are available:
*
* key.get(); / / return is key number you pressed, if no pressed then return 0
*
* disp.init(); // initialization
* disp.display(int Decimal); // display range: -999 ~ 9999
************************************************** ************************************************** *****/
#include <Wire.h>
#include "RichShieldKEY.h"
#include "RichShieldTM1637.h"
#define CLK 10//CLK of the TM1637 IC connect to D10 of OPEN-SMART UNO R3
#define DIO 11//DIO of the TM1637 IC connect to D11 of OPEN-SMART UNO R3
TM1637 disp(CLK,DIO);
#define KEY1_PIN 9//
#define KEY2_PIN 8//
Key key(KEY1_PIN,KEY2_PIN);
int count = 0;
void setup()
{
disp.init();//The initialization of the display
disp.display(count);//display counter number
}
void loop()
{
int keynum;
keynum = key.get();
if(keynum == 1) //if you press K1
{
count++;
}
else if(keynum == 2)//if you press K2
{
count--;
}
disp.display(count);
delay(50);//
}
/*********************************************************************************************************
The end of file
*********************************************************************************************************/
Tecla y rebote
/************************************************* ************************************************** ******
* OPEN-SMART Rich Shield Lesson 6: Press key and debounce
* Learn about software debounce method to achieve the number increasing step by step.
*
* The following functions are available:
*
* key.get(); / / return is key number you pressed, if no pressed then return 0
*
* disp.init(); // initialization
* disp.display(int Decimal); // display range: -999 ~ 9999
************************************************** ************************************************** *****/
#include <Wire.h>
#include "RichShieldKEY.h"
#include "RichShieldTM1637.h"
#define CLK 10//CLK of the TM1637 IC connect to D10 of OPEN-SMART UNO R3
#define DIO 11//DIO of the TM1637 IC connect to D11 of OPEN-SMART UNO R3
TM1637 disp(CLK,DIO);
#define KEY1_PIN 9//
#define KEY2_PIN 8//
Key key(KEY1_PIN,KEY2_PIN);
int count = 0;
void setup()
{
disp.init();//The initialization of the display
disp.display(count);//display counter number
}
void loop()
{
int keynum;
keynum = key.get();
if(keynum == 1) //if you press K1
{
delay(10);//delay for 10ms
if(key.get() == 1)//check it again
{
count++;
disp.display(count);
}
while(key.get() == 1);//Wait for the button to be released
}
}
/*********************************************************************************************************
The end of file
*********************************************************************************************************/
Temperatura y display
/************************************************* ************************************************** ******
* OPEN-SMART Rich Shield Lesson 7: Temperature measurement and display
* Through the NTC temperature measurement module to obtain the temperature, and display on the digital tube.
*
* The following functions are available:
* temper.getTemperature(); // get the temperature value, and return float-point number
*
* displayTemperature(int8_t temperature); // display temperature, at the end of the value of the word added C is the degree of Celsius
*
* disp.init(); // initialization
* disp.display(int8_t DispData []); // display the numbers in the array
* // For example, the array is {1,2,3,4}, then show 1234
************************************************** ************************************************** *****/
#include <Wire.h>
#include "RichShieldTM1637.h"
#include "RichShieldNTC.h"
#define NTC_PIN A1 //SIG pin of NTC module connect to A1 of IO Shield, that is pin A1 of OPEN-SMART UNO R3
NTC temper(NTC_PIN);
#define CLK 10//CLK of the TM1637 IC connect to D10 of OPEN-SMART UNO R3
#define DIO 11//DIO of the TM1637 IC connect to D11 of OPEN-SMART UNO R3
TM1637 disp(CLK,DIO);
void setup()
{
disp.init();//The initialization of the display
delay(1000);//
}
void loop()
{
float celsius;
celsius = temper.getTemperature();//get temperature
displayTemperature((int8_t)celsius);//
delay(1000);//delay 1000ms
}
/************************************************* *********************/
/* Function: Display temperature on 4-digit digital tube */
/* Parameter: -int8_t temperature, temperature range is -40 ~ 125 degrees celsius */
/* Return Value: void */
void displayTemperature(int8_t temperature)
{
int8_t temp[4];
if(temperature < 0)
{
temp[0] = INDEX_NEGATIVE_SIGN;
temperature = abs(temperature);
}
else if(temperature < 100)temp[0] = INDEX_BLANK;
else temp[0] = temperature/100;
temperature %= 100;
temp[1] = temperature / 10;
temp[2] = temperature % 10;
temp[3] = 12; //index of 'C' for celsius degree symbol.
disp.display(temp);
}
/*********************************************************************************************************
The end of file
*********************************************************************************************************/
Mostrar el ángulo del potenciómetro
/************************************************* ************************************************** ******
* OPEN-SMART Rich Shield Lesson 8: Knob angle display
* You can learn get the angle of the knob(rotary angle sensor) and display.
*
* The following functions are available:
*
* knob.getAngle();//read the angle of the knob, the range of angle is 0~280 degrees
*
* disp.init(); // initialization
* disp.display(int Decimal); // display range: -999 ~ 9999
************************************************** **************************************************/
#include <Wire.h>
#include "RichShieldTM1637.h"
#include "RichShieldKnob.h"
#define KNOB_PIN A0//SIG pin of Rotary Angle Sensor module connect to A0 of IO Shield, that is pin A0 of OPEN-SMART UNO R3
Knob knob(KNOB_PIN);
#define CLK 10//CLK of the TM1637 IC connect to D10 of OPEN-SMART UNO R3
#define DIO 11//DIO of the TM1637 IC connect to D11 of OPEN-SMART UNO R3
TM1637 disp(CLK,DIO);
void setup()
{
disp.init();//The initialization of the display
}
void loop()
{
int angle;
angle = knob.getAngle();
disp.display(angle);
delay(500);
}
/*********************************************************************************************************
The end of file
*********************************************************************************************************/
Control del brillo con el potenciómetro
/************************************************* ************************************************** ******
* OPEN-SMART Rich Shield Lesson 9: Knob control brightness
* You can learn use the knob(rotary angle sensor) to control the brightness of the display.
*
* The following functions are available:
* knob.getAngle();//read the angle of the knob, the range of angle is 0~280 degrees
* disp.init(); // initialization
* disp.display(int8_t DispData []); // display the numbers in the array
* // For example, the array is {1,2,3,4}, then show 1234
* disp.set(uint8_t brightness); // set the brightness, 0 ~ 7, the greater the value, the higher the brightness,
//the next display takes effect
* disp.point(0); // Turn off the display colon and the next display takes effect
* disp.point(1); // Turn on the display colon and the next display takes effect
************************************************** **************************************************/
#include <Wire.h>
#include "RichShieldTM1637.h"
#include "RichShieldKnob.h"
#define KNOB_PIN A0//SIG pin of Rotary Angle Sensor module connect to A0 of IO Shield, that is pin A0 of OPEN-SMART UNO R3
Knob knob(KNOB_PIN);
#define CLK 10//CLK of the TM1637 IC connect to D10 of OPEN-SMART UNO R3
#define DIO 11//DIO of the TM1637 IC connect to D11 of OPEN-SMART UNO R3
TM1637 disp(CLK,DIO);
void setup()
{
disp.init();//The initialization of the display
disp.point(1);//turn on the clock point;
}
void loop()
{
int angle;
angle = knob.getAngle();
int brightness;
/*The degrees is 0~280, should be converted to be 0~7 to control the*/
/*brightness of display */
brightness = map(angle, 0, FULL_ANGLE, 0, BRIGHTEST);
disp.set(brightness);
int8_t ListDisp[4] = {8,8,8,8};
disp.display(ListDisp);//display array
}
/*********************************************************************************************************
The end of file
*********************************************************************************************************/
Mostrando voltaje en el display
/************************************************* ************************************************** ******
* OPEN-SMART Rich Shield Lesson 10: Measure voltage and Display
* You can learn use the 4-digit tube to display the input voltage value you measure.
*
* The following functions are available:
* voltage.read();//read the voltage and return a floating-point value
* disp.init(); // initialization
* display(double Decimal);// display a floating-point value
************************************************** **************************************************/
#include <Wire.h>
#include "RichShieldVoltageSensor.h"
#include "RichShieldTM1637.h"
#define CLK 10//CLK of the TM1637 IC connect to D10 of OPEN-SMART UNO R3
#define DIO 11//DIO of the TM1637 IC connect to D11 of OPEN-SMART UNO R3
TM1637 disp(CLK,DIO);
#define VOL_SENOR A3//The SIG pin connects A3 pin of OPEN-SMART UNO R3
VoltageSensor voltage(VOL_SENOR);
void setup(){
disp.init();//
}
void loop(){
float vol;
vol = voltage.read();
disp.display(vol);
delay(1000);
}
Obra publicada con Licencia Creative Commons Reconocimiento No comercial Compartir igual 4.0