user27542: I'm new to the arduino board and my teacher gave us a project with little to no teaching about the language the board uses, or how to effectively program with it.
I'm trying to read temperature from a DHT11 and display it on two separate 7-segments displays. I've been trying to do this by editing a code he provided us that makes one display count from 0 to 9. I've been to hook up and read the DHT11 and split the temperature into two variables. But I have no idea how to make the variables display on the seven segment displays. My code is.
//DHT11 set up
#include <dht11.h>
dht11 DHT11;
void setup()
{
   DHT11.attach(4);
   for (int i = 0; i < 4; ++i)
    pinMode(i, OUTPUT);
   for (int i = 8; i < 11; ++i)
    pinMode(i, OUTPUT);
}
static int temp = DHT11.fahrenheit();
static int digit_0 = temp % 10;
static int digit_1 = temp - digit_0;
static int digit_2 = digit_1 / 10;
// Output pins.
static int output[] = {0,1,2,3};
// 8421 code lookup table.
static int segs_encoding[][4] =
  {{0,0,0,0},   //=0
    {0,0,0,1},  //=1
    {0,0,1,0},  //=2
    {0,0,1,1},  //=3
    {0,1,0,0},  //=4
    {0,1,0,1},  //=5
    {0,1,1,0},  //=6
    {0,1,1,1},  //=7
    {1,0,0,0},  //=8
    {1,0,0,1}}; //=9
// Outputs an 8421 decimal digit on the output pins from
// base to base + 3 in big endian order.
void show(int base, int num) {
 for (int i = 0; i < 4; ++i) {
   if(segs_encoding[num][i] == 1) {
     digitalWrite(base + 3 - i , HIGH);
   } else {
     digitalWrite(base + 3 - i , LOW);
   }
  }
}
// Output a 2 digit decimal value.
void show_2digit_decimal(int num) {
// force value to displayable range because show() doesn't do any sanity 
  if (num < 0)
    num = 0;
  if (num > 99)
    num = 99;
  // display it.
  show(0, num%10);
  show(8, num/10);
} 
void loop() { 
  static int i = 0;
  show_decimal(i);
  delay(1000);
// Increases the counter.
  ++i;
  if (i == 10)
    i = 0; 
}   

Let me know if you guys need pictures of my breadboard set up, thanks for any help you guys can provide.

user7508: There are many issues with this code:

