marți, 26 noiembrie 2013

Comanda periferice de la calculator pe portul serial folosind Arduino

   O aplicatie de efect este aceea in care se comanda un motor, un ventilator, un proiector, o usa de acces prin introducerea unor cifre sau cuvinte de la tastatura calculatorului.
   Aceasta se poate baza pe comanda Serial.read() a limbajului placii de dezvoltare Arduino.
   Pe net se gasesc cateva articole despre acest tip de comanda, din care amintesc:
USING ARDUINO TO CONTROL A LED USING THE SERIAL PORT
   Am incercat si eu sa fac asta, rezultandu-mi filmuletul control a LED using Arduino thru keyboard, apoi control a LED using Arduino thru keyboard (II)
   Niste poze din timpul experimentelor:
   Un exemplu de skech simplu este: 
// sketch write by niq_ro from http://www.tehnic.go.ro
// and http://nicuflorica.blogspot.com/
// version 1.0

int motorPin = 13;
void setup()
{
pinMode(motorPin, OUTPUT);
digitalWrite(motorPin, LOW);
Serial.begin(9600);
Serial.println("Apasa 1 pentru a aprinde LED-ul si 2 sa-l stingi!!!");
}

void loop()
{
if (Serial.available())
{
int ch = Serial.read();
Serial.print(ch);

if (ch==49)
{
digitalWrite(motorPin, HIGH);
Serial.println(" Aprind LED-ul");
}
if (ch==50)
{
digitalWrite(motorPin, LOW);
Serial.println(" Sting LED-ul");
}
Serial.println("Apasa 1 pentru a aprinde LED-ul si 2 sa-l stingi!!!");
}
}

   Am modificat sketch-ul si am primit o aplicatie, cu care pot comanda pe langa LED-ul de pe placa (cel de la pinul D13) inca 3 LED-uri, dintr-ul LED multicolor, cum am prezentat in filmuletele control a few LEDs with Arduino and own software (I), respectiv control a few LEDs with Arduino and own software (II)
   Led-urile se pot inlocui cu optocuploare, relee, etc si se pot comanda si echipamente alimentate la retea... dar asta alta data...

vineri, 22 noiembrie 2013

Arduino, un servomotor si o tastatura cu 12 butoane

   In prezentul articol o sa prezint un modul de acces cu parola, cu inchidere manuala sau temporizata, in care elementul de executie e un servomotor.
   Acest articol se bazeaza pe informatiile, schemele si programioarele (sketch-urile) din articolele anterioare:
Arduino si o tastatura cu 12 butoane
Arduino si o tastatura cu 12 butoane (II)
Arduino si un servomotor
   In prima faza am conectat tastatura, afisajul si servomotorul:
schema de conectare fiind:
   Am realizat 2 filmulete:
Arduino - door lock with code - ver.4.0cu timp de acces nelimitat, inchidere prin apasarea tastei '#':
- Arduino - door lock with code - ver.4.1, cu timp de acces limitat la 5 secunde:
   Sketch-ul pentru prima versiune este:
// original schematic and schetch from http://www.arduinoevilgenius.com/
// adapted schematic by niq_ro ( http://www.tehnic.go.ro/ )
// sketch door lock ver.4.0 (19.11.2013) use sketch door lock ver.2.0 (02.03.2013)
// http://nicuflorica.blogspot.com
#include <Keypad.h>
#include <EEPROM.h>
char* secretCode = "2255";
int position = 0;
int position2 = 0;
boolean locked = true;
const byte rows = 4;
const byte cols = 3;
char keys[rows][cols] = {
{'1','2','3'},
{'4','5','6'},
{'7','8','9'},
{'*','0','#'}
};
byte rowPins[rows] = {2, 7, 6, 4};
byte colPins[cols] = {3, 1, 5};

Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, rows, cols);
int redPin = 11;
int greenPin = 9;
int bluePin = 10;
int solenoidPin = 13;

// include the library code:
#include <LiquidCrystal.h>

// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(14, 15, 16, 17, 18, 19);

/*
---------------------
| Arduino | LCD1602 |
---------------------
| D14(A0) |    RS   |
---------------------
| D15(A1) |    E    |
---------------------
| D16(A2) |    D4   |
---------------------
| D14(A3) |    D5   |
---------------------
| D14(A4) |    D6   |
---------------------
| D14(A5) |    D7   |
---------------------

*/

#include <Servo.h>
Servo servoMain; // Define our Servo


