7. Input & Output device¶
Tasks¶
1- add a sensor to a microcontroller and read initialize 2- add an output device 3- add an input device
Outputs¶
connecting a rotatry to Adafruit sense¶
HW-040 Rotary Encoder was connected to Adafruit sense using the following code
/*
AnalogReadSerial
Reads an analog input on pin 0, prints the result to the Serial Monitor.
Graphical representation is available using Serial Plotter (Tools > Serial Plotter menu).
Attach the center pin of a potentiometer to pin A0, and the outside pins to +5V and ground.
This example code is in the public domain.
https://www.arduino.cc/en/Tutorial/BuiltInExamples/AnalogReadSerial
*/
// the setup routine runs once when you press reset:
void setup() {
// initialize serial communication at 9600 bits per second:
Serial.begin(9600);
}
// the loop routine runs over and over again forever:
void loop() {
// read the input on analog pin 0:
int sensorValue = analogRead(A0);
// print out the value you read:
Serial.println(sensorValue);
delay(1); // delay in between reads for stability
}
Rotary - Click¶
the wire connections between the mini controller and the senser connected GND,a plus and a click, so that everytime it gets clicked, a serial monitor would read the changes on Arduino IDE
Rotary - Switch command¶
the wire connections between the mini controller and the senser connected GND,a plus and a switch, so that everytime it gets rotated, a serial monitor would read the changes on Arduino IDE
LED¶
#include "Adafruit_APDS9960.h"
#include <Adafruit_NeoPixel.h>
#include <Stepper.h>
#define PIN 11
Adafruit_APDS9960 apds;
Adafruit_NeoPixel strip = Adafruit_NeoPixel(17, PIN, NEO_GRB + NEO_KHZ800);
const int stepsPerRevolution = 4056;
Stepper myStepper = Stepper(stepsPerRevolution, 6, 10, 5, 9);
void setup() {
Serial.begin(115200);
strip.begin();
strip.show();
if(!apds.begin()){
Serial.println("failed to initialize device! Please check your wiring.");
}
else Serial.println("Device initialized!");
apds.enableProximity(true);
apds.enableGesture(true);
}
void loop() {
uint8_t gesture = apds.readGesture();
if(gesture == APDS9960_LEFT){
Serial.println("<");
myStepper.setSpeed(0);
myStepper.step(stepsPerRevolution);
darken5();
}
else if(gesture == APDS9960_RIGHT) {
Serial.println(">");
myStepper.setSpeed(1);
myStepper.step(stepsPerRevolution);
brighten();
darken();
brighten1();
darken1();
brighten2();
darken2();
brighten3();
darken3();
brighten4();
darken4();
}
}
void brighten() {
uint16_t i, j;
for (j = 10; j < 255; j++) {
strip.setBrightness(j);
for (i = 0; i < strip.numPixels(); i++) {
strip.setPixelColor(i, 0, 0, 255);
}
strip.show();
delay(5);
}
delay(1500);
}
// 0 to 255 PURPLE
void brighten1() {
uint16_t i, j;
for (j = 10; j < 200; j++) {
strip.setBrightness(j);
for (i = 0; i < strip.numPixels(); i++) {
strip.setPixelColor(i, 160, 32, 240);
}
strip.show();
delay(5);
}
delay(1500);
}
// 255 to 0 blue
void darken() {
// Serial.begin(9600);
uint16_t i, j;
for (j = 200; j > 10; j--) {
strip.setBrightness(j);
for (i = 0; i < strip.numPixels(); i++) {
strip.setPixelColor(i, 0, 0, 255);
}
strip.show();
delay(5);
// Serial.println(j);
}
delay(1500);
}
// 255 to 0 PURPLE
void darken1() {
// Serial.begin(9600);
uint16_t i, j;
for (j = 200; j > 10; j--) {
strip.setBrightness(j);
for (i = 0; i < strip.numPixels(); i++) {
strip.setPixelColor(i, 160, 32, 240);
}
strip.show();
delay(5);
// Serial.println(j);
}
delay(1500);
}
// 0 to 255 PINK
void brighten2() {
uint16_t i, j;
for (j = 10; j < 200; j++) {
strip.setBrightness(j);
for (i = 0; i < strip.numPixels(); i++) {
strip.setPixelColor(i, 255, 0, 128);
}
strip.show();
delay(5);
}
delay(1500);
}
// 255 to 0 PINK
void darken2() {
// Serial.begin(9600);
uint16_t i, j;
for (j = 200; j > 10; j--) {
strip.setBrightness(j);
for (i = 0; i < strip.numPixels(); i++) {
strip.setPixelColor(i, 255, 0, 128);
}
strip.show();
delay(5);
// Serial.println(j);
}
delay(1500);
}
// 0 to 255 RED
void brighten3() {
uint16_t i, j;
for (j = 10; j < 200; j++) {
strip.setBrightness(j);
for (i = 0; i < strip.numPixels(); i++) {
strip.setPixelColor(i, 255, 0, 0);
}
strip.show();
delay(5);
}
delay(1500);
}
// 255 to 0 RED
void darken3() {
// Serial.begin(9600);
uint16_t i, j;
for (j = 200; j > 10; j--) {
strip.setBrightness(j);
for (i = 0; i < strip.numPixels(); i++) {
strip.setPixelColor(i, 255, 0, 0);
}
strip.show();
delay(5);
// Serial.println(j);
}
delay(1500);
}
// 0 to 255 ORANGE
void brighten4() {
uint16_t i, j;
for (j = 10; j < 200; j++) {
strip.setBrightness(j);
for (i = 0; i < strip.numPixels(); i++) {
strip.setPixelColor(i, 255, 165, 0);
}
strip.show();
delay(5);
}
delay(1500);
}
// 255 to 0 ORANGE
void darken4() {
// Serial.begin(9600);
uint16_t i, j;
for (j = 200; j > 10; j--) {
strip.setBrightness(j);
for (i = 0; i < strip.numPixels(); i++) {
strip.setPixelColor(i, 255, 165, 0);
}
strip.show();
delay(5);
// Serial.println(j);
}
delay(1500);
}
void darken5() {
// Serial.begin(9600);
uint16_t i, j;
for (j = 200; j > 0; j--) {
strip.setBrightness(j);
for (i = 0; i < strip.numPixels(); i++) {
strip.setPixelColor(i, 0, 0, 0);
}
strip.show();
delay(5);
// Serial.println(j);
}
}
Stepper¶
step motor 28byj-48 is connected to the Adafruit sense using the following code
//Includes the Arduino Stepper Library
#include <Stepper.h>
// Defines the number of steps per rotation
const int stepsPerRevolution = 4056;
// Creates an instance of stepper class
// Pins entered in sequence IN1-IN3-IN2-IN4 for proper step sequence
Stepper myStepper = Stepper(stepsPerRevolution, 6, 10, 5, 9);
void setup() {
// Nothing to do (Stepper Library sets pins as outputs)
}
void loop() {
// Rotate CW slowly at 5 RPM
myStepper.setSpeed(5);
myStepper.step(stepsPerRevolution);
Input¶
gensture sense¶
/***************************************************************************
This is a library for the APDS9960 digital proximity, ambient light, RGB, and gesture sensor
This sketch puts the sensor in gesture mode and decodes gestures.
To use this, first put your hand close to the sensor to enable gesture mode.
Then move your hand about 6" from the sensor in the up -> down, down -> up,
left -> right, or right -> left direction.
Designed specifically to work with the Adafruit APDS9960 breakout
----> http://www.adafruit.com/products/3595
These sensors use I2C to communicate. The device's I2C address is 0x39
Adafruit invests time and resources providing this open source code,
please support Adafruit andopen-source hardware by purchasing products
from Adafruit!
Written by Dean Miller for Adafruit Industries.
BSD license, all text above must be included in any redistribution
***************************************************************************/
#include "Adafruit_APDS9960.h"
Adafruit_APDS9960 apds;
// the setup function runs once when you press reset or power the board
void setup() {
Serial.begin(115200);
if(!apds.begin()){
Serial.println("failed to initialize device! Please check your wiring.");
}
else Serial.println("Device initialized!");
//gesture mode will be entered once proximity mode senses something close
apds.enableProximity(true);
apds.enableGesture(true);
}
// the loop function runs over and over again forever
void loop() {
//read a gesture from the device
uint8_t gesture = apds.readGesture();
if(gesture == APDS9960_DOWN) Serial.println("down");
if(gesture == APDS9960_UP) Serial.println("up");
if(gesture == APDS9960_LEFT) Serial.println("left");
if(gesture == APDS9960_RIGHT) Serial.println("right");
}
Last update:
July 14, 2024