Hello all,
I have a message in a display that tells me if the device is off.
Let me give you some more info.
I have a Caliper (measure device) connected to a display.
When the caliper is off i let it know in the display with the text measure device is off.
Know i wanted to know if there is another way to do this?
Because i used the ("int decimal = 0;") for it.
If decimal = 0 i know the device is off and when it is 1 or higher i know the device is on.
And if i turn of the device it wont go back to the message device is off.
And another problem with this is in If statement it works only when i turn on the device.
And in while statement not but i need while because i need the display to give more text message i need the loop.
I know i use delay i am trying to use mills but i need to know hou i can blink a Neopixel.
But thats another task for me.
First i want to know how i get a clean solution for turn on and off device text message.
Please go easy on me this i am a noob,
The message off code:
while(decimal == 0){
display.clearDisplay();
u8g2_for_adafruit_gfx.setFont(u8g2_font_helvR14_tf);
u8g2_for_adafruit_gfx.setCursor(25,30);
u8g2_for_adafruit_gfx.print(F("Meetklok"));
u8g2_for_adafruit_gfx.setCursor(20,50);
u8g2_for_adafruit_gfx.print(F("aan zetten."));
display.display();
delay(2000);
display.clearDisplay();
u8g2_for_adafruit_gfx.setFont(u8g2_font_helvR14_tf);
u8g2_for_adafruit_gfx.setCursor(25,30);
u8g2_for_adafruit_gfx.print(F("Druk op de"));
u8g2_for_adafruit_gfx.setCursor(20,50);
u8g2_for_adafruit_gfx.print(F("groene knop."));
display.display();
delay(2000);
strip.setPixelColor(0, 255, 0, 0);
strip.show();
delay(500);
strip.setPixelColor(0, 0, 0, 0);
strip.show();
delay(500);
}//while
The hole code:
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <U8g2_for_Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <Adafruit_NeoPixel.h>
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
//define NeoPixel Pin and Number of LEDs
#define PIN 12
#define NUM_LEDS 1
//create a NeoPixel strip
Adafruit_NeoPixel strip = Adafruit_NeoPixel(NUM_LEDS, PIN, NEO_GRB + NEO_KHZ800);
// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
#define OLED_RESET 4 // Reset pin # (or -1 if sharing Arduino reset pin)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
U8G2_FOR_ADAFRUIT_GFX u8g2_for_adafruit_gfx;
//bar defines
#define MID_BAR_RESULT 1.85f //mm Nominale maat
#define DELTA_BAR_RESULT 0.10f //mm + en - vanuit het midden naar hoog en laag
#define MIN_BAR_RESULT (MID_BAR_RESULT - DELTA_BAR_RESULT) //mm min scale reading (bar at bottom of rectangle)
#define MAX_BAR_RESULT (MID_BAR_RESULT + DELTA_BAR_RESULT) //mm full scale reading (bar at top of rectangle)
//
#define MAX_BAR_Y 1 //pixel Y # for bar max reading
#define MIN_BAR_Y 63 //pixel Y # for bar min reading
#define BAR_Y_SPAN (MIN_BAR_Y - MAX_BAR_Y) //pixel active Y span of rectangle
#define BAR_SCALE ((float)BAR_Y_SPAN/(MAX_BAR_RESULT-MIN_BAR_RESULT)) //pixel/mm scale term
int req = 5; //mic REQ line goes to pin 5 through q1 (arduino high pulls request line low)
int dat = 6; //mic Data line goes to pin 2
int clk = 7; //mic Clock line goes to pin 3
int i = 0;
int j = 0;
int k = 0;
int signCh = 8;
int sign = 0;
int decimal = 0;
float dpp;
int units;
byte mydata[14];
String value_str;
long value_int; //was an int, could not measure over 32mm
float value;
uint8_t newBarY;
uint8_t lastBarY = MIN_BAR_Y;
void setup() {
Serial.begin(9600);
delay(1000);
//pinMode (12, OUTPUT);
strip.begin();
strip.show();
display.begin(SSD1306_SWITCHCAPVCC, 0x3D);
u8g2_for_adafruit_gfx.begin(display);
pinMode(req, OUTPUT);
pinMode(clk, INPUT_PULLUP);
pinMode(dat, INPUT_PULLUP);
digitalWrite(req,LOW); // set request at high
display.clearDisplay();
u8g2_for_adafruit_gfx.setForegroundColor(WHITE);
u8g2_for_adafruit_gfx.setFont(u8g2_font_helvR14_tf);
u8g2_for_adafruit_gfx.setCursor(30,30);
u8g2_for_adafruit_gfx.print(F("Laden..."));
display.display();
// Neopixel collor test
strip.setPixelColor(0, 255, 0, 0);
strip.show();
delay(500);
strip.setPixelColor(0, 0, 255, 0);
strip.show();
delay(500);
strip.setPixelColor(0, 0, 0, 255);
strip.show();
delay(1000);
}
void loop() {
//Code device off.
while(decimal == 0){
display.clearDisplay();
u8g2_for_adafruit_gfx.setFont(u8g2_font_helvR14_tf);
u8g2_for_adafruit_gfx.setCursor(25,30);
u8g2_for_adafruit_gfx.print(F("klok off"));
display.display();
delay(2000);
display.clearDisplay();
u8g2_for_adafruit_gfx.setFont(u8g2_font_helvR14_tf);
u8g2_for_adafruit_gfx.setCursor(25,30);
u8g2_for_adafruit_gfx.print(F("Please turn on."));
display.display();
delay(2000);
//Indicate light
strip.setPixelColor(0, 255, 0, 0);
strip.show();
delay(500);
strip.setPixelColor(0, 0, 0, 0);
strip.show();
delay(500);
}//while
if ( (value <= 1.74) && (value > 0.05)){
display.clearDisplay();
u8g2_for_adafruit_gfx.setFont(u8g2_font_helvR14_tf);
u8g2_for_adafruit_gfx.setCursor(35,20);
u8g2_for_adafruit_gfx.print(F("Low"));
display.display();
}
else if(value >= 1.96){
display.clearDisplay();
u8g2_for_adafruit_gfx.setFont(u8g2_font_helvR14_tf);
u8g2_for_adafruit_gfx.setCursor(35,20);
u8g2_for_adafruit_gfx.print(F("High"));
display.display();
}
else{
display.clearDisplay();
display.setTextColor(WHITE);
display.setTextSize(3);
display.setCursor(15, 25);
display.print(value,decimal);
display.print("");
if ( value <= MIN_BAR_RESULT )
newBarY = MIN_BAR_Y;
else if ( value >= MAX_BAR_RESULT )
newBarY = MAX_BAR_Y;
else
newBarY = (uint8_t)((MIN_BAR_Y - ((value - MIN_BAR_RESULT) * BAR_SCALE)) + 0.01);
//Serial.print( "Measurement \t" ); Serial.print(result); Serial.print( "\tY bar \t" ); Serial.println( newBarY );
//if( newBarY != lastBarY ) //would be used when clearDisplay not used
//{
display.drawRect( 0, 0, 7, 64, WHITE );// bar on the left
display.drawLine(6, 32, 0, 32, WHITE);// bar on the left center line.
//display.drawLine( 4, lastBarY, 0, lastBarY, BLACK );//would be used when clearDisplay not used
display.drawFastHLine( 0, newBarY, 6, WHITE );
DrawBar( newBarY );
//lastBarY = newBarY; //would be used when clearDisplay not used
//}//if
display.display();
}//End if
digitalWrite(req, HIGH); // generate set request
for( i = 0; i < 13; i++ ) {
k = 0;
for (j = 0; j < 4; j++) {
while( digitalRead(clk) == LOW) {
} // hold until clock is high
while( digitalRead(clk) == HIGH) {
} // hold until clock is low
bitWrite(k, j, (digitalRead(dat) & 0x1));
}
mydata[i] = k;
}
sign = mydata[4];
value_str = String(mydata[5]) + String(mydata[6]) + String(mydata[7]) + String(mydata[8] + String(mydata[9] + String(mydata[10]))) ;
decimal = mydata[11];
units = mydata[12];
value_int = value_str.toInt();
if (decimal == 0) dpp = 1.0;
if (decimal == 1) dpp = 10.0;
if (decimal == 2) dpp = 100.0;
if (decimal == 3) dpp = 1000.0;
if (decimal == 4) dpp = 10000.0;
if (decimal == 5) dpp = 100000.0;
value = value_int / dpp;
digitalWrite(req,LOW);
delay(100);
}
//Drawbar
void DrawBar( uint8_t newBarY ){
if ( newBarY == 32 )
display.drawFastHLine( 0, newBarY, 5, WHITE );
else
{
if ( newBarY < 32 )
display.fillRect(1, newBarY, 6, 32 - newBarY, WHITE );
else
display.fillRect(1, 32, 6, newBarY - 32, WHITE );
}//else
}//DrawBar