void setup()
{
/*pinMode(redPin, OUTPUT);
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);
*/
//pinMode(9, OUTPUT);
servoMain.attach(9); // servo on digital pin 9
//servoMain.write(0);   // Turn Servo Left to 0 degrees
lcd.begin(16, 2);
// print my logo
lcd.setCursor(0, 0);
lcd.print("www.tehnic.go.ro");
lcd.setCursor(0, 1);
lcd.print("   by niq_ro");
delay(5000);
lcd.clear();

lcd.setCursor(0, 0);
lcd.print("acces permis la");
lcd.setCursor(2, 1);
lcd.print("introducere");
delay(1000);
lcd.clear();

lcd.setCursor(2, 0);
lcd.print("parola corecta");
lcd.setCursor(2, 1);
lcd.print("versiune 4 .0");
delay(1000);
lcd.clear();

lcd.setCursor(0, 0);
lcd.print("apasa tasta '#'");
lcd.setCursor(0, 1);
lcd.print("pentru stergere");
delay(5000);
lcd.clear();


//eraseCode(); // a first test for initial code at "2255";
delay(1000);
loadCode(); // load the code from EEPROM

flash();
updateOutputs();

// print a new message
lcd.clear();
lcd.setCursor(1, 0);
lcd.print("introdu parola");
lcd.setCursor(0, 1);

}
void loop()
{

//servoMain.write(0);   // Turn Servo Left to 0 degrees
char key = keypad.getKey();
if (key)
{
position2 ++;
//digitalWrite(bluePin, HIGH);
delay(30);
//digitalWrite(bluePin, LOW);
lcd.print("?");
}
if (key == '*' && ! locked)
{
// unlocked and * pressed so change code
position = 0;
position2 = 0;
getNewCode();
updateOutputs();
}
if (key == '#')
{
locked = true;
position = 0;
position2= 0;
updateOutputs();
//digitalWrite(bluePin, HIGH);
//delay(300);
//digitalWrite(bluePin, LOW);

lcd.clear();
lcd.setCursor(1, 0);
lcd.print("yala incuiata");
lcd.setCursor(0, 1);
delay(1000);
lcd.clear();
lcd.setCursor(1, 0);
lcd.print("introdu parola");
lcd.setCursor(0, 1);

//lcd.clear();

}
if (key == secretCode[position])
{
position ++;
}
if (position == 4 & position2 == 4)
{
locked = false;
//digitalWrite(bluePin, HIGH);
delay(300);
//digitalWrite(bluePin, LOW);
updateOutputs();
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("parola corecta..");
lcd.setCursor(2, 1);
lcd.print("acces permis");
}
delay(100);

}

void updateOutputs()
{
if (locked)
{
//digitalWrite(redPin, HIGH);
//digitalWrite(greenPin, LOW);
digitalWrite(solenoidPin, HIGH);
servoMain.write(90);  // Turn Servo back to center position (90 degrees)
}
else
{
//digitalWrite(redPin, LOW);
//digitalWrite(greenPin, HIGH);
digitalWrite(solenoidPin, LOW);
servoMain.write(0);   // Turn Servo Left to 0 degrees
}
}

void getNewCode()
{
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("parola noua este");
lcd.setCursor(6, 1);
lcd.print("");

flash();
for (int i = 0; i < 4; i++ )
{
char key;
key = keypad.getKey();
while (key == 0)
{
key = keypad.getKey();
}
flash();
secretCode[i] = key;
lcd.print(key);
}
saveCode();
flash();flash();

}


void loadCode()
{
if (EEPROM.read(0) == 7)
{
secretCode[0] = EEPROM.read(1);
secretCode[1] = EEPROM.read(2);
secretCode[2] = EEPROM.read(3);
secretCode[3] = EEPROM.read(4);
}
}

void saveCode()
{
EEPROM.write(1, secretCode[0]);
EEPROM.write(2, secretCode[1]);
EEPROM.write(3, secretCode[2]);
EEPROM.write(4, secretCode[3]);
EEPROM.write(0, 7);
}


void eraseCode() // code is "2255"
{
EEPROM.write(1, 2);
EEPROM.write(2, 2);
EEPROM.write(3, 5);
EEPROM.write(4, 5);
EEPROM.write(0, 2);
}


void flash()
{
/*digitalWrite(redPin, HIGH);
digitalWrite(greenPin, LOW);
delay(100);
digitalWrite(redPin, LOW);
digitalWrite(greenPin, HIGH);
*/
delay(100);
}
    Sketch-ul pentru a 2-a versiune este:
// original schematic and schetch from http://www.arduinoevilgenius.com/
// adapted schematic by niq_ro ( http://www.tehnic.go.ro/ )
// sketch door lock ver.4.1 (19.11.2013) use sketch door lock ver.2.0 (02.03.2013)
// http://nicuflorica.blogspot.com
#include <Keypad.h>
#include <EEPROM.h>
char* secretCode = "2255";
int position = 0;
int position2 = 0;
boolean locked = true;
const byte rows = 4;
const byte cols = 3;
char keys[rows][cols] = {
{'1','2','3'},
{'4','5','6'},
{'7','8','9'},
{'*','0','#'}
};
byte rowPins[rows] = {2, 7, 6, 4};
byte colPins[cols] = {3, 1, 5};

Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, rows, cols);
/*
int redPin = 11;
int greenPin = 9;
int bluePin = 10;
*/
int solenoidPin = 13;

// include the library code:
#include <LiquidCrystal.h>

// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(14, 15, 16, 17, 18, 19);

/*
---------------------
| Arduino | LCD1602 |
---------------------
| D14(A0) |    RS   |
---------------------
| D15(A1) |    E    |
---------------------
| D16(A2) |    D4   |
---------------------
| D14(A3) |    D5   |
---------------------
| D14(A4) |    D6   |
---------------------
| D14(A5) |    D7   |
---------------------
*/


#include <Servo.h>
Servo servoMain; // Define our Servo


void setup()
{
  /*
pinMode(redPin, OUTPUT);
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);
*/
pinMode(solenoidPin, OUTPUT);
servoMain.attach(9); // servo on digital pin 9

lcd.begin(16, 2);
// print my logo
lcd.setCursor(0, 0);
lcd.print("www.tehnic.go.ro");
lcd.setCursor(0, 1);
lcd.print("   by niq_ro");
delay(5000);
lcd.clear();

lcd.setCursor(0, 0);
lcd.print("acces permis la");
lcd.setCursor(2, 1);
lcd.print("introducere");
delay(1000);
lcd.clear();

lcd.setCursor(1, 0);
lcd.print("parola corecta");
lcd.setCursor(2, 1);
lcd.print("versiune 4.1");
delay(1000);
lcd.clear();

lcd.setCursor(0, 0);
lcd.print("apasa tasta '#'");
lcd.setCursor(0, 1);
lcd.print("pentru stergere");
delay(2000);
lcd.clear();



//eraseCode(); // a first test for initial code at "2255";
delay(1000);
loadCode(); // load the code from EEPROM

flash();
updateOutputs();
afisaj();

}
void loop()
{
/*
 servoMain.write(0);   // Turn Servo Left (to 0 degrees)
 delay(500);          // Wait 1 second  
*/

char key = keypad.getKey();
if (key)
{
position2 ++;
// digitalWrite(bluePin, HIGH);
delay(30);
// digitalWrite(bluePin, LOW);
lcd.print("?");
}


if (key == '*' && ! locked)
{
// unlocked and * pressed so change code
position = 0;
position2 = 0;
getNewCode();
updateOutputs();
afisaj();
}
// if (key == '#' && ! locked)   // manual locked when push '#'
if (key == '#' )   // manual locked when push '#'
{
locked = true;
position = 0;
position2= 0;
updateOutputs();
// digitalWrite(bluePin, HIGH);
//delay(300);
// digitalWrite(bluePin, LOW);
afisaj();
}

if (key == secretCode[position])
{
position ++;
}
if (position == 4 & position2 == 4)
{
locked = false;
// digitalWrite(bluePin, HIGH);
delay(300);
// digitalWrite(bluePin, LOW);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("parola corecta..");
lcd.setCursor(0, 1);
lcd.print("acces permis 5s");
updateOutputs();
afisaj();
}
}


void updateOutputs()
{
if (locked)
{
//digitalWrite(redPin, HIGH);
//digitalWrite(greenPin, LOW);
digitalWrite(solenoidPin, LOW);
servoMain.write(90);  // Turn Servo back to center position (90 degrees)
}
else
{
//digitalWrite(redPin, LOW);
//digitalWrite(greenPin, HIGH);
digitalWrite(solenoidPin, HIGH);
servoMain.write(0);  // Turn Servo back to left position (0 degrees)

delay (50);
for (int i=0; i <= 100; i++)
{
char key;
key = keypad.getKey();
if (key == '*')
{
// unlocked and * pressed so change code
position = 0;
position2 = 0;
getNewCode();
updateOutputs();
}
delay (50);
}
//digitalWrite(redPin, HIGH);
//digitalWrite(greenPin, LOW);
digitalWrite(solenoidPin, LOW);
servoMain.write(90);  // Turn Servo back to center position (90 degrees)
locked = true;
position = 0;
position2= 0;
}
}


void getNewCode()
{
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("parola noua este");
lcd.setCursor(6, 1);
lcd.print("");

flash();
for (int i = 0; i < 4; i++ )
{
char key;
key = keypad.getKey();
while (key == 0)
{
key = keypad.getKey();
}
flash();
secretCode[i] = key;
lcd.print(key);
}
saveCode();
flash();flash();
//delay(400);
//afisaj();
}


void loadCode()
{
if (EEPROM.read(0) == 7)
{
secretCode[0] = EEPROM.read(1);
secretCode[1] = EEPROM.read(2);
secretCode[2] = EEPROM.read(3);
secretCode[3] = EEPROM.read(4);
}
}

void saveCode()
{
EEPROM.write(1, secretCode[0]);
EEPROM.write(2, secretCode[1]);
EEPROM.write(3, secretCode[2]);
EEPROM.write(4, secretCode[3]);
EEPROM.write(0, 7);
}


void eraseCode() // code is "2255"
{
EEPROM.write(1, 2);
EEPROM.write(2, 2);
EEPROM.write(3, 5);
EEPROM.write(4, 5);
EEPROM.write(0, 2);
}


void flash()
{
//digitalWrite(redPin, HIGH);
//digitalWrite(greenPin, LOW);
delay(100);
//digitalWrite(redPin, LOW);
//digitalWrite(greenPin, HIGH);
delay(100);
}