The compiler says “error: ‘show_decimal’ was not declared in this
scope”. Indeed, you named the function show_2digit_decimal(), so
you should call it by that name.
The compiler also said: “warning: ‘output’ defined but not used”.
There is no valid reason to leave something defined that you do not
use. Just remove it.
You also have a function named sertup() that you defined but never
used. It looks like a typo. You should remove the function, and put
it's current contents inside setup().
You have the variables digit_0, digit_1 and digit_2 that serve
no useful purpose. Remove them as well.
You wrote, at top level: static int temp = DHT11.fahrenheit(); Don't
do this. This kind of initialization happens before setup(), i.e.
before you had a chance to attach() the sensor to its pins.
Your array segs_encoding is not a 7-segment encoding, it's just
the binary encoding of the numbers. You don't need it: the processor
already knows how to count in binary, and you can replace
segs_encoding[num][i] by num>>i&1. If you really have 7-segment
displays, you need 7 pins for each, unless you multiplex them. Search
this site for “7 segment” to see how to drive them.
The section
if(segs_encoding[num][i] == 1) { digitalWrite(base + 3 - i , HIGH); ...
can be simplified to
digitalWrite(base + 3 - i , segs_encoding[num][i]); or, considering
the previous point, to digitalWrite(base + 3 - i , num >> i & 1);
The test if (num < 0) num = 0; if (num > 99) num = 99; can be
written more succinctly by using the function Arduino provides for
that: num = constrain(num, 0, 99);
In loop(), you are only displaying a counter. You probably should
display the temperature instead.

All the previous comments lead to the following version of the code:
#include <dht11.h>

const int units_base_pin = 0;  // units on pins 0..3
const int tens_base_pin = 8;   // tens on pins 8..11

dht11 DHT11;

// Outputs a number in binary on the output pins from
// base to base + 3 in big endian order.
void show(int base, int num) {
    for (int i = 0; i < 4; ++i) {
        digitalWrite(base + 3 - i , num >> i & 1);
    }
}

// Output a 2 digit decimal value.
void show_2digit_decimal(int num) {
    // force value to displayable range because show() doesn't do any sanity
    num = constrain(num, 0, 99);
    // display it.
    show(units_base_pin, num%10);
    show(tens_base_pin, num/10);
}

void setup() {
    DHT11.attach(4);
    for (int i = 0; i < 4; ++i)
        pinMode(i, OUTPUT);
}

void loop() {
    show_2digit_decimal(DHT11.fahrenheit());
    delay(1000);
}


user24459: The show() function already has support for specifying the first pin of the display. So if the second digit is starting at pin 8 and connected in the same order then it should be as simple as:
// Output a 2 digit decimal value.
void show_2digit_decimal(int num) {
  // force value to displayable range because show() doesn't do any sanity checks
  if (num < 0)
    num = 0;
  if (num > 99)
    num = 99;
  // display it.
  show(0, num%10);
  show(8, num/10); // int/int always rounds down to the nearest int
}

You will also have to add the appropriate pinMode(<pin>, OUTPUT); commands to the setup() function to make the pins outputs.
----- update
To get the temperature to display first get rid of these lines, they aren't doing anything useful
static int temp = DHT11.fahrenheit();
static int digit_0 = temp % 10;
static int digit_1 = temp - digit_0;
static int digit_2 = digit_1 / 10;

And then change the loop function to being
void loop() { 
  show_2digit_decimal(DHT11.fahrenheit());
  delay(500);
}

Although unless the teacher is completely incompetent you should have been able to work that out yourself, it's not exactly rocket science. If unsure take a guess and try it first before asking. The logic is simple enough that you should be able to work it out. For syntax errors the compiler error messages will normally tell you exactly what's wrong.

27542: <p>I'm new to the arduino board and my teacher gave us a project with little to no teaching about the language the board uses, or how to effectively program with it.
I'm trying to read temperature from a DHT11 and display it on two separate 7-segments displays. I've been trying to do this by editing a code he provided us that makes one display count from 0 to 9. I've been to hook up and read the DHT11 and split the temperature into two variables. But I have no idea how to make the variables display on the seven segment displays. My code is.</p>

<pre><code>//DHT11 set up
#include &lt;dht11.h&gt;
dht11 DHT11;
void setup()
{
   DHT11.attach(4);
   for (int i = 0; i &lt; 4; ++i)
    pinMode(i, OUTPUT);
   for (int i = 8; i &lt; 11; ++i)
    pinMode(i, OUTPUT);
}
static int temp = DHT11.fahrenheit();
static int digit_0 = temp % 10;
static int digit_1 = temp - digit_0;
static int digit_2 = digit_1 / 10;
// Output pins.
static int output[] = {0,1,2,3};
// 8421 code lookup table.
static int segs_encoding[][4] =
  {{0,0,0,0},   //=0
    {0,0,0,1},  //=1
    {0,0,1,0},  //=2
    {0,0,1,1},  //=3
    {0,1,0,0},  //=4
    {0,1,0,1},  //=5
    {0,1,1,0},  //=6
    {0,1,1,1},  //=7
    {1,0,0,0},  //=8
    {1,0,0,1}}; //=9
// Outputs an 8421 decimal digit on the output pins from
// base to base + 3 in big endian order.
void show(int base, int num) {
 for (int i = 0; i &lt; 4; ++i) {
   if(segs_encoding[num][i] == 1) {
     digitalWrite(base + 3 - i , HIGH);
   } else {
     digitalWrite(base + 3 - i , LOW);
   }
  }
}
// Output a 2 digit decimal value.
void show_2digit_decimal(int num) {
// force value to displayable range because show() doesn't do any sanity 
  if (num &lt; 0)
    num = 0;
  if (num &gt; 99)
    num = 99;
  // display it.
  show(0, num%10);
  show(8, num/10);
} 
void loop() { 
  static int i = 0;
  show_decimal(i);
  delay(1000);
// Increases the counter.
  ++i;
  if (i == 10)
    i = 0; 
}   
</code></pre>

<p>Let me know if you guys need pictures of my breadboard set up, thanks for any help you guys can provide.</p>
 7508: <p>There are many issues with this code:</p>

<ul>
<li>The compiler says “<em>error</em>: ‘show_decimal’ <em>was not declared in this
scope</em>”. Indeed, you named the function <code>show_2digit_decimal()</code>, so
you should call it by that name.</li>
<li>The compiler also said: “<em>warning: ‘output’ defined but not used</em>”.
There is no valid reason to leave something defined that you do not
use. Just remove it.</li>
<li>You also have a function named <code>sertup()</code> that you defined but never
used. It looks like a typo. You should remove the function, and put
it's current contents inside <code>setup()</code>.</li>
<li>You have the variables <code>digit_0</code>, <code>digit_1</code> and <code>digit_2</code> that serve
no useful purpose. Remove them as well.</li>
<li>You wrote, at top level: <code>static int temp = DHT11.fahrenheit();</code> Don't
do this. This kind of initialization happens before <code>setup()</code>, i.e.
before you had a chance to <code>attach()</code> the sensor to its pins.</li>
<li>Your array <code>segs_encoding</code> is <strong>not</strong> a 7-segment encoding, it's just
the binary encoding of the numbers. You don't need it: the processor
already knows how to count in binary, and you can replace
<code>segs_encoding[num][i]</code> by <code>num&gt;&gt;i&amp;1</code>. If you really have 7-segment
displays, you need 7 pins for each, unless you multiplex them. Search
this site for “7 segment” to see how to drive them.</li>
<li>The section
<code>if(segs_encoding[num][i] == 1) { digitalWrite(base + 3 - i , HIGH); ...</code>
can be simplified to
<code>digitalWrite(base + 3 - i , segs_encoding[num][i]);</code> or, considering
the previous point, to <code>digitalWrite(base + 3 - i , num &gt;&gt; i &amp; 1);</code></li>
<li>The test <code>if (num &lt; 0) num = 0; if (num &gt; 99) num = 99;</code> can be
written more succinctly by using the function Arduino provides for
that: <code>num = constrain(num, 0, 99);</code></li>
<li>In <code>loop()</code>, you are only displaying a counter. You probably should
display the temperature instead.</li>
</ul>

<p>All the previous comments lead to the following version of the code:</p>

<pre class="lang-c++ prettyprint-override"><code>#include &lt;dht11.h&gt;

const int units_base_pin = 0;  // units on pins 0..3
const int tens_base_pin = 8;   // tens on pins 8..11

dht11 DHT11;

// Outputs a number in binary on the output pins from
// base to base + 3 in big endian order.
void show(int base, int num) {
    for (int i = 0; i &lt; 4; ++i) {
        digitalWrite(base + 3 - i , num &gt;&gt; i &amp; 1);
    }
}

// Output a 2 digit decimal value.
void show_2digit_decimal(int num) {
    // force value to displayable range because show() doesn't do any sanity
    num = constrain(num, 0, 99);
    // display it.
    show(units_base_pin, num%10);
    show(tens_base_pin, num/10);
}

void setup() {
    DHT11.attach(4);
    for (int i = 0; i &lt; 4; ++i)
        pinMode(i, OUTPUT);
}

void loop() {
    show_2digit_decimal(DHT11.fahrenheit());
    delay(1000);
}
</code></pre>
 24459: <p>The <code>show()</code> function already has support for specifying the first pin of the display. So if the second digit is starting at pin 8 and connected in the same order then it should be as simple as:</p>

<pre><code>// Output a 2 digit decimal value.
void show_2digit_decimal(int num) {
  // force value to displayable range because show() doesn't do any sanity checks
  if (num &lt; 0)
    num = 0;
  if (num &gt; 99)
    num = 99;
  // display it.
  show(0, num%10);
  show(8, num/10); // int/int always rounds down to the nearest int
}
</code></pre>

<p>You will also have to add the appropriate <code>pinMode(&lt;pin&gt;, OUTPUT);</code> commands to the <code>setup()</code> function to make the pins outputs.</p>

<p>----- update</p>

<p>To get the temperature to display first get rid of these lines, they aren't doing anything useful</p>

<pre><code>static int temp = DHT11.fahrenheit();
static int digit_0 = temp % 10;
static int digit_1 = temp - digit_0;
static int digit_2 = digit_1 / 10;
</code></pre>

<p>And then change the loop function to being</p>

<pre><code>void loop() { 
  show_2digit_decimal(DHT11.fahrenheit());
  delay(500);
}
</code></pre>

<p>Although unless the teacher is completely incompetent you should have been able to work that out yourself, it's not exactly rocket science. If unsure take a guess and try it first before asking. The logic is simple enough that you should be able to work it out. For syntax errors the compiler error messages will normally tell you exactly what's wrong.</p>
