السبت، 7 أكتوبر 2017

مشروع نظام الامن والانذار باستخدام اردوينو

يتم تنشيط التنبيه في 10 ثوان بعد الضغط على الزر A. للكشف عن الأشياء فإنه يستخدم جهاز استشعار بالموجات فوق الصوتية، وبمجرد أن إنذار كشف شيء ما الجرس يبدأ انبعاث الصوت. من أجل وقف التنبيه نحن بحاجة إلى إدراج كلمة مرور 4 أرقام. كلمة السر مسبقا هي 1234 ولكن لدينا أيضا إمكانية لتغييره.
عن طريق الضغط على زر B ندخل قائمة تغيير كلمة المرور حيث أولا نحن بحاجة إلى إدخال كلمة المرور الحالية من أجل الاستمرار ثم أدخل كلمة المرور الجديدة 4 أرقام. مرة واحدة يتم تغيير كلمة المرور،    سوف تكون قادرا على وقف التنبيه عن طريق إدخال كلمة المرور الجديدة فقط. إذا أدخلنا كلمة مرور خاطئة سوف نحصل على رسالة أننا بحاجة إلى المحاولة مرة أخرى.

المكونات المطلوبة
لوحة Arduino 
 متحسس التراسونيك ultrasonic sensor
شاشة LCD 
4×4 keypad
منبه




ربط الدائرة