void afisaj()
{
lcd.clear();
lcd.setCursor(1, 0);
lcd.print("yala incuiata");
lcd.setCursor(0, 1);
delay(500);
lcd.clear();
lcd.setCursor(1, 0);
lcd.print("introdu parola");
lcd.setCursor(0, 1);
}

   Ulterior am conectat si LED-ul multicolor, care imi indica starea (rosu la inchis, verde la deschis si albastru la apasare tasta):
 
   Am facut un filmulet, numit Arduino - door lock with code - ver.4.2
iar sketch-ul pentru aceasta varianta este:
// original schematic and schetch from http://www.arduinoevilgenius.com/
// adapted schematic by niq_ro ( http://www.tehnic.go.ro/ )
// sketch door lock ver.4.2 (20.11.2013) use sketch door lock ver.2.0 (02.03.2013)
// http://nicuflorica.blogspot.com

#include <Keypad.h>
#include <EEPROM.h>
char* secretCode = "2255";
int position = 0;
int position2 = 0;
boolean locked = true;
const byte rows = 4;
const byte cols = 3;
char keys[rows][cols] = {
{'1','2','3'},
{'4','5','6'},
{'7','8','9'},
{'*','0','#'}
};
byte rowPins[rows] = {2, 7, 6, 4};
byte colPins[cols] = {3, 1, 5};

Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, rows, cols);

int solenoidPin = 13;
int redPin = 12;
int bluePin = 11;
int greenPin = 10;
int servoPin = 9;

// include the library code:
#include <LiquidCrystal.h>

// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(14, 15, 16, 17, 18, 19);

/*
---------------------
| Arduino | LCD1602 |
---------------------
| D14(A0) |    RS   |
---------------------
| D15(A1) |    E    |
---------------------
| D16(A2) |    D4   |
---------------------
| D14(A3) |    D5   |
---------------------
| D14(A4) |    D6   |
---------------------
| D14(A5) |    D7   |
---------------------
*/


#include <Servo.h>
Servo servoMain; // Define our Servo


void setup()
{

pinMode(redPin, OUTPUT);
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);

pinMode(solenoidPin, OUTPUT);
servoMain.attach(servoPin); 

lcd.begin(16, 2);
// print my logo
lcd.setCursor(0, 0);
lcd.print("www.tehnic.go.ro");
lcd.setCursor(0, 1);
lcd.print("   by niq_ro");
delay(5000);
lcd.clear();

lcd.setCursor(0, 0);
lcd.print("acces permis la");
lcd.setCursor(2, 1);
lcd.print("introducere");
delay(1000);
lcd.clear();

lcd.setCursor(1, 0);
lcd.print("parola corecta");
lcd.setCursor(2, 1);
lcd.print("versiune 4.2");
delay(1000);
lcd.clear();

lcd.setCursor(0, 0);
lcd.print("apasa tasta '#'");
lcd.setCursor(0, 1);
lcd.print("pentru stergere");
delay(2000);
lcd.clear();



//eraseCode(); // a first test for initial code at "2255";
delay(1000);
loadCode(); // load the code from EEPROM

flash();
updateOutputs();
afisaj();

}
void loop()
{

char key = keypad.getKey();
if (key)
{
position2 ++;
digitalWrite(bluePin, HIGH);
delay(30);
digitalWrite(bluePin, LOW);
lcd.print("?");
}


if (key == '*' && ! locked)
{
// unlocked and * pressed so change code
position = 0;
position2 = 0;
getNewCode();
updateOutputs();
afisaj();
}
// if (key == '#' && ! locked)   // manual locked when push '#'
if (key == '#' )   // manual locked when push '#'
{
locked = true;
position = 0;
position2= 0;
updateOutputs();
digitalWrite(bluePin, HIGH);
delay(300);
digitalWrite(bluePin, LOW);
afisaj();
}

if (key == secretCode[position])
{
position ++;
}
if (position == 4 & position2 == 4)
{
locked = false;
digitalWrite(bluePin, HIGH);
delay(300);
digitalWrite(bluePin, LOW);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("parola corecta..");
lcd.setCursor(0, 1);
lcd.print("acces permis 5s");
updateOutputs();
afisaj();
}
}


void updateOutputs()
{
if (locked)
{
digitalWrite(redPin, HIGH);
digitalWrite(greenPin, LOW);
digitalWrite(solenoidPin, LOW);
servoMain.write(90);  // Turn Servo back to center position (90 degrees)
}
else
{
digitalWrite(redPin, LOW);
digitalWrite(greenPin, HIGH);
digitalWrite(solenoidPin, HIGH);
servoMain.write(0);  // Turn Servo back to left position (0 degrees)

delay (50);
for (int i=0; i <= 100; i++)
{
char key;
key = keypad.getKey();
if (key == '*')
{
// unlocked and * pressed so change code
position = 0;
position2 = 0;
getNewCode();
updateOutputs();
}
delay (50);
}
digitalWrite(redPin, HIGH);
digitalWrite(greenPin, LOW);
digitalWrite(solenoidPin, LOW);
servoMain.write(90);  // Turn Servo back to center position (90 degrees)
locked = true;
position = 0;
position2= 0;
}
}


