Code Temperature Arduino 1

From ccitonlinewiki
Revision as of 10:00, 22 April 2020 by Tanwir.ahmad (talk | contribs)
Jump to: navigation, search

//CODE TEMPERATURE ARDUINO 1# /*UNTUK MENGUKUR TEMPERATUR:


  • /
 #include "max6675.h"
 #include "Stepper.h"
 #define STEPS  32   // Number of steps for one revolution of Internal shaft
                   // 2048 steps for one revolution of External shaft
 #include <AccelStepper.h> //library motor stepper
 #define HALFSTEP 8        // definisi jumlah step
 //#include <Servo.h>
 //Servo servo1;
 //Servo servo2;
 volatile boolean TurnDetected;  // need volatile for Interrupts
 volatile boolean rotationdirection;  // CW or CCW rotation
 const int PinCLK=2;   // Generating interrupts using CLK signal
 const int PinDT=3;    // Reading DT signal
 const int PinSW=4;    // Reading Push Button switch
 int RotaryPosition=0;    // To store Stepper Motor Position
 int PrevPosition;     // Previous Rotary position Value to check accuracy
 int StepsToTake;      // How much to move Stepper
 // Setup of proper sequencing for Motor Driver Pins
 // In1, In2, In3, In4 in the sequence 1-3-2-4
 Stepper small_stepper(STEPS, 8, 10, 9, 11);
 // Interrupt routine runs if CLK goes from HIGH to LOW
 void isr ()  {
   delay(5);  // delay for Debouncing
   if (digitalRead(PinCLK))
     rotationdirection= digitalRead(PinDT);
   else
     rotationdirection= !digitalRead(PinDT);
   TurnDetected = true;
 }
 //T RB
 int ktcSO1 = 4;      //34;
 int ktcCS1 = 5 ;   //35;
 int ktcCLK1 = 6;    //36;
 MAX6675 ktc1(ktcCLK1, ktcCS1, ktcSO1);
 //T Reg
 int ktcSO2 = 31;
 int ktcCS2 = 32;
 int ktcCLK2 = 33;
 MAX6675 ktc2(ktcCLK2, ktcCS2, ktcSO2);
 //T riser1
 int ktcSO3 = 22;
 int ktcCS3 = 23;
 int ktcCLK3 = 24;
 MAX6675 ktc3(ktcCLK3, ktcCS3, ktcSO3);
 //T riser2
 int ktcSO4 =   46 ;    //2;
 int ktcCS4 =   47  ;   //3;
 int ktcCLK4 =  48  ;  //4;
 MAX6675 ktc4(ktcCLK4, ktcCS4, ktcSO4);
 // T riser3
 int ktcSO5 = 28;
 int ktcCS5 = 29;
 int ktcCLK5 = 30;
 MAX6675 ktc5(ktcCLK5, ktcCS5, ktcSO5);
 ///T Cyclone1
 int ktcSO6 = 37;
 int ktcCS6 = 38;
 int ktcCLK6 = 39;
 MAX6675 ktc6(ktcCLK6, ktcCS6, ktcSO6);
 //T Cyclone2
 int ktcSO7 = 40;
 int ktcCS7 = 41; 
 int ktcCLK7 = 42;
 MAX6675 ktc7(ktcCLK7, ktcCS7, ktcSO7);
 //T Cyclone3
 int ktcSO8 = 43;
 int ktcCS8 = 44;
 int ktcCLK8 = 45;
 MAX6675 ktc8(ktcCLK8, ktcCS8, ktcSO8);
 /*//T out cyclone
 int ktcSO9 = 46;
 int ktcCS9 = 47;
 int ktcCLK9 = 48;
 MAX6675 ktc9(ktcCLK9, ktcCS9, ktcSO9);
 //T in Fraksinator
 int ktcSO10 = 49;
 int ktcCS10 = 50;
 int ktcCLK10 = 51;
 MAX6675 ktc10(ktcCLK10, ktcCS10, ktcSO10);
 //T Fraksinator1
 int ktcSO11 = 2;
 int ktcCS11 = 3;
 int ktcCLK11 = 4;
 MAX6675 ktc11(ktcCLK11, ktcCS11, ktcSO11);
 //T Fraksinator2
 int ktcSO12 = 5;
 int ktcCS12 = 6;
 int ktcCLK12 = 7;
 MAX6675 ktc12(ktcCLK12, ktcCS12, ktcSO12);
 //T Fraksinator3
 int ktcSO13 = 8;
 int ktcCS13 = 9;
 int ktcCLK13 = 10;
 MAX6675 ktc13(ktcCLK13, ktcCS13, ktcSO13);
 //T Fraksinator4
 int ktcSO14 = 11;
 int ktcCS14 = 12;
 int ktcCLK14 = 13;
 MAX6675 ktc14(ktcCLK14, ktcCS14, ktcSO14);
 //T GB1
 int ktcSO15 = 13;
 int ktcCS15= 15;
 int ktcCLK15 = 16;
 MAX6675 ktc15(ktcCLK15, ktcCS15, ktcSO15);
 //T GB2
 int ktcSO16 = 17;
 int ktcCS16 = 18;
 int ktcCLK16 = 19;
 MAX6675 ktc16(ktcCLK16, ktcCS16, ktcSO16);
 //T GB3
 int ktcSO17 = A0;
 int ktcCS17 = A1;
 int ktcCLK17 = A2;
 MAX6675 ktc17(ktcCLK17, ktcCS17, ktcSO17);
 /*float TURBINE;      //pengukuran SINYAL data yang bersifat incremental
 const int HSensor = 2; //nama alias pada pin 2
 int Calc;
 */
 const int AnalogInput = A0; // Analog input where the resistor is placed
 int sensorValue = 0;
 float voltage, convert, pressure_pascal, pressure_bar, pressure_psi = 0.0;
 float t1, f1 = 0.0;
 unsigned long previousMillis, previousMillis1 = 0;
 float waktu_cuplik = 0.0;
 const long interval = 1500;
 const long interval1 = 300;
 /*
 #define motorPin1  10     // IN1 on the ULN2003 driver 1
 #define motorPin2  11     // IN2 on the ULN2003 driver 1
 #define motorPin3  12     // IN3 on the ULN2003 driver 1
 #define motorPin4  13     // IN4 on the ULN2003 driver 1
 // Initialize with pin sequence IN1-IN3-IN2-IN4 for using the AccelStepper with 28BYJ-48
 AccelStepper stepper1(HALFSTEP, motorPin1, motorPin3, motorPin2, motorPin4);
   */
 /*void speedrpm ()    //fungsi penghitungan dan interrupt
 {
 TURBINE++; //bersifat incrementing (dengan mode falling edge)
 }*/


 void setup() {
   // put your setup code here, to run once:
   Serial.begin(9600);
   pinMode(PinCLK,INPUT);
   pinMode(PinDT,INPUT);  
   pinMode(PinSW,INPUT);
   digitalWrite(PinSW, HIGH); // Pull-Up resistor for switch
   attachInterrupt (0,isr,FALLING); // interrupt 0 always connected to pin 2 on Arduino UNO
 /*  pinMode(HSensor, INPUT); //inisialisasi sebagai input
   Serial.begin(9600);
   attachInterrupt(00, speedrpm, RISING); //cara penulisan perintah interrupt
   */
   /*
   stepper1.setMaxSpeed(1000.0);
   stepper1.setAcceleration(200.0);
   stepper1.setSpeed(1000);
   //stepper1.moveTo(-15000);
   */
   /*servo1.attach(10); // Attaches servo to specified pin
   servo2.attach(6);
   servo1.write(0);
   servo2.write(0);
   */
   Serial.println("CLEARDATA");
   Serial.println("LABEL, WAKTU, Cuplik(s), T.RB, T.Reg, T.Riser1, T.Riser2, T.Riser3, T.Cyc1, T.Cyc2 , T.Cyc3");
   // Serial.println("LABEL, WAKTU, Cuplik(s), T.RB, T.Reg, T.Riser1, T.Riser2, T.Riser3, T.Cyc1, T.Cyc2 , T.Cyc3, T.outCyc, T.inFR, T.FR1, T.FR2 , T.FR3, T.FR4, T.GB1, T.GB2 ,T.GB3 ,Pressure(bar), flowrate (lpm)");
   }


 void baca_tekanan() {
   sensorValue = analogRead(AnalogInput);
   voltage = (sensorValue*5.0)/1024.0;
   convert = (3.0*(voltage-0.475));
   pressure_pascal = (3.0*(voltage-0.475))*1000000.0;
   pressure_bar = pressure_pascal/10e5;
   pressure_psi = pressure_bar*14.5038;
  }
 /*void baca_flowrate() {
   TURBINE = 00; //data awal = 0
   sei(); //perintah aktifnya mode interrupt
   delay (1000); //nilai delay 1 detik
   cli(); //perintah untuk matinya program interrupt
   float Calc = (TURBINE  / 9); //Pulsa * 60 / 7.5 
    }*/
 void loop() {

  //  Serial.print("suhu = "); 
  //  Serial.print(ktc.readCelsius()); // Printing temperature value over serial port
  //  Serial.print("\t");
  //  baca_tekanan();
  //  Serial.print(convert);
  //  Serial.print("\t");
  //  Serial.println(pressure_pascal);
  //  //Serial.print("\t");
  //  //Serial.println(presure);
  //  delay(500);


   unsigned long currentMillis = millis();
   unsigned long currentMillis1 = millis();
   waktu_cuplik = (float) currentMillis/1000;  
   //sensorValue = analogRead(A12);
   baca_tekanan();
   //baca_flowrate();
   //speedrpm();
  if (currentMillis - previousMillis == interval){
 //    Serial.print(ktc.readCelsius());
 //    Serial.print("\t");
 //    Serial.println(sensorValue);
     previousMillis = currentMillis;
     /*Serial.print(ktc1.readCelsius());
     Serial.print(",");
     Serial.println(ktc2.readCelsius());*/
     Serial.println((String) "DATA,TIME," + waktu_cuplik + "," + ktc1.readCelsius() + "," + ktc2.readCelsius() + "," + ktc3.readCelsius() + "," + ktc4.readCelsius() + "," + ktc5.readCelsius() + "," + ktc6.readCelsius() + "," + ktc7.readCelsius() + "," + ktc8.readCelsius());
     //Serial.println((String) "DATA,TIME," + waktu_cuplik + "," + ktc1.readCelsius() + "," + ktc2.readCelsius() + "," + ktc3.readCelsius() + "," + ktc4.readCelsius() + "," + ktc5.readCelsius() + "," + ktc6.readCelsius() + "," + ktc7.readCelsius() + "," + ktc8.readCelsius() + "," + ktc9.readCelsius() + "," + ktc10.readCelsius()+ "," + ktc11.readCelsius()+ "," + ktc12.readCelsius()+ "," + ktc13.readCelsius()+ "," + ktc14.readCelsius()+  "," + ktc15.readCelsius()+ "," + ktc16.readCelsius()+ "," + ktc17.readCelsius()+ "," + pressure_bar ); 

// Serial.println((String) "DATA,TIME," + waktu_cuplik + "," + ktc1.readCelsius() + "," + ktc2.readCelsius() + "," + ktc3.readCelsius() + ","+ ktc4.readCelsius() + "," + ktc5.readCelsius() + ","+ktc6.readCelsius() + "," + ktc7.readCelsius() + "," + ktc8.readCelsius()+ "," + ktc9.readCelsius() + "," + ktc10.readCelsius() + "," + pressure_bar);

      small_stepper.setSpeed(800); //Max seems to be 700
      if (!(digitalRead(PinSW))) {   // check if button is pressed
        if (RotaryPosition == 0) {  // check if button was already pressed
     }  else {
         small_stepper.step((RotaryPosition*100));
         RotaryPosition=0; // Reset position to ZERO
       }
     }
   // Runs if rotation was detected
     if (TurnDetected)  {
       PrevPosition = RotaryPosition; // Save previous position in variable
       if (rotationdirection) {
         RotaryPosition=RotaryPosition-1;} // decrase Position by 1
       else {
       RotaryPosition=RotaryPosition+1;} // increase Position by 1
       TurnDetected = false;  // do NOT repeat IF loop until new rotation detected
     // Which direction to move Stepper motor
       if ((PrevPosition + 1) == RotaryPosition) { // Move motor CW
         StepsToTake=-100; 
         small_stepper.step(StepsToTake);
     }
       if ((RotaryPosition + 1) == PrevPosition) { // Move motor CCW
         StepsToTake=100;
         small_stepper.step(StepsToTake);
     }
   }
    
    
    
    
    
    
    
    
    
    
    
    
    
      /* if(ktc1.readCelsius()  < 27)
   {  
       //servo1.write(80);
       //servo2.write(80);
 //      stepper1.moveTo(-15000);
   
   }
       else if(ktc1.readCelsius()<=30)
   {
       //servo1.write(45);
       //servo2.write(45);
   }    
       else if (ktc1.readCelsius()>30)
   {
       //servo1.write(0);
       //servo2.write(0);
   }    
        delay(1000);
        */
  }
    }