- Basic led blink
- int led_red = 0;
- int led_yellow = 1;
- int led_green = 2;
- void setup() {
- pinMode(led_red, OUTPUT);
- pinMode(led_yellow, OUTPUT);
- pinMode(led_green, OUTPUT);
- }
- void loop() {
- digitalWrite(led_red, LOW);
- digitalWrite(led_yellow, LOW);
- digitalWrite(led_green, HIGH);
- delay(2000);
- digitalWrite(led_red, LOW);
- digitalWrite(led_yellow, HIGH);
- digitalWrite(led_green, LOW);
- delay(1000); // espera 1 segundo
- digitalWrite(led_red, HIGH);
- digitalWrite(led_yellow, LOW);
- digitalWrite(led_green, LOW);
- delay(3000);
- }
- Push button
- const int buttonPin = 2; // the number of the pushbutton pin
- const int ledPin = 13; // the number of the LED pin
- // variables will change:
- int buttonState = 0; // variable for reading the pushbutton status
- void setup() {
- // initialize the LED pin as an output:
- pinMode(ledPin, OUTPUT);
- // initialize the pushbutton pin as an input:
- pinMode(buttonPin, INPUT);
- }
- void loop() {
- // read the state of the pushbutton value:
- buttonState = digitalRead(buttonPin);
- // check if the pushbutton is pressed. If it is, the buttonState is HIGH:
- if (buttonState == HIGH) {
- // turn LED on:
- digitalWrite(ledPin, HIGH);
- } else {
- // turn LED off:
- digitalWrite(ledPin, LOW);
- }
- }
- LED blink with POT
- #define LED_PIN 11
- #define POTENTIOMETER_PIN A1
- void setup()
- {
- pinMode(LED_PIN, OUTPUT);
- }
- void loop()
- {
- int potentiometerValue = analogRead(POTENTIOMETER_PIN);
- int brightness = potentiometerValue / 4;
- analogWrite(LED_PIN, brightness);
- }
- Dist using ultrasonic sensor & objects
- int trigger = 12; //KUNING TRIG
- int echo = 13; //PIN 13 ECHO
- int led = 8;
- long duration = 0;
- int cm = 0;
- int inch = 0;
- void setup() {
- // put your setup code here, to run once:
- Serial.begin(9600);
- pinMode(trigger, OUTPUT);
- pinMode(echo, INPUT);
- pinMode(led, OUTPUT);
- }
- void loop() {
- // put your main code here, to run repeatedly:
- digitalWrite(trigger, LOW);
- digitalWrite(trigger, HIGH);
- digitalWrite(trigger, LOW);
- duration = pulseIn(echo, HIGH);
- cm = duration*0.034/2;
- inch = duration*0.0133/2;
- if (inch < 50){
- digitalWrite(led, HIGH);
- } else {
- digitalWrite(led, LOW);
- }
- if (inch < 100 ) {
- Serial.print("Inches: ");
- Serial.println(inch);
- Serial.print("Cm: ");
- Serial.println(cm);
- }
- delay(500);
- }
- 2-way traffic
- // Define pin numbers
- #define trigPin 9
- #define echoPin 10
- #define red1Pin 2
- #define yellow1Pin 3
- #define green1Pin 4
- #define red2Pin 5
- #define yellow2Pin 6
- #define green2Pin 7
- void setup() {
- // Initialize pins
- pinMode(trigPin, OUTPUT);
- pinMode(echoPin, INPUT);
- pinMode(red1Pin, OUTPUT);
- pinMode(yellow1Pin, OUTPUT);
- pinMode(green1Pin, OUTPUT);
- pinMode(red2Pin, OUTPUT);
- pinMode(yellow2Pin, OUTPUT);
- pinMode(green2Pin, OUTPUT);
- }
- void loop() {
- // Measure distance
- long duration, distance;
- digitalWrite(trigPin, LOW);
- delayMicroseconds(2);
- digitalWrite(trigPin, HIGH);
- delayMicroseconds(10);
- digitalWrite(trigPin, LOW);
- duration = pulseIn(echoPin, HIGH);
- distance = (duration * 0.0343) / 2; // Speed of sound is 343 m/s
- // Traffic light control logic
- if (distance < 50) { // Adjust threshold as needed
- // Vehicles detected, switch lights
- digitalWrite(green1Pin, LOW);
- digitalWrite(yellow1Pin, HIGH);
- delay(2000);
- digitalWrite(yellow1Pin, LOW);
- digitalWrite(red1Pin, HIGH);
- digitalWrite(green2Pin, HIGH);
- digitalWrite(red2Pin, LOW);
- delay(5000);
- } else {
- // No vehicles detected, normal light sequence
- digitalWrite(red1Pin, LOW);
- digitalWrite(yellow1Pin, HIGH);
- delay(2000);
- digitalWrite(yellow1Pin, LOW);
- digitalWrite(green1Pin, HIGH);
- digitalWrite(green2Pin, LOW);
- digitalWrite(yellow2Pin, HIGH);
- delay(2000);
- digitalWrite(yellow2Pin, LOW);
- digitalWrite(red2Pin, HIGH);
- delay(5000);
- }
- }
- 4-way traffic light
- int d_red =10;
- int d_yellow =9;
- int d_green =8;
- int r_red =4;
- int r_yellow =3;
- int r_green =2;
- int l_red =13;
- int l_yellow =12;
- int l_green =11;
- int u_red =7;
- int u_yellow =6;
- int u_green =5;
- void setup()
- {
- pinMode(d_red, OUTPUT);
- pinMode(d_yellow, OUTPUT);
- pinMode(d_green, OUTPUT);
- pinMode(r_red, OUTPUT);
- pinMode(r_yellow, OUTPUT);
- pinMode(r_green, OUTPUT);
- pinMode(l_red, OUTPUT);
- pinMode(l_yellow, OUTPUT);
- pinMode(l_green, OUTPUT);
- pinMode(u_red, OUTPUT);
- pinMode(u_yellow, OUTPUT);
- pinMode(u_green, OUTPUT);
- }
- void loop()
- {
- changeLights();
- }
- void changeLights()
- {
- //Start (all yellow)
- digitalWrite(u_red,LOW);
- digitalWrite(d_red,LOW);
- digitalWrite(r_red,LOW);
- digitalWrite(l_green,LOW);
- digitalWrite(u_yellow,HIGH);
- digitalWrite(d_yellow,HIGH);
- digitalWrite(r_yellow,HIGH);
- digitalWrite(l_yellow,HIGH);
- delay(5000);
- //upper lane go
- digitalWrite(u_yellow,LOW);
- digitalWrite(d_yellow,LOW);
- digitalWrite(r_yellow,LOW);
- digitalWrite(l_yellow,LOW);
- digitalWrite(u_green,HIGH);
- digitalWrite(r_red,HIGH);
- digitalWrite(l_red,HIGH);
- digitalWrite(d_red,HIGH);
- delay(10000);
- //ALL YELLOW
- digitalWrite(u_yellow,HIGH);
- digitalWrite(d_yellow,HIGH);
- digitalWrite(r_yellow,HIGH);
- digitalWrite(l_yellow,HIGH);
- digitalWrite(u_green,LOW);
- digitalWrite(r_red,LOW);
- digitalWrite(l_red,LOW);
- digitalWrite(d_red,LOW);
- delay(5000);
- //RIGHT LANE GO
- digitalWrite(u_yellow,LOW);
- digitalWrite(d_yellow,LOW);
- digitalWrite(r_yellow,LOW);
- digitalWrite(l_yellow,LOW);
- digitalWrite(u_red,HIGH);
- digitalWrite(l_red,HIGH);
- digitalWrite(d_red,HIGH);
- digitalWrite(r_green,HIGH);
- delay(10000);
- //ALL YELLOW ON
- digitalWrite(u_yellow,HIGH);
- digitalWrite(d_yellow,HIGH);
- digitalWrite(r_yellow,HIGH);
- digitalWrite(l_yellow,HIGH);
- digitalWrite(u_red,LOW);
- digitalWrite(l_red,LOW);
- digitalWrite(d_red,LOW);
- digitalWrite(r_green,LOW);
- delay(5000);
- //DOWN LANE GO
- digitalWrite(u_yellow,LOW);
- digitalWrite(d_yellow,LOW);
- digitalWrite(r_yellow,LOW);
- digitalWrite(l_yellow,LOW);
- digitalWrite(u_red,HIGH);
- digitalWrite(l_red,HIGH);
- digitalWrite(r_red,HIGH);
- digitalWrite(d_green,HIGH);
- delay(10000);
- //ALL YELLOW
- digitalWrite(u_yellow,HIGH);
- digitalWrite(d_yellow,HIGH);
- digitalWrite(r_yellow,HIGH);
- digitalWrite(l_yellow,HIGH);
- digitalWrite(u_red,LOW);
- digitalWrite(l_red,LOW);
- digitalWrite(r_red,LOW);
- digitalWrite(d_green,LOW);
- delay(5000);
- //LEFT LANE GO
- digitalWrite(u_yellow,LOW);
- digitalWrite(d_yellow,LOW);
- digitalWrite(r_yellow,LOW);
- digitalWrite(l_yellow,LOW);
- digitalWrite(u_red,HIGH);
- digitalWrite(d_red,HIGH);
- digitalWrite(r_red,HIGH);
- digitalWrite(l_green,HIGH);
- delay(10000);
- }
- Obstacle avoidance
- const int trigPin=13;
- const int echoPin=12;
- const int in1=2;
- const int in2=3;
- const int in3=4;
- const int in4=5;
- void setup(){
- pinMode(trigPin, OUTPUT);
- pinMode(echoPin, INPUT);
- pinMode(in1, OUTPUT);
- pinMode(in2, OUTPUT);
- pinMode(in3, OUTPUT);
- pinMode(in4, OUTPUT);
- Serial.begin(9600);
- }
- long duration;
- int distance;
- void loop(){
- digitalWrite(trigPin, LOW);
- delay(2);
- digitalWrite(trigPin, HIGH);
- delay(10);
- digitalWrite(trigPin, LOW);
- duration=pulseIn(echoPin, HIGH);
- distance=duration*0.034/2;
- Serial.println(distance);
- if(distance<50) {
- digitalWrite(in1, HIGH);
- digitalWrite(in2, LOW);
- digitalWrite(in3, LOW);
- digitalWrite(in4, LOW);
- }
- else{
- digitalWrite(in1, HIGH);
- digitalWrite(in2, LOW);
- digitalWrite(in3, HIGH);
- digitalWrite(in4, LOW);
- }
- delay(200);
- }
- Home automation using bluetooth HC-05
- int tx=1;
- int rx=0;
- int r=11;
- int reds=0;
- int bs=0;
- int gs=0;
- int b=12;
- int g=6;
- int ser;
- int relay1=8;
- int relay2=9;
- void setup() {
- // put your setup code here, to run once:
- pinMode(r,OUTPUT);
- pinMode(g,OUTPUT);
- pinMode(b,OUTPUT);
- pinMode(relay1,OUTPUT);
- pinMode(relay2,OUTPUT);
- Serial.begin(9600);
- }
- void loop() {
- // put your main code here, to run repeatedly:
- if(Serial.available()>0)
- {ser=Serial.read();
- Serial.println(ser);
- if(reds== 0 && ser ==255 )
- {
- digitalWrite(r,HIGH);
- digitalWrite(relay1,HIGH );
- Serial.println("BULB 1 ON");
- ser = 0;
- reds = 1;
- }
- if(ser == 255 && reds== 1 )
- {
- digitalWrite(r,LOW);
- digitalWrite(relay1,LOW );
- Serial.println("BULB 1 OFF");
- ser = 0;
- reds = 0;
- }
- if( bs== 0 && ser ==254 )
- {
- digitalWrite(b,HIGH);
- digitalWrite(relay2,HIGH );
- Serial.println("BULB 2 ON");
- ser = 0;
- bs = 1;
- }
- if( bs== 1 && ser ==254)
- {
- digitalWrite(b,LOW);
- digitalWrite(relay2,LOW );
- Serial.println("BULB 2 OFF");
- ser = 0;
- bs = 0;
- }
- if(gs== 0 && ser== 249 )
- {
- digitalWrite(g,HIGH);
- ser = 0;
- gs = 1;
- }
- if(gs== 1 && ser == 249 )
- {
- digitalWrite(g,LOW);
- ser = 0;
- gs = 0;
- }
- }
- }
[text] paste 3
Viewer
*** This page was generated with the meta tag "noindex, nofollow". This happened because you selected this option before saving or the system detected it as spam. This means that this page will never get into the search engines and the search bot will not crawl it. There is nothing to worry about, you can still share it with anyone.
Editor
You can edit this paste and save as new: