Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ******************************************************************
- Project name : Nozzle machine
- Author :
- Date Time :
- Description :
- CPU BOARD :
- URL : www.plc-expert.com
- ******************************************************************/
- //#include "EmonLib.h"
- #include <Arduino.h>
- #include <U8g2lib.h>
- #include <SPI.h>
- #include <Wire.h>
- #define DEF_VERSION "Nozzle 1.0"
- #define DEF_PIN_LAMP_RED 8 // All currents are > DEF_CURRENT_MIN && any < DEF_CURRENT_OK
- #define DEF_PIN_LAMP_ORANGE 9 // All currents are < DEF_CURRENT_MIN
- #define DEF_PIN_LAMP_GREEN 10 // All currents are > DEF_CURRENT_OK
- #define DEF_PIN_ALARM 11 // Alarm state = RED LAMP STATE
- #define DEF_CALIBRATION_OFFSET 30
- #define DEF_CURRENT_MIN 0.12
- #define DEF_CURRENT_OK 0.20
- // Display initialization
- U8G2_SSD1306_128X32_UNIVISION_F_HW_I2C u8g2(U8G2_R0);
- float currents[4] = {0,0,0,0};
- // Initialize Emonitor
- //EnergyMonitor monitors[4];
- void setup()
- {
- pinMode(DEF_PIN_LAMP_RED, OUTPUT);
- pinMode(DEF_PIN_LAMP_ORANGE, OUTPUT);
- pinMode(DEF_PIN_LAMP_GREEN, OUTPUT);
- pinMode(DEF_PIN_ALARM, OUTPUT);
- Serial.begin(9600);
- while (!Serial){
- delay(50);
- }
- Serial.println(DEF_VERSION);
- u8g2.begin();
- // read values on startup
- for(int n=0; n<4; n++){
- // Refresh values using parameters : Analog input pin number, calibration value.
- //monitors[n].current(DEF_PIN_ANALOG_INPUTS[n], DEF_CALIBRATION_OFFSET);
- }
- }
- void loop(){
- for(int n=0; n<4; n++){
- //currents[n] = monitors[n].calcIrms(1674); // Calculate Irms only (1674... n/50*5580.. orig 1480)
- }
- SendDataToSerialPort();
- ShowValuesOnDisplay();
- bool motors_are_on = AllMotorsAreOn();
- bool currents_are_ok = AllCurrentsAreOk();
- bool red = motors_are_on && !currents_are_ok;
- bool green = motors_are_on && currents_are_ok;
- bool orange = !motors_are_on;
- SetOutputs(red, orange, green);
- delay(500);
- }
- bool AllMotorsAreOn(){
- bool all_motors_are_on = false;
- for(int n=0; n<4; n++){
- all_motors_are_on = currents[n] > DEF_CURRENT_MIN;
- if(!all_motors_are_on) break;
- }
- return all_motors_are_on;
- }
- bool AllCurrentsAreOk(){
- if(!AllMotorsAreOn()) return false;
- bool currents_are_on = false;
- for(int n=0; n<4; n++){
- currents_are_on = currents[n] > DEF_CURRENT_OK;
- if(!currents_are_on) break;
- }
- return currents_are_on;
- }
- void SendDataToSerialPort(){
- String jsonData = "{\"I1\":";
- jsonData += currents[0];
- jsonData += ",\"I2\":";
- jsonData += currents[1];
- jsonData += ",\"I3\":";
- jsonData += currents[2];
- jsonData += ",\"I4\":";
- jsonData += currents[3];
- jsonData += "}";
- Serial.println(jsonData);
- }
- void ShowValuesOnDisplay(){
- // u8g2.clearBuffer(); // clear the internal memory
- // u8g2.setFont(u8g2_font_logisoso28_tr); // choose a suitable font at https://github.com/olikraus/u8g2/wiki/fntlistall
- // u8g2.drawStr(8,29,"Irms="); // write something to the internal memory
- // u8g2.sendBuffer(); // transfer internal memory to the display
- //delay(1000);
- // for(int n=0; n<4; n++){
- // u8g2.clearBuffer(); // clear the internal memory
- // u8g2.setFont(u8g2_font_logisoso22_tr); // choose a suitable font at https://github.com/olikraus/u8g2/wiki/fntlistall
- // u8g2.setCursor(8, 29);
- // u8g2.print("K");
- // u8g2.print(n+1);
- // u8g2.print(":");
- // u8g2.print(currents[n]);
- // u8g2.print(" A");
- // u8g2.sendBuffer();
- // delay (1000);
- // }
- }
- void SetOutputs(bool Red, bool Orange, bool Green){
- digitalWrite(DEF_PIN_ALARM, Red);
- digitalWrite(DEF_PIN_LAMP_RED, Red);
- digitalWrite(DEF_PIN_LAMP_ORANGE, Orange);
- digitalWrite(DEF_PIN_LAMP_GREEN, Green);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement