Jump to content

Coding help

Faisal A

I am making an arduino project that utilises the following parts

 

Arduino Real Time Clock

Arduino Uno

Arduino BME280

Arduino LCD (need blue with IIC)

Arduino Gas sensor

Arduino SD logger

Arduino noise sensor

Arduino light sensor

 

What I want to do is create 3 variables ; Screen 1, screen 2, screen 3

 

I want screen 1 to have the temperature, humidity and pressure

Screen 2 needs to contain flammable gases

Screen 3 needs to contain noise and light readings

 

Then screen 1 2 and 3 need to be logged to the SD card including the date and time

 

Then screen 1 must be shown on the LCD for 3 seconds. Then screen 2. Then screen 3. Then it should wait one second and repeat the cycle. Please can someone write this as arduino code for me

Edited by LogicalDrm

If you want me to see your reply, please tag me @Faisal A

Link to comment
Share on other sites

Link to post
Share on other sites

9 minutes ago, Faisal A said:

Please can someone write this as arduino code for me

for you? at most we should help you with some of the logic but this sounds like homework, and we shouldn't be doing assignments for you. What have you got to start off?

I WILL find your ITX build thread, and I WILL recommend the SIlverstone Sugo SG13B

 

Primary PC:

i7 8086k - EVGA Z370 Classified K - G.Skill Trident Z RGB - WD SN750 - Jedi Order Titan Xp - Hyper 212 Black (with RGB Riing flair) - EVGA G3 650W - dual booting Windows 10 and Linux - Black and green theme, Razer brainwashed me.

Draws 400 watts under max load, for reference.

 

How many watts do I needATX 3.0 & PCIe 5.0 spec, PSU misconceptions, protections explainedgroup reg is bad

Link to comment
Share on other sites

Link to post
Share on other sites

@Fasauceome I only know vary basic arduino stuff like digital write. This isn't homework. I can code micro python but I can't use that code in Arduino.

 

//Storing data to variables

screen_1 = (get data from BME280)

screen_2 = (get data)

screen _3 = (get data)

 

//print text to LCD

print(screen1);

delay(3000);

print(screen2);

delay(3000);

print(screen3);

delay(3000);

 

 

 

If you want me to see your reply, please tag me @Faisal A

Link to comment
Share on other sites

Link to post
Share on other sites

Step 1. Learn how to display to the screen with IIC
Step 2. Learn how to read data from all the sensors you've got there

Step 3. Collect data from the sensors in a loop and write it to the SD card

Step 4. Display that data to the LCD

 

Now you've got your app built.

Edited by BobVonBob
IIC not I2C

¯\_(ツ)_/¯

 

 

Desktop:

Intel Core i7-11700K | Noctua NH-D15S chromax.black | ASUS ROG Strix Z590-E Gaming WiFi  | 32 GB G.SKILL TridentZ 3200 MHz | ASUS TUF Gaming RTX 3080 | 1TB Samsung 980 Pro M.2 PCIe 4.0 SSD | 2TB WD Blue M.2 SATA SSD | Seasonic Focus GX-850 Fractal Design Meshify C Windows 10 Pro

 

Laptop:

HP Omen 15 | AMD Ryzen 7 5800H | 16 GB 3200 MHz | Nvidia RTX 3060 | 1 TB WD Black PCIe 3.0 SSD | 512 GB Micron PCIe 3.0 SSD | Windows 11

Link to comment
Share on other sites

Link to post
Share on other sites

@BobVonBob I know how to display screen with iic and how to read data from the sensors. I need help with the variables and serial.print

If you want me to see your reply, please tag me @Faisal A

Link to comment
Share on other sites

Link to post
Share on other sites

Here's how i would do it.

 

Set a timer that overflows every second or every 100ms. Trigger an interrupt every time it overflows.

In the interrupt, increment a global variable and reset the timer.

If that variable reaches exceeds a certain value, for example 3 (seconds) x 10 updates (if timer at 100ms) = 30, then screen needs to change.

Let's say you have a global variable called ShowScreen ... that's gonna be 0, 1 or 2. If ShowScreen is 2 you reset it to 0 otherwise you add 1 to the variable.

Reset the variable that counts the 100ms steps back to 0

 

In the main loop you're gonna have two variables, the showScreen from above and another variable previousScreen.