void getNewCode()
{
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("parola noua este");
lcd.setCursor(6, 1);
lcd.print("");

flash();
for (int i = 0; i < 4; i++ )
{
char key;
key = keypad.getKey();
while (key == 0)
{
key = keypad.getKey();
}
flash();
secretCode[i] = key;
lcd.print(key);
}
saveCode();
flash();flash();
//delay(400);
//afisaj();
}


void loadCode()
{
if (EEPROM.read(0) == 7)
{
secretCode[0] = EEPROM.read(1);
secretCode[1] = EEPROM.read(2);
secretCode[2] = EEPROM.read(3);
secretCode[3] = EEPROM.read(4);
}
}

void saveCode()
{
EEPROM.write(1, secretCode[0]);
EEPROM.write(2, secretCode[1]);
EEPROM.write(3, secretCode[2]);
EEPROM.write(4, secretCode[3]);
EEPROM.write(0, 7);
}


void eraseCode() // code is "2255"
{
EEPROM.write(1, 2);
EEPROM.write(2, 2);
EEPROM.write(3, 5);
EEPROM.write(4, 5);
EEPROM.write(0, 2);
}


void flash()
{
digitalWrite(redPin, HIGH);
digitalWrite(greenPin, LOW);
delay(100);
digitalWrite(redPin, LOW);
digitalWrite(greenPin, HIGH);
delay(100);
}

void afisaj()
{
lcd.clear();
lcd.setCursor(1, 0);
lcd.print("yala incuiata");
lcd.setCursor(0, 1);
delay(500);
lcd.clear();
lcd.setCursor(1, 0);
lcd.print("introdu parola");
lcd.setCursor(0, 1);
}

joi, 21 noiembrie 2013

Arduino si un servomotor

   Dupa ce am primit un cadou deosebit, un "Arduino Starter Kit" de la prietenul meu Adrian Roman, caruia ii multumesc si pe aceasta cale, am ales sa fac probe cu servomotorul si, bineinteles, placa originala Arduino UNO R3...
   Din primele teste am constatat ca servomotorul meu nu poate face cursa completa pana la 1800 am limitat miscarea intre 0..1700.
   Am folosit sketch-ul numit Sweep de la Servo din exemple si sketch-ul prezentat la http://www.hobbytronics.co.uk/arduino-tutorial2-servos
iar un sketch, care le combina pe cele doua, in care pinul D9 este pentru comanda:
/*
Arduino Servo Test sketch
http://www.hobbytronics.co.uk/arduino-tutorial2-servos
and 
SWEEEP by BARRAGAN http://barraganstudio.com
adapted by niq_ro from http://nicuflorica.blogspot.com
*/

#include <Servo.h>
Servo servoMain; // Define our Servo

void setup()
{
servoMain.attach(9); // servo on digital pin 9
pinMode(13, OUTPUT); // initialize the digital pin no 13 as an output.
}

void loop()
{
  
   servoMain.write(45);  // Turn Servo Left to 45 degrees
   delay(1000);          // Wait 1 second
   servoMain.write(0);   // Turn Servo Left to 0 degrees
   delay(1000);          // Wait 1 second
   servoMain.write(90);  // Turn Servo back to center position (90 degrees)
   digitalWrite(13, HIGH);  
   delay(4000);          // Wait 1 second
   digitalWrite(13, LOW);  
   servoMain.write(135); // Turn Servo Right to 135 degrees
   delay(1000);          // Wait 1 second
   servoMain.write(170); // Turn Servo Right to 170 degrees
   delay(1000);          // Wait 1 second
   servoMain.write(90);  // Turn Servo back to center position (90 degrees)
   delay(1000);          // Wait 1 second   

// adapted part from SWEEP example
  for(int pos = 0; pos < 170; pos += 1)  // goes from 0 degrees to 170 degrees 
  {                                  // in steps of 1 degree 
    servoMain.write(pos);              // tell servo to go to position in variable 'pos' 
    delay(15);                       // waits 15ms for the servo to reach the position 
  }
 delay(1000); 
  for(int pos = 170; pos>=1; pos-=1)     // goes from 170 degrees to 0 degrees 
  {                                
    servoMain.write(pos);              // tell servo to go to position in variable 'pos' 
    delay(15);                       // waits 15ms for the servo to reach the position 
  } 
delay (1000);
}
iar filmul, care demonstreaza functionarea, dupa acest sketch se numeste First tests with servomotor and Arduino from "Arduino Starter Kit":
   Pentru a nu aparea neclaritati, pun si schema de conectare:

miercuri, 20 noiembrie 2013

