Is it possible to digitalWrite and lcd.print at the same time?

Hi, I am doing a project using bitvoicer, Mega2560, 16 relay card and a lcd screen.

What i would like to happen is bitvoicer sends int eg( 35) this turns on one of my relays and prints to screen to say "relay 16 ON"

This is part of my code to show a example of how i would like it to work, but the screen, doesnt print if the relay is made high/low. If i remove the relay line of code it works fine. i have tried to put delays in places but all it does is slow the relay coming on or it comes on without signal from bitvoicer

if (bvSerial.intData == 35)

relay16 = HIGH;

if (bvSerial.intData == 35) lcd.clear ();

if (bvSerial.intData == 35) lcd.print("RELAY 16 ON");

Thanks in advance

Jamie993: a project using bitvoicer, Mega2560, 16 relay card and a lcd screen.

If you use specific items like that a link to them is the least you can do. Not a clue what bitvoicer is

And next time, use code tags. Or even better, please read How to use this forum before you continue. [edit]No, insert code tags first...

An to get to you're problem, why isn't it all in one if? Why check that value three times? My guess is the value isn't the same each time you read it ;) It's like a deck of cards, you receive a card, read it, throw it away an then you try to read a card again.

This is complete nonsense. You write the pin number to the pin:

int relay1 = 31;
[...]
digitalWrite(31, relay1);

You didn’t use code tags to post your code. It’s important that you use them - if you don’t, the forum software sometimes interprets sequences of characters in the code as directives to format text in some way. It won’t display those sequences, and it will unexpectedly reformat the rest of the text. When that happens, and someone tries to copy your code and paste it into the IDE, it often throws an error, and readers will complain that the code fails to compile. Using code tags also makes it easier to read, and can be copied with a single mouse click.

Unless the sketch is too large, it’s better if you post your code, rather than attach it. When it’s attached, we have to download it, open it in a text editor, then copy it to the IDE. It’s much easier to just view the code in your post.

The code tags make the code look

like this

when posting source code files.
If you have already posted without using code tags, open your message and select “modify” from the pull down menu labelled, “More”, at the lower left corner of the message. Before posting the code, use Ctrl-T in the IDE to reformat the code in a standard format, which makes it easier for us to read. Highlight your code by selecting it (it turns blue), and then click on the “</>” icon at the upper left hand corner. Click on the “Save” button. Code tags can also be inserted manually in the forum text using the [code] and [/code] metatags.

When you are finished that, please read this post:

How to use this forum - please read.

Sorry for not know how the forum works, first post. Are you saying i can put all of the outputs under one IF? I have not tried that, still pretty new. Bitvoicer, http://www.bitsophia.com/en-US/Home.aspx Is the software for voice recognition which sends different numbers to connected arduino depending on what i say. Its pretty fun.

That's why somebody made the effort to tell you all about this forum. All you had to do was click the big fat How to use this forum and read it ;)

And yes, otherwise code would be terrible and some things even impossible.

if(this){
  doThis();
  andThis();
  andMakeThis = 5;
}
else{
  otherStuff();
}

I have tried the multiple outputs under one IF, it doesnt work, only the first output comes on. Is it necessary to use else statements on this code?

No, the else is not mandetory. But if you made/changed code, why not share that with us, the proper way?