If the two don't match, you know it's time to put new data on the screen. You put the data on screen and set the previousScreen to the showScreen value so they'll match, this way you won't update the screen as you go through the loop

In the same loop, you can put there measurements for those things you want to display and save the values in some variables.

 

As for how to actually print a number to a screen, you have to convert the digits of the number to characters.

It helps to know the ASCII code : http://www.asciitable.com/

For example, let's say you want to print an unsigned 16 bit number (0..65535) to the screen but you don't want to use printf or something that uses lots of space in the microcontroller.

you can do it like this
 

printed = false; // i use this as a flag... becomes true at first non-zero. ex if number is 15, don't print 00015 on screen.
stepSize = 10000;

for (step = 0;step < 5; step ++) { //  10000, 1000, 100, 10, 1
	digit = 0;
    while (value > stepSize) { digit++; value = value-stepSize }
    if ((digit==0) && (printed==true) ) || (digit !=0) {  // print charactes "0" = 0x30 ... "9" = 0x39
        byteToSend = 0x30 + digit; 
        printed = true; // we sent at least one non-zero digit, from now must print zeroes
    }
    digit = 0;
    stepSize = stepSize / 10;  // 10000 becomes 1000, then 1000 becomes 100 and so on until 1
}

You can modify such a function to work with negative numbers (check if first bit is 1, if so print "-" , clear the bit and substract value from 32768 

ex -100 =  1111 1111 1001 1100 , print - and clear the bit -> 0111 1111 1001 1100 = 32,668  ... 32768 - 32668 = 100

You can also implement the dot for floating point.. just multiply a number like 10.5 by 100 and send 1050 to your print function with the instruction to put dot when stepsize becomes 10

 

Link to comment
Share on other sites

Link to post
Share on other sites

@mariushm I have an IIC adapter connected to the screen so will this code still work and what libaries do i need to include. 

If you want me to see your reply, please tag me @Faisal A

Link to comment
Share on other sites

Link to post
Share on other sites

doesn't matter what you have, code above will work with anything (after you write it correctly as i wrote it straight in forum without error checking so it probably won't work directly if you copy-paste)

 

Basically, your i2c lcd display, or SPI display, or parallel display will have some commands like move cursor to x,y , put character/byte at this cursor position etc

 

So for example, if you need to print a 2 byte number like I showed above, you simply have to send the command to position cursor at the x,y you want on the screen,

then go through the digits like I showed above and where you see that 

byteToSend = 0x30 + digit;

you simply have to add the command to the lcd display controller (print at current position byte value byteToSend)

 

There are some libraries for Arduino for various LCD controllers ... i don't know what's best as i use mainly PIC microcontrollers.

Maybe see this one : https://www.arduinolibraries.info/libraries/liquid-crystal-i2-c

It claims it works with DFRobot I2C lcd displays but probably lots of lcd displays will use the same command set.

with the above library, you'd initialize it with the i2c address of the lcd display and the number of rows and columns and then you have commands , scroll down to see the setcursor and write commands.

class LiquidCrystal_I2C : public Print {
public:
  LiquidCrystal_I2C(uint8_t lcd_Addr,uint8_t lcd_cols,uint8_t lcd_rows);
  void begin(uint8_t cols, uint8_t rows, uint8_t charsize = LCD_5x8DOTS );
  void clear();  <------ clear display
  void home();   <------ reset cursor to 0,0 
  void noDisplay();
  void display();
  void noBlink();  <----- make character at cursor position blink or not
  void blink();   <--/
  void noCursor();  <---- show/hide cursor (underline on character)
  void cursor();  <--/
  void scrollDisplayLeft();
  void scrollDisplayRight();
  void printLeft();
  void printRight();
  void leftToRight();
  void rightToLeft();
  void shiftIncrement();
  void shiftDecrement();
  void noBacklight();
  void backlight();
  void autoscroll();
  void noAutoscroll();
  void createChar(uint8_t, uint8_t[]);
  void setCursor(uint8_t, uint8_t);      <------- put cursor (even if invisible) at x,y
#if defined(ARDUINO) && ARDUINO >= 100
  virtual size_t write(uint8_t);         <------- write byte at cursor position (cursor autoincrements)
#else
  virtual void write(uint8_t);         
#endif
  void command(uint8_t);
  void init();

 

 

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×