Arduino de casa cu interfata USB CP2102

   Fata de articolul similar din iunie, numit Arduino de casa cu interfata USB PL2303HX, in care am prezentat un modul care nu are pinul DTR/RESET si la incarcarea unui sketch in Arduino trebuia sa tin apasat butonul RESET de pe placa, acum am sa prezint un modul care se bazeaza pe integratul CP2102 fabricat de Silicon Labs si are disponibil si pinul DTR/RST.
   Modulul se numeste STE13-007 si e fabricat de o firma BAITE... din pacate nu am gasit poza exacta a acestuia, asa ca am facut unele cu telefonul (decat deloc mai bine ceva...)
   Ulterior, am facut si cu un aparat foto serios:
   Dupa instalarea driver-ului disponibil pe pagina fabricantului, cand se conecteaza interfata la calculator vor gasi pe portul 15 (in cazul meu) Silicon Labs CP210x USB to UART Bridge:
  Modul de conectare la o placa Arduino de casa (sau una fara interfata USB) foarte simplu:
   Ca sa nu se interpreteze gresit, eu am avut interfata USB cu integratul PL2303HX fara pinul de reset (RST/DTR), dar pe piata exista si cu... iar corespondenta din tabelul de mai sus este valabila pentru toate interfetele USB...
  Am facut si un scurt filmulet numit Arduino de casa cu interfata USB pe baza integratului CP2102
   Sketch-ul modificat folosit in filmulet este:
/*
  Blink
  Turns on an LED on for one second, then off for one second, repeatedly.
  This example code is in the public domain. 

   sketch modified by niq_ro for testing a home-made Arduino
 for more details see http://nicuflorica.blogspot.com/
   Craiova, 11.2013
*/
 
 
// Pin 13 has an LED connected on most Arduino boards.
// give it a name:
int led = 13;

// the setup routine runs once when you press reset:
void setup() {                
  // initialize the digital pin as an output.
  pinMode(led, OUTPUT);     
}