[

//Imports the BitVoicer library to the sketch
#include <BitVoicer11.h>
#include <LiquidCrystal.h>

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

//Instantiates the BitVoicerSerial class
BitVoicerSerial bvSerial = BitVoicerSerial();

//Stores the data type retrieved by getData()
int dataType = 0;

//Stores the state of pins
int relay1 = 31;
int relay2 = 22;
int relay3 = 33;
int relay4 = 24;
int relay5 = 35;
int relay6 = 26;
int relay7 = 37;
int relay8 = 28;
int relay9 = 39;
int relay10 = 30;
int relay11 = 41;
int relay12 = 32;
int relay13 = 43;
int relay14 = 34;
int relay15 = 15;
int relay16 = 36;


void setup()
{
lcd.begin(16, 2);
  
//Starts serial communication at 9600 bps
Serial.begin(9600);

//Sets digital pins as OUTPUT

pinMode(22, OUTPUT);
pinMode(24, OUTPUT);
pinMode(26, OUTPUT);
pinMode(28, OUTPUT);
pinMode(30, OUTPUT);
pinMode(32, OUTPUT);
pinMode(34, OUTPUT);
pinMode(36, OUTPUT);
pinMode(31, OUTPUT);
pinMode(33, OUTPUT);
pinMode(35, OUTPUT);
pinMode(37, OUTPUT);
pinMode(39, OUTPUT);
pinMode(41, OUTPUT);
pinMode(43, OUTPUT);
pinMode(45, OUTPUT);
}
void loop()
{
//Updates the state of pins on every loop
digitalWrite(31, relay1);
digitalWrite(22, relay2);
digitalWrite(33, relay3);
digitalWrite(24, relay4);
digitalWrite(35, relay5);
digitalWrite(26, relay6);
digitalWrite(37, relay7);
digitalWrite(28, relay8);
digitalWrite(39, relay9);
digitalWrite(30, relay10);
digitalWrite(41, relay11);
digitalWrite(32, relay12);
digitalWrite(43, relay13);
digitalWrite(34, relay14);
digitalWrite(45, relay15);
digitalWrite(36, relay16);

}

//This function runs every time serial data is available
//in the serial buffer after a loop
void serialEvent()
{
//Reads the serial buffer and stores the received data type
dataType = bvSerial.getData();
//Checks if the data type is the same as the one in the
//Voice Schema
if (dataType == BV_INT)

// commands from bitvoicer are numbered.

if (bvSerial.intData == 0)
relay1 = LOW;
if (bvSerial.intData == 1)
relay1 = HIGH;

if (bvSerial.intData == 2)
relay2 = LOW;
if (bvSerial.intData == 3)
relay2 = HIGH;

if (bvSerial.intData == 4)
relay3 = LOW;
if (bvSerial.intData == 5)
relay3= HIGH;

if (bvSerial.intData == 6)
relay4 = LOW;
if (bvSerial.intData == 7)
relay4 = HIGH;

if (bvSerial.intData == 8)
relay5 = LOW;
if (bvSerial.intData == 9)
relay5 = HIGH;

if (bvSerial.intData == 10)
relay6 = LOW;
if (bvSerial.intData == 11)
relay6 = HIGH;

if (bvSerial.intData == 12)
relay7 = LOW;
if (bvSerial.intData == 13)
relay7 = HIGH;

if (bvSerial.intData == 14)
relay8 = LOW;
if (bvSerial.intData == 15)
relay8 = HIGH;

if (bvSerial.intData == 16)
relay9 = LOW;
if (bvSerial.intData == 17)
relay9 = HIGH;

if (bvSerial.intData == 18)
relay10 = LOW;
if (bvSerial.intData == 19)
relay10 = HIGH;

if (bvSerial.intData == 20)
relay11 = LOW;
if (bvSerial.intData == 21)
relay11 = HIGH;

if (bvSerial.intData == 22)
relay12 = LOW;
if (bvSerial.intData == 23)
relay12 = HIGH;

if (bvSerial.intData == 24)
relay13 = LOW;
if (bvSerial.intData == 25)
relay13 = HIGH;

if (bvSerial.intData == 26)
relay14 = LOW;
if (bvSerial.intData == 27)
relay14 = HIGH;

if (bvSerial.intData == 28)
relay15 = LOW;
if (bvSerial.intData == 29)
relay15 = HIGH;

if (bvSerial.intData == 30)
relay16 = LOW;
if (bvSerial.intData == 31)
relay16 = HIGH;

if (bvSerial.intData == 31)
lcd.clear ();
if (bvSerial.intData == 31)
lcd.print("RELAY 16 ON");

if (bvSerial.intData == 35)
lcd.clear ();
if (bvSerial.intData == 35)
lcd.print("CHECK OFF");

if (bvSerial.intData == 34)
lcd.clear ();
if (bvSerial.intData == 34)
lcd.print("CHECK ON");




// all lights on or off

if (bvSerial.intData == 32)
relay1 = HIGH;
if (bvSerial.intData == 33)
relay1 = LOW;

if (bvSerial.intData == 32)
relay2 = HIGH;
if (bvSerial.intData == 33)
relay2 = LOW;

if (bvSerial.intData == 32)
relay3 = HIGH;
if (bvSerial.intData == 33)
relay3 = LOW;

if (bvSerial.intData == 32)
relay4 = HIGH;
if (bvSerial.intData == 33)
relay4 = LOW;

if (bvSerial.intData == 32)
relay5 = HIGH;
if (bvSerial.intData == 33)
relay5 = LOW;

if (bvSerial.intData == 32)
relay6 = HIGH;
if (bvSerial.intData == 33)
relay6 = LOW;

if (bvSerial.intData == 32)
relay7 = HIGH;
if (bvSerial.intData == 33)
relay7 = LOW;

if (bvSerial.intData == 32)
relay8 = HIGH;
if (bvSerial.intData == 33)
relay8 = LOW;

if (bvSerial.intData == 32)
relay9 = HIGH;
if (bvSerial.intData == 33)
relay9 = LOW;

if (bvSerial.intData == 32)
relay10 = HIGH;
if (bvSerial.intData == 33)
relay10 = LOW;

if (bvSerial.intData == 32)
relay11 = HIGH;
if (bvSerial.intData == 33)
relay11 = LOW;

if (bvSerial.intData == 32)
relay12 = HIGH;
if (bvSerial.intData == 33)
relay12 = LOW;

if (bvSerial.intData == 32)
relay13 = HIGH;
if (bvSerial.intData == 33)
relay13 = LOW;

if (bvSerial.intData == 32)
relay14 = HIGH;
if (bvSerial.intData == 33)
relay14 = LOW;

if (bvSerial.intData == 32)
relay15 = HIGH;
if (bvSerial.intData == 33)
relay15 = LOW;

if (bvSerial.intData == 32)
relay16 = HIGH;
if (bvSerial.intData == 33)
relay16 = LOW;

}

You still haven't responded about the most serious error in your code.

Due to the fact that you are saying this:

int relay1 = 31;
[...]
digitalWrite(31, relay1);

the result is that you are sending integer values to the pins, not HIGH and LOW, in effect:

digitalWrite(31, 31);

My full code above, as requested in the proper way lol.

Switching the relays is not a problem on its own, i can turn on/off all the outputs at the same time with the bottom bit of code. All i need is when the relay goes high or low, for that information to be printed to screen???

I'm a bit lost as to which bit your on about? this code works fine with the 16 relay card outputs, most of the code used is from someone else using bitvoicer, ive just expanded it to work with 16 outputs instead of one. the problem is getting it to lcd.print when the relay goes high or low.

Jamie993: Switching the relays is not a problem on its own, i can turn on/off all the outputs at the same time with the bottom bit of code. All i need is when the relay goes high or low, for that information to be printed to screen???

Nobody who knows much about coding will be very interested in helping you unless you have code that "means what it says". You have misunderstood the purpose of naming relays. I know this because you meticulously associated pins with relay names.

//Stores the state of pins
int relay1 = 31;
[...]

should be:

//Give the relay pins meaningful names
int relay1 = 31;
[...]
int relay1state = LOW;
[...]

then use

pinMode(relay1, OUTPUT);

and

digitalWrite(relay1, relay1state);
[...]
if (bvSerial.intData == 0)
relay1state = LOW;
if (bvSerial.intData == 1)
relay1state = HIGH;

This way you only have to deal with relay pin assignment numbers once, in the declaration.

Ok, Thanks, its going to take me a while to change the code and check if it still works. i was hoping it would be a easy fix, Im not that clued up with how coding should be written as you can tell. Is there no way lcd.print will work with what i have?

Jamie993: Is there no way lcd.print will work with what i have?

If it couldn't write a pin high and print to an LCD in the same program it wouldn't be much use as a micro controller. Of course you can print to the lcd. You just have to write the code right first. A little less guessing and hacking and a little more reading and studying would probably help.

what does the code look like now? Has it changed? If so post up the new version.

I know am a bit late to this party, but perhaps you would get some ideas in the demo Several Things at a Time

The important "trick" is to ensure that no part of your program holds things up for more than a number of microseconds.

You might also wish to look at Planning and Implementing a Program which uses similar techniques.

...R