Please help me finish my project....
Is there a way to use digitalWrite() to turn on 2 pins at the same time......with the "IF" statement?
If not is there a way to do this?
thank you!!!
Please help me finish my project....
Is there a way to use digitalWrite() to turn on 2 pins at the same time......with the "IF" statement?
If not is there a way to do this?
thank you!!!
No, but how about two digitalWrites?
You will need to put the write in curly braces like this:
if (something == true)
{
digitalWrite(pin1, HIGH);
digitalWrite(pin2, HIGH);
}
Maybe he is referring to exactly at the same time, isn't that possible by using Port Registers ?
Nevertheless there are some limitations i.e. the two pins must be at the same port.
yes I would like to turn them high and low at the same time...
thank you
how about port manipulation?
yes I would like to turn them high and low at the same time
Many beginners think this is what they want to do but it is seldom what the want.
Using two digitalWrites will turn on the pins within about 10uS of each other. This is so short a time as to be considered the same as "at the same time" for 99.99% of the cases you will come across.
What are these pins driving?
I have an analog volt output in the range of 0-1.7 and I have an RGB Led wired to digital pins 22-24. I would like to use all 7 colors of the RGB led to represent a voltage range with increments of .10 starting at .700mV....(ex: white=.70-.80 , blue=.81-90 , green=.91-1.00..........all the way up to red=1.40-1.70. I have tried using "digitalWrite(led, LOW)" and it works to keep the led on, but the led remains on and when I cal the led to the HIGH state to turn it off, this effects the other colors when I mix the other pins together. This is due to the secondary colors that need a combination of pins in order to output the correct color.....ex: cyan color requires pin 2(blue) and pin 3(green) to be on at the same time through the range of 1.10-1.20, can I call these pins on for just that range without effecting pin2(blue) and pin3(green)...?
Here's an example of the code I wrote for the digital pins.
const int red1 =22; //initiate pins
const int blue1 =28;
const int green1 =26;
pinMode(red1, INPUT); //set output pins to high by defualt
digitalWrite(red1, HIGH);
pinMode(green1, INPUT);
digitalWrite(green1, HIGH);
pinMode(blue1, INPUT);
digitalWrite(blue1, HIGH);
pinMode( red1, OUTPUT); //set output assignments for led pins
pinMode( green1, OUTPUT);
pinMode( blue1, OUTPUT);
I have tried the following code but It just doesn't allow me to combine the colors(on at the same time), in order to get my secondary colors.
If I do repeat them, they blink.
if (PPO >= .90 && PPO <= 1.10) digitalWrite(yellow1, LOW); //yellow led
else digitalWrite(yellow1, HIGH);
if (PPO >= 1.10 && PPO <= 1.40) digitalWrite(blue1, LOW); //blue led
else digitalWrite(blue1, HIGH);
if (PPO >= .60 && PPO <= .90) digitalWrite(green1, LOW); //green led
else digitalWrite(green1,HIGH);
if (PPO > 1.40) digitalWrite(red1, LOW);
else digitalWrite(red1, HIGH);
Thank you so much for all your help and feedback, I'm so close to finishing my proto this is all I'm missing.
pinMode(red1, INPUT); //set output pins to high by defualt
digitalWrite(red1, HIGH);
Why are you using the internal pullups?
Post ALL your code.
I'm using the pullup resistors because the RGB led requires common positive and an negative output at the three legs to activate each of the seven colors.
// include the library code:
#include <LiquidCrystal.h>
#include <PID_v1.h>
#define aref_voltage 1.72
#define PPO inputReading1*aref_voltage/1024
/ Variables connecting to
double Setpoint, Input, Output;
//initial tuning parameters
PID myPID(&Input, &Output, &Setpoint, 5,7,2, REVERSE);
// initialize the lcd pins
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
/****************************************************************
*initiate pins for each led
*****************************************************************/
const int red1 =22;
const int blue1 =28;
const int green1 =26;
const int yellow1 =24;
const int red2 =0;
const int green2 =0;
const int blue2 =0;
const int yellow2 =0;
const int red3 =0;
const int green3 =0;
const int blue3 =0;
const int yellow3 =0;
/****************************************************************/
/****************************************************************
*initiate pins for sensor inputs 1, 2, 3
*****************************************************************/
int sensor1 = A0; // the analog pin the 02 Sensor Vout pin is connected to
int inputReading1; //declare a variable
int sensor2 = A1; // the analog pin the 02 Sensor Vout pin is connected to
int inputReading2; // declare a variable
int sensor3 = A2; // the analog pin the 02 sensor Vout pin is connected to
int inputReading3; // declare a variable
/*****************************************************************/
void setup ()
{
Input = analogRead(PPO);
Setpoint = 520;
myPID.SetMode(AUTOMATIC);
analogReference(EXTERNAL); // set external analog Ref for PPO output
/*****************************************************************
*set output pins to HIGH by default
******************************************************************/
pinMode(red1, INPUT);
digitalWrite(red1, HIGH);
pinMode(green1, INPUT);
digitalWrite(green1, HIGH);
pinMode(blue1, INPUT);
digitalWrite(blue1, HIGH);
pinMode(yellow1, INPUT);
digitalWrite(yellow1, HIGH);
pinMode(red2, INPUT);
digitalWrite(red2, HIGH);
pinMode(green2, INPUT);
digitalWrite(green2, HIGH);
pinMode(blue2, INPUT);
digitalWrite(blue2, HIGH);
pinMode(yellow2, INPUT);
digitalWrite(yellow2, HIGH);
pinMode(red3, INPUT);
digitalWrite(red3, HIGH);
pinMode(green3, INPUT);
digitalWrite(green3, HIGH);
pinMode(blue3, INPUT);
digitalWrite(blue3, HIGH);
pinMode(yellow3, INPUT);
digitalWrite(yellow3, HIGH);
/****************************************************************/
/*****************************************************************
*set output assignments for led pins
******************************************************************/
pinMode( red1, OUTPUT);
pinMode( green1, OUTPUT);
pinMode( blue1, OUTPUT);
pinMode( yellow1, OUTPUT);
pinMode( red2, OUTPUT);
pinMode( green2, OUTPUT);
pinMode( blue2, OUTPUT);
pinMode( yellow2, OUTPUT);
pinMode( red3, OUTPUT);
pinMode( green3, OUTPUT);
pinMode( blue3, OUTPUT);
pinMode( yellow3, OUTPUT);
/****************************************************************/
/***************************************************************
*set up the LCD's number of columns and rows:
****************************************************************/
lcd.begin(20, 4);
lcd.setCursor(0, 0);//where to start cursor
lcd.print("PINA SYSYTEMS ");//print message
lcd.print("SOFTWARE VERSION 1.0");//print message
delay(2000);
lcd.clear();
lcd.setCursor(0, 0); // set the cursor to column 0, line 0
lcd.print("PP1"); // Print a message to the LCD.
lcd.setCursor(6, 0); // set the cursor to column 6, line 0
lcd.print("PP2"); // Print a message to the LCD
lcd.setCursor(12, 0); // set the cursor to column 12, line 0
lcd.print("PP3"); // Print a message to the LCD.
/*************************************************************/
}
void loop ()
{
/*************************************************************
* turn the output pin on/off based on pid output
**************************************************************/
Input = analogRead(PPO);
myPID.Compute();
analogWrite(7,Output);
/*************************************************************/
if (PPO >= .90 && PPO <= 1.10) digitalWrite(yellow1, LOW); //yellow led
else digitalWrite(yellow1, HIGH);
if (PPO >= 1.10 && PPO <= 1.40) digitalWrite(blue1, LOW); //blue led
else digitalWrite(blue1, HIGH);
if (PPO >= .60 && PPO <= .90) digitalWrite(green1, LOW); //green led
else digitalWrite(green1,HIGH);
if (PPO > 1.40) digitalWrite(red1, LOW);
else digitalWrite(red1, HIGH);
Moderator edit: added CODE TAGS
All of the code would be good, but are you saying that you're using a 20k + resistor as a current limiter?
const int red2 =0;
const int green2 =0;
pinMode(red2, INPUT);
digitalWrite(red2, HIGH);
pinMode(green2, INPUT);
digitalWrite(green2, HIGH);
Really?
No, just the pullup, and how do I call two of the colors at the same time, without effecting the other single colors?
Before I answer that, tell me about all those pin zero operations
No, just the pullup
The pullup is a 20k + resistor
those "cons int" that are set to "0" I have not assigned an output pin to yet, I'm just testing one of the leds right now, the plan is to have three of them working together on three sensors.
oh really?..it's a 20k +......I did'nt know this .....thank you!!
everything seems to work perfect, only when I call 2 colors(2 outputs at the same time) to get say "cyan" it makes those 2 primary colors malfunction....? Is there a way to avoid this?
I'm sorry, I really don't understand how you're driving the LEDs.
It is a common anode RGB?
So, wire the common to +5V, and each LED via an individual current-limiter (start with 220R) to an output pin.
Make the pin an OUTPUT, and write a zero to illuminate it, and a one to turn it off.
If you want PWM write 255 to turn it off, and zero to turn it on.
I'm so sorry if I'm not explaining myself very well.......
here's an example of the code I've tried to achieve a secondary color......but just makes the primary color of say "blue1" blink.
if (PPO >= 1.10 && PPO <= 1.20) digitalWrite(blue1, LOW); //light blue and green
else digitalWrite(blue1, HIGH);
if (PPO >= 1.10 && PPO <= 1.20) digitalWrite(green1, LOW); //light blue and green
else digitalWrite(green1, HIGH);
Those two if tests do exactly the same thing or did I miss something?
Essentially that code is
if (PPO >= 1.10 && PPO <= 1.20) {
digitalWrite(blue1, LOW); //light blue and green
digitalWrite(green1, LOW);
} else {
digitalWrite(blue1, HIGH);
digitalWrite(green1, HIGH);
}
Rob
yes the common is +5v and the digital outputs are LOW to turn the leds on and HIGH to turn them off