// the loop routine runs over and over again forever:
void loop() {
  digitalWrite(led, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(100);               // wait for 100ms
  digitalWrite(led, LOW);    // turn the LED off by making the voltage LOW
  delay(100);               // wait for 100ms
  digitalWrite(led, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(100);               // wait for 100ms
  digitalWrite(led, LOW);    // turn the LED off by making the voltage LOW
  delay(100);               // wait for 100ms
  digitalWrite(led, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(100);               // wait for a second
  digitalWrite(led, LOW);    // turn the LED off by making the voltage LOW
  delay(1000);
}
  

luni, 18 noiembrie 2013

Arduino si o tastatura cu 12 butoane (II)

   Fata de articolul precedent, in care am facut un sistem de acces cu cod, cu timp de acces pana se apasa o tasta sau temporizat, fara sau cu posibilitate de schimbare a parolei, acum o sa conectez si un afisaj LCD1602 (cu 16 coloane si 2 randuri).
   Deoarece pentru tastatura folosesc 7 intrari digitale (D1..D7), iar pentru LED-ul multicolor inca 3 (D9, D10, D11) + o iesire pentru electromagnet (D13), nu am suficiente iesiri digitale pentru afisajul LCD... imi trebuie 6... asa ca o sa folosesc cele 6 intrari analogice A0..A5 ca iesiri digitale D14..D19, dupa cum am gasit in cartea "30 Arduino Projects for the Evil Genius" scrisa de Simon Monk si in articolul How to add 6 extra pins to your Arduino with no extra hardware
 
   Schema de conectare, din bucatele este:
O poza cu montajul:
   Schema completa este:
   Un sketch care are implementata si partea de afisare si acces functie de parola, iar inchiderea se face apasand tasta '#'. Schimbarea parolei se face cand zavorul este decuplat (acces permis) dupa apasarea tastei '*' (stocarea datelor se face in memoria interna EEPROM a microcontrolerului ATmega). LED-ul multicolor se aprinde in rosu, cand yala este incuiata, verde cand este descuiata si "palpaie" in albastru cand se apasa oricare tasta.
   Am facut 2 filmulete in care sunt toate starile si prezint modul de functionare:
   Sketch-ul pentru ce am prezentat mai inainte este:  
// original schematic and schetch from http://www.arduinoevilgenius.com/
// adapted schematic by niq_ro ( http://www.tehnic.go.ro/ )
// sketch door lock ver.3.1 (16.11.2013) use sketch door lock ver.2.0 (02.03.2013)
#include <Keypad.h>
#include <EEPROM.h>
char* secretCode = "2255";
int position = 0;
int position2 = 0;
boolean locked = true;
const byte rows = 4;
const byte cols = 3;
char keys[rows][cols] = {
{'1','2','3'},
{'4','5','6'},
{'7','8','9'},
{'*','0','#'}
};
byte rowPins[rows] = {2, 7, 6, 4};
byte colPins[cols] = {3, 1, 5};

Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, rows, cols);
int redPin = 11;
int greenPin = 9;
int bluePin = 10;
int solenoidPin = 13;

// include the library code:
#include <LiquidCrystal.h>

// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(14, 15, 16, 17, 18, 19);

/*
---------------------
| Arduino | LCD1602 |
---------------------
| D14(A0) |    RS   |
---------------------
| D15(A1) |    E    |
---------------------
| D16(A2) |    D4   |
---------------------
| D14(A3) |    D5   |
---------------------
| D14(A4) |    D6   |
---------------------
| D14(A5) |    D7   |
---------------------

*/

void setup()
{
pinMode(redPin, OUTPUT);
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);

lcd.begin(16, 2);
// print my logo
lcd.setCursor(0, 0);
lcd.print("www.tehnic.go.ro");
lcd.setCursor(0, 1);
lcd.print("   by niq_ro");
delay(5000);
lcd.clear();

lcd.setCursor(0, 0);
lcd.print("acces permis la");
lcd.setCursor(2, 1);
lcd.print("introducere");
delay(1000);
lcd.clear();

lcd.setCursor(2, 0);
lcd.print("parola corecta");
lcd.setCursor(2, 1);
lcd.print("versiune 3.0");
delay(1000);
lcd.clear();

lcd.setCursor(0, 0);
lcd.print("apasa tasta '#'");
lcd.setCursor(0, 1);
lcd.print("pentru stergere");
delay(5000);
lcd.clear();


//eraseCode(); // a first test for initial code at "2255";
delay(1000);
loadCode(); // load the code from EEPROM

flash();
updateOutputs();

// print a new message
lcd.clear();
lcd.setCursor(1, 0);
lcd.print("introdu parola");
lcd.setCursor(0, 1);

}
void loop()
{

char key = keypad.getKey();
if (key)
{
position2 ++;
digitalWrite(bluePin, HIGH);
delay(30);
digitalWrite(bluePin, LOW);
lcd.print("?");
}
if (key == '*' && ! locked)
{
// unlocked and * pressed so change code
position = 0;
position2 = 0;
getNewCode();
updateOutputs();
}
if (key == '#')
{
locked = true;
position = 0;
position2= 0;
updateOutputs();
digitalWrite(bluePin, HIGH);
delay(300);
digitalWrite(bluePin, LOW);

lcd.clear();
lcd.setCursor(1, 0);
lcd.print("yala incuiata");
lcd.setCursor(0, 1);
delay(1000);
lcd.clear();
lcd.setCursor(1, 0);
lcd.print("introdu parola");
lcd.setCursor(0, 1);

//lcd.clear();

}
if (key == secretCode[position])
{
position ++;
}
if (position == 4 & position2 == 4)
{
locked = false;
digitalWrite(bluePin, HIGH);
delay(300);
digitalWrite(bluePin, LOW);
updateOutputs();
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("parola corecta..");
lcd.setCursor(2, 1);
lcd.print("acces permis");
}
delay(100);

}

void updateOutputs()
{
if (locked)
{
digitalWrite(redPin, HIGH);
digitalWrite(greenPin, LOW);
digitalWrite(solenoidPin, HIGH);
}
else
{
digitalWrite(redPin, LOW);
digitalWrite(greenPin, HIGH);
digitalWrite(solenoidPin, LOW);
}
}

void getNewCode()
{
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("parola noua este");
lcd.setCursor(6, 1);
lcd.print("");

flash();
for (int i = 0; i < 4; i++ )
{
char key;
key = keypad.getKey();
while (key == 0)
{
key = keypad.getKey();
}
flash();
secretCode[i] = key;
lcd.print(key);
}
saveCode();
flash();flash();

}


void loadCode()
{
if (EEPROM.read(0) == 7)
{
secretCode[0] = EEPROM.read(1);
secretCode[1] = EEPROM.read(2);
secretCode[2] = EEPROM.read(3);
secretCode[3] = EEPROM.read(4);
}
}

void saveCode()
{
EEPROM.write(1, secretCode[0]);
EEPROM.write(2, secretCode[1]);
EEPROM.write(3, secretCode[2]);
EEPROM.write(4, secretCode[3]);
EEPROM.write(0, 7);
}


void eraseCode() // code is "2255"
{
EEPROM.write(1, 2);
EEPROM.write(2, 2);
EEPROM.write(3, 5);
EEPROM.write(4, 5);
EEPROM.write(0, 2);
}


void flash()
{
digitalWrite(redPin, HIGH);
digitalWrite(greenPin, LOW);
delay(100);
digitalWrite(redPin, LOW);
digitalWrite(greenPin, HIGH);
delay(100);
}
   Am modificat sketch-ul pentru a avea acces temporizat (doar 5 secunde dupa introducerea corecta a codului), dupa cum se vede in filmuletul Arduino - door lock ver.3.4 (code stored in EEPROM) with 1602 LCD - acces for 5 seconds
   Sketch-ul modificat este:

// original schematic and schetch from http://www.arduinoevilgenius.com/
// adapted schematic by niq_ro ( http://www.tehnic.go.ro/ )
// sketch door lock ver.3.4 (17.11.2013) use sketch door lock ver.2.0 (02.03.2013)
#include <Keypad.h>
#include <EEPROM.h>
char* secretCode = "2255";
int position = 0;
int position2 = 0;
boolean locked = true;
const byte rows = 4;
const byte cols = 3;
char keys[rows][cols] = {
{'1','2','3'},
{'4','5','6'},
{'7','8','9'},
{'*','0','#'}
};
byte rowPins[rows] = {2, 7, 6, 4};
byte colPins[cols] = {3, 1, 5};

Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, rows, cols);
int redPin = 11;
int greenPin = 9;
int bluePin = 10;
int solenoidPin = 13;