كود اردوينو

  1. /*
  2. * Arduino Security and Alarm System
  3. *
  4. * by Dejan Nedelkovski,
  5. * www.HowToMechatronics.com
  6. *
  7. */
  8. #include <LiquidCrystal.h> // includes the LiquidCrystal Library
  9. #include <Keypad.h>
  10. #define buzzer 8
  11. #define trigPin 9
  12. #define echoPin 10
  13. long duration;
  14. int distance, initialDistance, currentDistance, i;
  15. int screenOffMsg =0;
  16. String password="1234";
  17. String tempPassword;
  18. boolean activated = false; // State of the alarm
  19. boolean isActivated;
  20. boolean activateAlarm = false;
  21. boolean alarmActivated = false;
  22. boolean enteredPassword; // State of the entered password to stop the alarm
  23. boolean passChangeMode = false;
  24. boolean passChanged = false;
  25. const byte ROWS = 4; //four rows
  26. const byte COLS = 4; //four columns
  27. char keypressed;
  28. //define the cymbols on the buttons of the keypads
  29. char keyMap[ROWS][COLS] = {
  30. {'1','2','3','A'},
  31. {'4','5','6','B'},
  32. {'7','8','9','C'},
  33. {'*','0','#','D'}
  34. };
  35. byte rowPins[ROWS] = {14, 15, 16, 17}; //Row pinouts of the keypad
  36. byte colPins[COLS] = {18, 19, 20, 21}; //Column pinouts of the keypad
  37. Keypad myKeypad = Keypad( makeKeymap(keyMap), rowPins, colPins, ROWS, COLS);
  38. LiquidCrystal lcd(1, 2, 4, 5, 6, 7); // Creates an LC object. Parameters: (rs, enable, d4, d5, d6, d7)
  39. void setup() {
  40. lcd.begin(16,2);
  41. pinMode(buzzer, OUTPUT); // Set buzzer as an output
  42. pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
  43. pinMode(echoPin, INPUT); // Sets the echoPin as an Input
  44. }
  45. void loop() {
  46. if (activateAlarm) {
  47. lcd.clear();
  48. lcd.setCursor(0,0);
  49. lcd.print("Alarm will be");
  50. lcd.setCursor(0,1);
  51. lcd.print("activated in");
  52. int countdown = 9; // 9 seconds count down before activating the alarm
  53. while (countdown != 0) {
  54. lcd.setCursor(13,1);
  55. lcd.print(countdown);
  56. countdown--;
  57. tone(buzzer, 700, 100);
  58. delay(1000);
  59. }
  60. lcd.clear();
  61. lcd.setCursor(0,0);
  62. lcd.print("Alarm Activated!");
  63. initialDistance = getDistance();
  64. activateAlarm = false;
  65. alarmActivated = true;
  66. }
  67. if (alarmActivated == true){
  68. currentDistance = getDistance() + 10;
  69. if ( currentDistance < initialDistance) {
  70. tone(buzzer, 1000); // Send 1KHz sound signal
  71. lcd.clear();
  72. enterPassword();
  73. }
  74. }
  75. if (!alarmActivated) {
  76. if (screenOffMsg == 0 ){
  77. lcd.clear();
  78. lcd.setCursor(0,0);
  79. lcd.print("A - Activate");
  80. lcd.setCursor(0,1);
  81. lcd.print("B - Change Pass");
  82. screenOffMsg = 1;
  83. }
  84. keypressed = myKeypad.getKey();
  85. if (keypressed =='A'){ //If A is pressed, activate the alarm
  86. tone(buzzer, 1000, 200);
  87. activateAlarm = true;
  88. }
  89. else if (keypressed =='B') {
  90. lcd.clear();
  91. int i=1;
  92. tone(buzzer, 2000, 100);
  93. tempPassword = "";
  94. lcd.setCursor(0,0);
  95. lcd.print("Current Password");
  96. lcd.setCursor(0,1);
  97. lcd.print(">");
  98. passChangeMode = true;
  99. passChanged = true;
  100. while(passChanged) {
  101. keypressed = myKeypad.getKey();
  102. if (keypressed != NO_KEY){
  103. if (keypressed == '0' || keypressed == '1' || keypressed == '2' || keypressed == '3' ||
  104. keypressed == '4' || keypressed == '5' || keypressed == '6' || keypressed == '7' ||
  105. keypressed == '8' || keypressed == '9' ) {
  106. tempPassword += keypressed;
  107. lcd.setCursor(i,1);
  108. lcd.print("*");
  109. i++;
  110. tone(buzzer, 2000, 100);
  111. }
  112. }
  113. if (i > 5 || keypressed == '#') {
  114. tempPassword = "";
  115. i=1;
  116. lcd.clear();
  117. lcd.setCursor(0,0);
  118. lcd.print("Current Password");
  119. lcd.setCursor(0,1);
  120. lcd.print(">");
  121. }
  122. if ( keypressed == '*') {
  123. i=1;
  124. tone(buzzer, 2000, 100);
  125. if (password == tempPassword) {
  126. tempPassword="";
  127. lcd.clear();
  128. lcd.setCursor(0,0);
  129. lcd.print("Set New Password");
  130. lcd.setCursor(0,1);
  131. lcd.print(">");
  132. while(passChangeMode) {
  133. keypressed = myKeypad.getKey();
  134. if (keypressed != NO_KEY){
  135. if (keypressed == '0' || keypressed == '1' || keypressed == '2' || keypressed == '3' ||
  136. keypressed == '4' || keypressed == '5' || keypressed == '6' || keypressed == '7' ||
  137. keypressed == '8' || keypressed == '9' ) {
  138. tempPassword += keypressed;
  139. lcd.setCursor(i,1);
  140. lcd.print("*");
  141. i++;
  142. tone(buzzer, 2000, 100);
  143. }
  144. }
  145. if (i > 5 || keypressed == '#') {
  146. tempPassword = "";
  147. i=1;
  148. tone(buzzer, 2000, 100);
  149. lcd.clear();
  150. lcd.setCursor(0,0);
  151. lcd.print("Set New Password");
  152. lcd.setCursor(0,1);
  153. lcd.print(">");
  154. }
  155. if ( keypressed == '*') {
  156. i=1;
  157. tone(buzzer, 2000, 100);
  158. password = tempPassword;
  159. passChangeMode = false;
  160. passChanged = false;
  161. screenOffMsg = 0;
  162. }
  163. }
  164. }
  165. }
  166. }
  167. }
  168. }
  169. }
  170. void enterPassword() {
  171. int k=5;
  172. tempPassword = "";
  173. activated = true;
  174. lcd.clear();
  175. lcd.setCursor(0,0);
  176. lcd.print(" *** ALARM *** ");
  177. lcd.setCursor(0,1);
  178. lcd.print("Pass>");
  179. while(activated) {
  180. keypressed = myKeypad.getKey();
  181. if (keypressed != NO_KEY){
  182. if (keypressed == '0' || keypressed == '1' || keypressed == '2' || keypressed == '3' ||
  183. keypressed == '4' || keypressed == '5' || keypressed == '6' || keypressed == '7' ||
  184. keypressed == '8' || keypressed == '9' ) {
  185. tempPassword += keypressed;
  186. lcd.setCursor(k,1);
  187. lcd.print("*");
  188. k++;
  189. }
  190. }
  191. if (k > 9 || keypressed == '#') {
  192. tempPassword = "";
  193. k=5;
  194. lcd.clear();
  195. lcd.setCursor(0,0);
  196. lcd.print(" *** ALARM *** ");
  197. lcd.setCursor(0,1);
  198. lcd.print("Pass>");
  199. }
  200. if ( keypressed == '*') {
  201. if ( tempPassword == password ) {
  202. activated = false;
  203. alarmActivated = false;
  204. noTone(buzzer);
  205. screenOffMsg = 0;
  206. }
  207. else if (tempPassword != password) {
  208. lcd.setCursor(0,1);
  209. lcd.print("Wrong! Try Again");
  210. delay(2000);
  211. lcd.clear();
  212. lcd.setCursor(0,0);
  213. lcd.print(" *** ALARM *** ");
  214. lcd.setCursor(0,1);
  215. lcd.print("Pass>");
  216. }
  217. }
  218. }
  219. }
  220. // Custom function for the Ultrasonic sensor
  221. long getDistance(){
  222. //int i=10;
  223. //while( i<=10 ) {
  224. // Clears the trigPin
  225. digitalWrite(trigPin, LOW);
  226. delayMicroseconds(2);
  227. // Sets the trigPin on HIGH state for 10 micro seconds
  228. digitalWrite(trigPin, HIGH);
  229. delayMicroseconds(10);
  230. digitalWrite(trigPin, LOW);
  231. // Reads the echoPin, returns the sound wave travel time in microseconds
  232. duration = pulseIn(echoPin, HIGH);
  233. // Calculating the distance
  234. distance = duration*0.034/2;
  235. //sumDistance += distance;
  236. //}
  237. //int averageDistance= sumDistance/10;
  238. return distance;
  239. }

