hendriawan

Test board modul wsn baru

Feb 5th, 2019
620
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include<Wire.h>
  2. const int MPU_addr = 0x68; // I2C address of the MPU-6050
  3. int16_t AcX, AcY, AcZ, Tmp, GyX, GyY, GyZ;
  4.  
  5. #include <Adafruit_GFX.h>
  6. #include <Adafruit_SSD1306.h>
  7. #define OLED_RESET 23
  8. Adafruit_SSD1306 display(OLED_RESET);
  9.  
  10. #include <Wire.h>
  11. #include <Adafruit_Sensor.h>
  12. #include <Adafruit_BME280.h>
  13. #define SEALEVELPRESSURE_HPA (1013.25)
  14.  
  15.  
  16. #define Buzzer A1
  17. #define LED1 22 //dekat xbee
  18. #define LED2 24
  19. #define LED3 26
  20. #define LED4 28
  21. #define LED5 30
  22. #define LED6 32
  23. #define LED7 34
  24. #define LED8 36
  25. #define LED9 38
  26. #define LED10 40
  27.  
  28.  
  29. #define btnDown 2
  30. #define btnRight 3
  31. #define btnUp 4
  32. #define btnLeft 5
  33. #define btnCenter 6
  34.  
  35. #define sw1 A2 //dekat header
  36. #define sw2 A3
  37. #define sw3 A4
  38. #define sw4 A5
  39. const int
  40. PWM_A   = 9,
  41. PWM_B   = 12,
  42. DIR_A1   = 7,
  43. DIR_A2   = 8,
  44. DIR_B1   = 10,
  45. DIR_B2   = 11;
  46.  
  47. int  barLED[] = {22, 24, 26, 28, 30, 32, 34, 36, 38, 40};
  48.  
  49. int mspeed;
  50. // prototipe function here
  51. void testBuzzer(void);
  52. void GPIOinit(void);
  53. void testBarLED(void);
  54. void OLEDinit(void);
  55. void printBME280Value(void);
  56.  
  57.  
  58. Adafruit_BME280 bme; // I2C
  59.  
  60. void setup()
  61. {
  62.   GPIOinit();
  63.   OLEDinit();
  64.     // by default, we'll generate the high voltage from the 3.3v line internally! (neat!)
  65.   Serial.begin(9600);
  66.   Serial.println("1-Test buzzer:\n");
  67.   Serial.println("Please Listen the test  manually:\n");
  68.   display.setCursor(0,10);
  69.   display.println("Please use Serial ");
  70.   display.setCursor(0,20);
  71.   display.println("Port Terminal");
  72.   display.display();
  73.   testBuzzer();
  74.   Serial.println("1-Test BarLED:\n");
  75.   Serial.println("Please see the result manually:\n");
  76.   testBarLED();
  77.  
  78.   Serial.println("2-Motor shield DC motor Test and Btn:\n");
  79.   //gy-88 init***
  80.   Wire.begin();
  81.   Wire.beginTransmission(MPU_addr);
  82.   Wire.write(0x6B);  // PWR_MGMT_1 register
  83.   Wire.write(0);     // set to zero (wakes up the MPU-6050)
  84.   Wire.endTransmission(true);
  85.   //gy-88 end of init
  86.  
  87.     bool status;
  88.     status = bme.begin();  
  89.     if (!status) {
  90.         Serial.println("Could not find a valid BME280 sensor, check wiring!");
  91.         while (1);
  92.     }
  93.    delay(100); // let sensor boot up  
  94.    printBME280Value();
  95. }
  96.  
  97. // the loop function runs over and over again forever
  98. void loop()
  99. {
  100.  
  101.   mspeed = analogRead(A0);
  102.   mspeed = map(mspeed, 0, 705, 0, 255); //map the pot. output in range of 0-255
  103.   Serial.println(mspeed);
  104.  
  105.   digitalWrite(DIR_A1, HIGH);
  106.   //  digitalWrite(DIR_A2, LOW);
  107.   //  analogWrite(PWM_A, mspeed);
  108.   //
  109.   //  digitalWrite(DIR_B1, HIGH);
  110.   //  digitalWrite(DIR_B2, LOW);
  111.   //  analogWrite(PWM_B, mspeed);
  112.  
  113.   if (digitalRead(btnDown) == LOW)
  114.     digitalWrite(LED1, HIGH);
  115.   else
  116.     digitalWrite(LED1, LOW);
  117.    
  118.   if (digitalRead(btnRight) == LOW)
  119.     digitalWrite(LED2, HIGH);
  120.   else
  121.     digitalWrite(LED2, LOW);
  122.  
  123.   if (digitalRead(btnUp) == LOW)
  124.     digitalWrite(LED3, HIGH);
  125.   else
  126.     digitalWrite(LED3, LOW);
  127.  
  128.   if (digitalRead(btnLeft) == LOW)
  129.     digitalWrite(LED4, HIGH);
  130.   else
  131.     digitalWrite(LED4, LOW);
  132.  
  133.   if (digitalRead(btnCenter) == LOW)
  134.     digitalWrite(LED5, HIGH);
  135.   else
  136.     digitalWrite(LED5, LOW);
  137.  
  138.   if (digitalRead(sw1) == LOW)
  139.     digitalWrite(LED6, HIGH);
  140.   else
  141.     digitalWrite(LED6, LOW);
  142.  
  143.   if (digitalRead(sw2) == LOW)
  144.     digitalWrite(LED7, HIGH);
  145.   else
  146.     digitalWrite(LED7, LOW);
  147.  
  148.   if (digitalRead(sw3) == LOW)
  149.     digitalWrite(LED8, HIGH);
  150.   else
  151.     digitalWrite(LED8, LOW);
  152.  
  153.   if (digitalRead(sw4) == LOW)
  154.     digitalWrite(Buzzer, HIGH);
  155.   else
  156.     digitalWrite(Buzzer, LOW);
  157.  
  158.   //gy-88 test***
  159.   Wire.beginTransmission(MPU_addr);
  160.   Wire.write(0x3B);  // starting with register 0x3B (ACCEL_XOUT_H)
  161.   Wire.endTransmission(false);
  162.   Wire.requestFrom(MPU_addr, 14, true); // request a total of 14 registers
  163.   AcX = Wire.read() << 8 | Wire.read(); // 0x3B (ACCEL_XOUT_H) & 0x3C (ACCEL_XOUT_L)
  164.   AcY = Wire.read() << 8 | Wire.read(); // 0x3D (ACCEL_YOUT_H) & 0x3E (ACCEL_YOUT_L)
  165.   AcZ = Wire.read() << 8 | Wire.read(); // 0x3F (ACCEL_ZOUT_H) & 0x40 (ACCEL_ZOUT_L)
  166.   Tmp = Wire.read() << 8 | Wire.read(); // 0x41 (TEMP_OUT_H) & 0x42 (TEMP_OUT_L)
  167.   GyX = Wire.read() << 8 | Wire.read(); // 0x43 (GYRO_XOUT_H) & 0x44 (GYRO_XOUT_L)
  168.   GyY = Wire.read() << 8 | Wire.read(); // 0x45 (GYRO_YOUT_H) & 0x46 (GYRO_YOUT_L)
  169.   GyZ = Wire.read() << 8 | Wire.read(); // 0x47 (GYRO_ZOUT_H) & 0x48 (GYRO_ZOUT_L)
  170.   Serial.print("AcX = ");
  171.   Serial.print(AcX);
  172.   Serial.print(" | AcY = ");
  173.   Serial.print(AcY);
  174.   Serial.print(" | AcZ = ");
  175.   Serial.print(AcZ);
  176.   Serial.print(" | Tmp = ");
  177.   Serial.print(Tmp / 340.00 + 36.53); //equation for temperature in degrees C from datasheet
  178.   Serial.print(" | GyX = ");
  179.   Serial.print(GyX);
  180.   Serial.print(" | GyY = ");
  181.   Serial.print(GyY);
  182.   Serial.print(" | GyZ = ");
  183.   Serial.println(GyZ);
  184.   delay(333);
  185.   //end test of gy-88
  186. }
  187.  
  188. void GPIOinit(void)
  189. {
  190.   for (int i = 0; i < 10; i++) {
  191.     pinMode(barLED[i], OUTPUT);
  192.   }
  193.   pinMode(Buzzer , OUTPUT);
  194.   pinMode(btnDown, INPUT_PULLUP);
  195.   pinMode(btnRight, INPUT_PULLUP);
  196.   pinMode(btnUp, INPUT_PULLUP);
  197.   pinMode(btnLeft, INPUT_PULLUP);
  198.   pinMode(btnCenter, INPUT_PULLUP);
  199.   pinMode(sw1, INPUT_PULLUP);
  200.   pinMode(sw2, INPUT_PULLUP);
  201.   pinMode(sw3, INPUT_PULLUP);
  202.   pinMode(sw4, INPUT_PULLUP);
  203.  
  204.   pinMode(DIR_A1, OUTPUT);
  205.   pinMode(DIR_A2, OUTPUT);
  206.   pinMode(DIR_B1, OUTPUT);
  207.   pinMode(DIR_B2, OUTPUT);
  208. }
  209.  
  210. void OLEDinit(void){
  211.   display.begin(SSD1306_SWITCHCAPVCC, 0x3C);  // initialize with the I2C addr 0x3D (for the 128x64)
  212.   display.clearDisplay();
  213.   display.setTextSize(1);
  214.   display.setTextColor(WHITE);
  215.   // init done
  216.  
  217.   display.setCursor(0,0);
  218.   display.println("Demo WSN Board v1.0!");
  219.   display.display();
  220.   delay(300);
  221. }
  222.  
  223. void testBuzzer(void)
  224. {
  225.   for (int i = 0; i < 5; i++) {
  226.     digitalWrite(Buzzer, HIGH);   // turn the LED on (HIGH is the voltage level)
  227.     delay(100);              // wait for a second
  228.     digitalWrite(Buzzer , LOW);    // turn the LED off by making the voltage LOW
  229.     delay(100);              // wait for a second
  230.   }
  231. }
  232.  
  233. void testBarLED(void) {
  234.   for (int i = 0; i < 3; i++)   {
  235.     for (int i = 0; i < 10; i++) {    
  236.       digitalWrite(barLED[i], HIGH);
  237.       delay(300);
  238.     }
  239.     for (int i = 0; i < 10; i++) {
  240.       digitalWrite(barLED[i], LOW);
  241.     }
  242.   }
  243. }
  244.  
  245. void printBME280Value(void) {
  246.       Serial.println(F("BME280 test"));
  247.     Serial.print("Temperature = ");
  248.     Serial.print(bme.readTemperature());
  249.     Serial.println(" *C");
  250.  
  251.     Serial.print("Pressure = ");
  252.  
  253.     Serial.print(bme.readPressure() / 100.0F);
  254.     Serial.println(" hPa");
  255.  
  256.     Serial.print("Approx. Altitude = ");
  257.     Serial.print(bme.readAltitude(SEALEVELPRESSURE_HPA));
  258.     Serial.println(" m");
  259.  
  260.     Serial.print("Humidity = ");
  261.     Serial.print(bme.readHumidity());
  262.     Serial.println(" %");
  263.  
  264.     Serial.println();
  265. }
Advertisement
Add Comment
Please, Sign In to add comment