// include the library code:
#include <LiquidCrystal.h>

// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(14, 15, 16, 17, 18, 19);

/*
---------------------
| Arduino | LCD1602 |
---------------------
| D14(A0) |    RS   |
---------------------
| D15(A1) |    E    |
---------------------
| D16(A2) |    D4   |
---------------------
| D14(A3) |    D5   |
---------------------
| D14(A4) |    D6   |
---------------------
| D14(A5) |    D7   |
---------------------
*/

void setup()
{
pinMode(redPin, OUTPUT);
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);

lcd.begin(16, 2);
// print my logo
lcd.setCursor(0, 0);
lcd.print("www.tehnic.go.ro");
lcd.setCursor(0, 1);
lcd.print("   by niq_ro");
delay(5000);
lcd.clear();

lcd.setCursor(0, 0);
lcd.print("acces permis la");
lcd.setCursor(2, 1);
lcd.print("introducere");
delay(1000);
lcd.clear();

lcd.setCursor(1, 0);
lcd.print("parola corecta");
lcd.setCursor(2, 1);
lcd.print("versiune 3.4");
delay(1000);
lcd.clear();

lcd.setCursor(1, 0);
lcd.print("apasa tasta '#'");
lcd.setCursor(2, 1);
lcd.print("pentru stergere");
delay(1000);
lcd.clear();

//eraseCode(); // a first test for initial code at "2255";
delay(1000);
loadCode(); // load the code from EEPROM

flash();
updateOutputs();
afisaj();

}
void loop()
{

char key = keypad.getKey();
if (key)
{
position2 ++;
digitalWrite(bluePin, HIGH);
delay(30);
digitalWrite(bluePin, LOW);
lcd.print("?");
}


if (key == '*' && ! locked)
{
// unlocked and * pressed so change code
position = 0;
position2 = 0;
getNewCode();
updateOutputs();
afisaj();
}
if (key == '#' )   // manual locked when push '#'
{
locked = true;
position = 0;
position2= 0;
updateOutputs();
digitalWrite(bluePin, HIGH);
delay(300);
digitalWrite(bluePin, LOW);
afisaj();
}

if (key == secretCode[position])
{
position ++;
}
if (position == 4 & position2 == 4)
{
locked = false;
digitalWrite(bluePin, HIGH);
delay(300);
digitalWrite(bluePin, LOW);
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("parola corecta..");
lcd.setCursor(0, 1);
lcd.print("acces permis 5s");
updateOutputs();
afisaj();
}
}


void updateOutputs()
{
if (locked)
{
digitalWrite(redPin, HIGH);
digitalWrite(greenPin, LOW);
digitalWrite(solenoidPin, HIGH);
}
else
{
digitalWrite(redPin, LOW);
digitalWrite(greenPin, HIGH);
digitalWrite(solenoidPin, LOW);

delay (50);
for (int i=0; i <= 100; i++)
{
char key;
key = keypad.getKey();
if (key == '*')
{
// unlocked and * pressed so change code
position = 0;
position2 = 0;
getNewCode();
updateOutputs();
}
delay (50);
}
digitalWrite(redPin, HIGH);
digitalWrite(greenPin, LOW);
digitalWrite(solenoidPin, HIGH);
locked = true;
position = 0;
position2= 0;
}
}




void getNewCode()
{
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("parola noua este");
lcd.setCursor(6, 1);
lcd.print("");

flash();
for (int i = 0; i < 4; i++ )
{
char key;
key = keypad.getKey();
while (key == 0)
{
key = keypad.getKey();
}
flash();
secretCode[i] = key;
lcd.print(key);
}
saveCode();
flash();flash();
//delay(400);
//afisaj();
}


void loadCode()
{
if (EEPROM.read(0) == 7)
{
secretCode[0] = EEPROM.read(1);
secretCode[1] = EEPROM.read(2);
secretCode[2] = EEPROM.read(3);
secretCode[3] = EEPROM.read(4);
}
}

void saveCode()
{
EEPROM.write(1, secretCode[0]);
EEPROM.write(2, secretCode[1]);
EEPROM.write(3, secretCode[2]);
EEPROM.write(4, secretCode[3]);
EEPROM.write(0, 7);
}


void eraseCode() // code is "2255"
{
EEPROM.write(1, 2);
EEPROM.write(2, 2);
EEPROM.write(3, 5);
EEPROM.write(4, 5);
EEPROM.write(0, 2);
}


void flash()
{
digitalWrite(redPin, HIGH);
digitalWrite(greenPin, LOW);
delay(100);
digitalWrite(redPin, LOW);
digitalWrite(greenPin, HIGH);
delay(100);
}

void afisaj()
{
lcd.clear();
lcd.setCursor(1, 0);
lcd.print("yala incuiata");
lcd.setCursor(0, 1);
delay(500);
lcd.clear();
lcd.setCursor(1, 0);
lcd.print("introdu parola");
lcd.setCursor(0, 1);
}