الجمعة، 6 أكتوبر 2017

استخدام بوش بتن في اردوينو- use bush button in arduino



شاهد الفديو-watch the video

/*
  Button

int buttonState = 0;

void setup()
{
  pinMode(2, INPUT);
  pinMode(13, OUTPUT);
}

void loop()
{
  // read the state of the pushbutton value
  buttonState = digitalRead(2);
  // check if pushbutton is pressed.  if it is, the
  // buttonState is HIGH
  if (buttonState == HIGH) {
    // turn LED on
    digitalWrite(13, HIGH);
  } else {
    // turn LED off
    digitalWrite(13, LOW);
  }
  delay(10); // Delay a little bit to improve simulation performance
}

الاثنين، 2 أكتوبر 2017

neopixel arduino


شاهد الفديو-watch the video

#include <Adafruit_NeoPixel.h>

#define PIN 2 // input pin Neopixel is attached to

#define NUMPIXELS      12 // number of neopixels in Ring

Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);

int delayval = 100; // timing delay

int redColor = 0;
int greenColor = 0;
int blueColor = 0;

void setup() {
  pixels.begin(); // Initializes the NeoPixel library.
//  Serial.begin(9600);
}

void loop() {
  setColor();

  for(int i=0;i<NUMPIXELS;i++){

    // pixels.Color takes RGB values, from 0,0,0 up to 255,255,255
    pixels.setPixelColor(i, pixels.Color(redColor, greenColor, blueColor)); // Moderately bright green color.

    pixels.show(); // This sends the updated pixel color to the hardware.

    delay(delayval); // Delay for a period of time (in milliseconds).
    
    // Serial.println(i);
    
    if (i == NUMPIXELS){
    i = 0; // start all over again!
        setColor();
  }

  }
}

// setColor()
// picks random values to set for RGB

void setColor(){
  redColor = random(0, 255);
  greenColor = random(0,255);
  blueColor = random(0, 255);
  Serial.print("red: ");
  Serial.println(redColor);
  Serial.print("green: ");
  Serial.println(greenColor);
  Serial.print("blue: ");
  Serial.println(blueColor);
  
}

قياس المسافات باستخدام اردوينو - distance measuring using arduino


Watch the video above- شاهد الفديو اعلاه


 Ping))) Sensor 

// this constant won't change.  It's the pin number
// of the sensor's output:
const int pingPin = 7;

void setup() {
  // initialize serial communication:
  Serial.begin(9600);
}

void loop() {
  // establish variables for duration of the ping,
  // and the distance result in inches and centimeters:
  long duration, inches, cm;

  // The PING))) is triggered by a HIGH pulse of 2 or more microseconds.
  // Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
  pinMode(pingPin, OUTPUT);
  digitalWrite(pingPin, LOW);
  delayMicroseconds(2);
  digitalWrite(pingPin, HIGH);
  delayMicroseconds(5);
  digitalWrite(pingPin, LOW);

  // The same pin is used to read the signal from the PING))): a HIGH
  // pulse whose duration is the time (in microseconds) from the sending
  // of the ping to the reception of its echo off of an object.
  pinMode(pingPin, INPUT);
  duration = pulseIn(pingPin, HIGH);

  // convert the time into a distance
  inches = microsecondsToInches(duration);
  cm = microsecondsToCentimeters(duration);

  Serial.print(inches);
  Serial.print("in, ");
  Serial.print(cm);
  Serial.print("cm");
  Serial.println();

  delay(100);
}

long microsecondsToInches(long microseconds) {
  // According to Parallax's datasheet for the PING))), there are
  // 73.746 microseconds per inch (i.e. sound travels at 1130 feet per
  // second).  This gives the distance travelled by the ping, outbound
  // and return, so we divide by 2 to get the distance of the obstacle.
  // See: http://www.parallax.com/dl/docs/prod/acc/28015-PING-v1.3.pdf
  return microseconds / 74 / 2;
}

long microsecondsToCentimeters(long microseconds) {
  // The speed of sound is 340 m/s or 29 microseconds per centimeter.
  // The ping travels out and back, so to find the distance of the
  // object we take half of the distance travelled.
  return microseconds / 29 / 2;
}

اردوينو سيرفو موتور- arduino servo motor


Watch the video aboveشاهد الفديو اعلاه


  Sweep


#include <Servo.h>

int pos = 0;

Servo servo_9;

void setup()
{
  servo_9.attach(9);

}

void loop()
{
  // sweep the servo from 0 to 180 degrees in steps of 1 degrees
  for (pos = 0; pos <= 180; pos += 1) {
    // tell servo to go to position in variable 'pos'
    servo_9.write(pos);
    // wait 15 ms for servo to reach the position
    delay(15); // Wait for 15 millisecond(s)
  }
  for (pos = 180; pos >= 0; pos -= 1) {
    // tell servo to go to the position in variable 'pos'
    servo_9.write(pos);
    // wait 15 ms for servo to reach the position
    delay(15); // Wait for 15 millisecond(s)
  }
}