void setup() {
// רץ פעם אחת, מיד אחרי ההפעלה או אחרי לחיצה על אתחול
}
void loop() {
// רץ שוב ושוב, בלי סוף, כל עוד יש מתח
}
// Blink - the first experiment. No wiring needed.
// The built-in LED is wired to pin 13 on every Uno board.
int ledPin = 13; // keep the pin number in one place
void setup() {
pinMode(ledPin, OUTPUT); // runs once: this pin is an output
}
void loop() { // runs again and again, forever
digitalWrite(ledPin, HIGH);
delay(500);
digitalWrite(ledPin, LOW);
delay(500);
}
| int ledPin = 13; | |
| pinMode(ledPin, OUTPUT); | |
| digitalWrite(ledPin, HIGH); | |
| delay(500); | |
| digitalWrite(ledPin, LOW); |
// One LED on pin 8, through a series resistor to ground.
// R = (Vcc - Vf) / I -> (5 - 2) / 0.014 = 214 ohm -> use 220
const int LED = 8;
void setup() {
pinMode(LED, OUTPUT);
}
void loop() {
digitalWrite(LED, HIGH);
delay(700);
digitalWrite(LED, LOW);
delay(300);
}
// The naive version. No pull-up, no debounce.
const int BTN = 2;
const int LED = 8;
int count = 0;
int last = HIGH;
void setup() {
pinMode(BTN, INPUT);
pinMode(LED, OUTPUT);
Serial.begin(9600);
}
void loop() {
int raw = digitalRead(BTN);
if (raw == LOW && last == HIGH) {
count = count + 1;
Serial.println(count);
}
last = raw;
digitalWrite(LED, raw == LOW ? HIGH : LOW);
}
// Internal pull-up: the pin idles HIGH, a press pulls it LOW.
// A change is accepted only after it has held for 25 ms.
const int BTN = 2;
const int LED = 8;
int count = 0;
int lastRead = HIGH;
int stable = HIGH;
unsigned long lastEdge = 0;
void setup() {
pinMode(BTN, INPUT_PULLUP);
pinMode(LED, OUTPUT);
Serial.begin(9600);
}
void loop() {
int raw = digitalRead(BTN);
if (raw != lastRead) {
lastRead = raw;
lastEdge = millis();
}
if (millis() - lastEdge > 25 && stable != lastRead) {
stable = lastRead;
if (stable == LOW) {
count = count + 1;
Serial.println(count);
}
}
digitalWrite(LED, stable == LOW ? HIGH : LOW);
}
// PWM fade on pin 9.
// analogWrite takes 0..255 - that is a duty cycle, not a voltage.
const int LED = 9;
void setup() {
pinMode(LED, OUTPUT);
}
void loop() {
for (int d = 0; d <= 255; d = d + 5) { // brighter
analogWrite(LED, d);
delay(20);
}
for (int d = 255; d >= 0; d = d - 5) { // dimmer
analogWrite(LED, d);
delay(20);
}
}
| 0 | 0 % | 0.00 V | |
| 64 | 25 % | 1.25 V | |
| 127 | 50 % | 2.49 V | |
| 191 | 75 % | 3.75 V | |
| 255 | 100 % | 5.00 V |
// A0 reads a voltage divider. The LED on pin 9 follows it.
const int POT = A0;
const int LED = 9;
void setup() {
pinMode(LED, OUTPUT);
Serial.begin(9600);
}
void loop() {
int raw = analogRead(POT); // 0 .. 1023, never volts
float volts = raw * 5.0 / 1023.0; // this is the conversion
int duty = map(raw, 0, 1023, 0, 255); // 10 bits -> 8 bits
analogWrite(LED, duty);
Serial.print("raw=");
Serial.print(raw);
Serial.print(" V=");
Serial.print(volts, 2);
Serial.print(" duty=");
Serial.println(duty);
delay(300);
}
// The board listens to the computer over the same USB cable.
// Type ON, OFF or BLINK in the monitor and press send.
const int LED = 8;
String cmd = "";
void handle(String s) {
if (s == "ON") {
digitalWrite(LED, HIGH);
Serial.println("led is on");
} else if (s == "OFF") {
digitalWrite(LED, LOW);
Serial.println("led is off");
} else if (s == "BLINK") {
for (int i = 0; i < 3; i = i + 1) {
digitalWrite(LED, HIGH);
delay(200);
digitalWrite(LED, LOW);
delay(200);
}
Serial.println("blinked 3 times");
} else {
Serial.print("unknown command: ");
Serial.println(s);
}
}
void setup() {
pinMode(LED, OUTPUT);
Serial.begin(9600);
Serial.println("ready. commands: ON OFF BLINK");
}
void loop() {
while (Serial.available() > 0) {
char c = Serial.read();
if (c == '\n' || c == '\r') { // a whole line arrived
if (cmd.length() > 0) {
handle(cmd);
cmd = "";
}
} else {
cmd = cmd + c; // one character at a time
}
}
}
// A ready-made relay module on pin 7 switches the load.
// The pin drives the module input only - never a coil directly.
const int RELAY = 7;
void setup() {
pinMode(RELAY, OUTPUT);
digitalWrite(RELAY, LOW); // start with the load off
}
void loop() {
digitalWrite(RELAY, HIGH); // contact closes, load is fed
delay(3000);
digitalWrite(RELAY, LOW); // contact opens
delay(3000);
}
| 80 mA | — | ||
| 4.3 mA | ≈ 300 V | ||
| 4.3 mA | ≈ 5.7 V | ||
| 3.5 mA | ≈ 5.7 V |
#include <Servo.h>
// The library builds the 50 Hz pulse train in the background.
// We only write an angle in degrees.
Servo arm;
void setup() {
arm.attach(9); // signal wire on pin 9
arm.write(90); // start in the middle
delay(500);
}
void loop() {
for (int a = 0; a <= 180; a = a + 2) {
arm.write(a);
delay(15); // give the arm time to get there
}
delay(400);
for (int a = 180; a >= 0; a = a - 2) {
arm.write(a);
delay(15);
}
delay(400);
}
#include <SoftwareSerial.h>
// HC-05 module TX -> pin 10 (our RX)
// HC-05 module RX <- pin 11 (our TX)
// Pins 0 and 1 stay free for the USB monitor.
SoftwareSerial BT(10, 11);
const int LED = 9;
String cmd = "";
void apply(String s) {
if (s == "ON") {
analogWrite(LED, 255);
BT.println("on");
} else if (s == "DIM") {
analogWrite(LED, 60);
BT.println("dim");
} else if (s == "OFF") {
analogWrite(LED, 0);
BT.println("off");
} else {
BT.print("unknown: ");
BT.println(s);
}
Serial.print("from phone: ");
Serial.println(s);
}
void setup() {
pinMode(LED, OUTPUT);
Serial.begin(9600); // the cable, for debugging
BT.begin(9600); // the module, for the phone
Serial.println("bridge ready");
}
void loop() {
while (BT.available() > 0) {
char c = BT.read();
if (c == '\n' || c == '\r') {
if (cmd.length() > 0) {
apply(cmd);
cmd = "";
}
} else {
cmd = cmd + c;
}
}
}
| VCC | 5V | |
| GND | GND | |
| TXD | D10 | |
| RXD | D11 |
#include <SoftwareSerial.h>
// Room thermostat: LM35 on A0, fan through an isolated relay module on D7,
// status LED on D8, and a report to the cable and to the phone once a second.
SoftwareSerial BT(10, 11);
const int SENSOR = A0;
const int RELAY = 7;
const int LED = 8;
// Two thresholds, never one. The gap between them is the hysteresis.
const float ON_C = 30.0;
const float OFF_C = 27.0;
bool fan = false;
unsigned long lastReport = 0;
float readCelsius() {
int raw = analogRead(SENSOR);
float volts = raw * 5.0 / 1023.0;
return volts * 100.0; // LM35: 10 mV per degree
}
void setup() {
pinMode(RELAY, OUTPUT);
pinMode(LED, OUTPUT);
digitalWrite(RELAY, LOW);
Serial.begin(9600);
BT.begin(9600);
Serial.println("thermostat ready");
}
void loop() {
float t = readCelsius();
if (fan == false && t >= ON_C) fan = true;
if (fan == true && t <= OFF_C) fan = false;
digitalWrite(RELAY, fan ? HIGH : LOW);
digitalWrite(LED, fan ? HIGH : LOW);
// No delay here. The loop stays free to read the sensor and the module.
if (millis() - lastReport >= 1000) {
lastReport = millis();
Serial.print("T=");
Serial.print(t, 1);
Serial.print(" fan=");
Serial.println(fan ? 1 : 0);
BT.print("T=");
BT.println(t, 1);
}
}