This document discusses using Nodebot to control an Arduino board with JavaScript. It introduces the IDEs and libraries needed like Arduino, Sublime Text, Stino plugin and Node.js. Examples shown include making an LED blink, creating a traffic light, playing music with a piezo buzzer, reading a push button, potentiometer, and temperature sensor. The goal is to explore different ways to control the Arduino using tools that increase productivity and leverage what Arduino provides through JavaScript and electronics projects.
3. @desisant
Agenda
•
IDE Sublime + Stino
•
IDE Arduino
•
Instalar nodejs
•
Download john-Five
Desenvolvimento:
100% prática:
•
Javascript
•
Eletrônica
•
Analógico
•
Digital
Referência: node-ardx.org
4. @desisant
Expectativa
Explorar meios diferentes para controlar o arduino
Utilizando ferramentas que aumentam a nossa produtividade
Extrair o melhor que o arduino proporciona
Alavancar aprendizado com JS + arduino para desenvolver projetos
Javascript é meio e não fm, não é ofcina de javascript
Sim
Não
Não se preocupe, haverá suporte para todos fcarem na mesma página
5. @desisant
Arduino is an open-source electronics
platform based on easy-to-use
hardware and software.
It's intended for anyone
making interactive projects.
By Massimo Banzi
11. @desisant
var five = require("johnny-five");
var board, leds=[], ledPins = [2,3,4,5,6,7,8,9];
board = new five.Board();
board.on("ready", function() {
// initialize LEDs using a for loop
for (var i = 0; i < ledPins.length; i++){
var myLed = new five.Led(ledPins[i]);
leds.push(myLed);
}
function allOn(){
for (var i = 0; i < leds.length; i++) {
leds[i].on();
}
}
function allOff(){
for (var i = 0; i < leds.length; i++) {
leds[i].off();
}
}
function oneAfterAnother() {
var delay = 1;
board.counter = 0;
for (var i = 0; i < leds.length; i++) {
var led = leds[i];
board.wait(delay,function(){
console.log(this.counter + " on");
leds[this.counter].on();
});
board.wait(delay + 200,function(){
console.log(this.counter + " off");
leds[this.counter].off();
this.counter = (this.counter + 1) % leds.length;
});
delay += 500;
}
}
// allOn();
// board.wait(1000,allOff);
oneAfterAnother();
semaforo.js
13. @desisant
var five = require("johnny-five");
five.Board().on("ready", function() {
var piezo = new five.Piezo(9);
var val = 0;
this.loop(200, function() {
if (val ^= 1) {
// Play note a4 for 1/5 second
piezo.frequency(five.Piezo.Notes["a4"], 200);
}
});
});
Music.js
14. @desisant
• 2 pin header x 4
• 5mm LED
• Pushbutton x 2
• 330 ohm resistor (orange-orange-brown)
• 10k Ohm Resistor (brown-black-orange) x 2
• jumper wires
Push Button
15. @desisant
var five = require("johnny-five"),
onButton, offButton, led;
five.Board().on("ready", function() {
onButton = new five.Button(2);
led = new five.Led(13);
onButton.on("down", function(value){
led.on();
});
});
Button.js
17. @desisant
var five = require("johnny-five"),
board, myPotentiometer;
board = new five.Board();
board.on("ready", function() {
myPotentiometer = new five.Sensor({
pin: "A0",
freq: 250
});
myLed = new five.Led(9);
myPotentiometer.on("read", function() {
var rawValue = this.raw;
myLed.brightness(Math.floor(rawValue / 4));
});
});
Potenciometro.js
18. @desisant
• 2 pin header x 4
• TMP36 Temperature sensor
• jumper wires
Temperatura
19. @desisant
var five = require("johnny-five");
five.Board().on("ready", function(){
var tempSensor = new five.Sensor("A0");
tempSensor.on("read", function(err, value){
var cel = (100 * (value / 1000) - 50).toFixed(2);
console.log("temp is " + cel);
});
});
Temperatura.js