Hi!
Ok, so this is my first Arduino Project and im trying to make a thermometer.
It works as intended except for one thing…i can get the else if for negative values to execute…
else if (tempC < 0)
any ideas?
Here is the rest…
#include <OneWire.h>
#include <DallasTemperature.h>
// Data wire is plugged into port 2 on the Arduino
#define ONE_WIRE_BUS 2
#define TEMPERATURE_PRECISION 9
#include <Adafruit_NeoPixel.h>
#ifdef __AVR__
#include <avr/power.h>
#endif
#define PIN 6
// Parameter 1 = number of pixels in strip
// Parameter 2 = Arduino pin number (most are valid)
// Parameter 3 = pixel type flags, add together as needed:
// NEO_KHZ800 800 KHz bitstream (most NeoPixel products w/WS2812 LEDs)
// NEO_KHZ400 400 KHz (classic 'v1' (not v2) FLORA pixels, WS2811 drivers)
// NEO_GRB Pixels are wired for GRB bitstream (most NeoPixel products)
// NEO_RGB Pixels are wired for RGB bitstream (v1 FLORA pixels, not v2)
Adafruit_NeoPixel strip = Adafruit_NeoPixel(16, PIN, NEO_GRB + NEO_KHZ800);
// IMPORTANT: To reduce NeoPixel burnout risk, add 1000 uF capacitor across
// pixel power leads, add 300 - 500 Ohm resistor on first pixel's data input
// and minimize distance between Arduino and first pixel. Avoid connecting
// on a live circuit...if you must, connect GND first.
// Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
OneWire oneWire(ONE_WIRE_BUS);
// Pass our oneWire reference to Dallas Temperature.
DallasTemperature sensors(&oneWire);
int numberOfDevices; // Number of temperature devices found
DeviceAddress tempDeviceAddress; // We'll use this variable to store a found device address
void setup(void)
{
// start serial port
Serial.begin(9600);
Serial.println("Dallas Temperature IC Control Library Demo");
// Start up the library
sensors.begin();
strip.begin();
strip.show(); // Initialize all pixels to 'off'
// Grab a count of devices on the wire
numberOfDevices = sensors.getDeviceCount();
// locate devices on the bus
Serial.print("Locating devices...");
Serial.print("Found ");
Serial.print(numberOfDevices, DEC);
Serial.println(" devices.");
// report parasite power requirements
Serial.print("Parasite power is: ");
if (sensors.isParasitePowerMode()) Serial.println("ON");
else Serial.println("OFF");
// Loop through each device, print out address
for(int i=0;i<numberOfDevices; i++)
{
// Search the wire for address
if(sensors.getAddress(tempDeviceAddress, i))
{
Serial.print("Found device ");
Serial.print(i, DEC);
Serial.print(" with address: ");
printAddress(tempDeviceAddress);
Serial.println();
Serial.print("Setting resolution to ");
Serial.println(TEMPERATURE_PRECISION, DEC);
// set the resolution to TEMPERATURE_PRECISION bit (Each Dallas/Maxim device is capable of several different resolutions)
sensors.setResolution(tempDeviceAddress, TEMPERATURE_PRECISION);
Serial.print("Resolution actually set to: ");
Serial.print(sensors.getResolution(tempDeviceAddress), DEC);
Serial.println();
}else{
Serial.print("Found ghost device at ");
Serial.print(i, DEC);
Serial.print(" but could not detect address. Check power and cabling");
}
}
}
// function to print the temperature for a device
void printTemperature(DeviceAddress deviceAddress)
{
// method 1 - slower
//Serial.print("Temp C: ");
//Serial.print(sensors.getTempC(deviceAddress));
//Serial.print(" Temp F: ");
//Serial.print(sensors.getTempF(deviceAddress)); // Makes a second call to getTempC and then converts to Fahrenheit
// method 2 - faster
float tempC = sensors.getTempC(deviceAddress);
// Serial.print("Temp C: ");
// Serial.print(tempC);
// Serial.print(" Temp F: ");
// Serial.println(DallasTemperature::toFahrenheit(tempC)); // Converts tempC to Fahrenheit
}
void loop(void)
{
int numberOfLeds = 16;
int tempC=-10;
int PixelIndex;
int wheelIndex;
int MappedTempC;
if (tempC > 0)
{
MappedTempC = map(tempC,0,30,8,16);
for (PixelIndex = numberOfLeds/2; PixelIndex < MappedTempC;)
{
// Light up warm side
wheelIndex = map(PixelIndex, 8, 16, 127, 0); // map pixel position to a wheel position
strip.setPixelColor(PixelIndex, Wheel(wheelIndex ));
PixelIndex++;
Serial.print("Its hot, ");
}
}
else if (tempC < 0)
{
// Light up cold side
MappedTempC = map(tempC,0,-20,7,0);
for (PixelIndex = numberOfLeds/2; PixelIndex < MappedTempC;)
{
wheelIndex = map(PixelIndex, 8, numberOfLeds, 128, 255); // map pixel position to a wheel position
strip.setPixelColor(PixelIndex, Wheel(wheelIndex ));
PixelIndex++;
Serial.print("Its cold");
}
}
else
{
// Light up 1 and -1 LED, these two indicate zero degrees
wheelIndex = map(7, 7, numberOfLeds, 255, 0); // map pixel position to a wheel position
strip.setPixelColor(7, Wheel(wheelIndex ));
wheelIndex = map(8, 8, numberOfLeds, 255, 0); // map pixel position to a wheel position
strip.setPixelColor(8, Wheel(wheelIndex ));
Serial.print("Its Zero");
}
strip.show();
// call sensors.requestTemperatures() to issue a global temperature
// request to all devices on the bus
Serial.print("Requesting temperatures...");
sensors.requestTemperatures(); // Send the command to get temperatures
Serial.println("DONE");
// Loop through each device, print out temperature data
for(int i=0;i<numberOfDevices; i++)
{
// Search the wire for address
if(sensors.getAddress(tempDeviceAddress, i))
{
// Output the device ID
Serial.print("Temperature for device: ");
Serial.println(i,DEC);
// It responds almost immediately. Let's print out the data
printTemperature(tempDeviceAddress); // Use a simple function to print out the data
}
//else ghost device! Check your power requirements and cabling
}
}
// function to print a device address
void printAddress(DeviceAddress deviceAddress)
{
for (uint8_t i = 0; i < 8; i++)
{
if (deviceAddress[i] < 16) Serial.print("0");
Serial.print(deviceAddress[i], HEX);
}
}
// Input a value 0 to 255 to get a color value.
// The colours are a transition r - g - b - back to r.
uint32_t Wheel(byte WheelPos) {
WheelPos = 255 - WheelPos;
if(WheelPos < 85) {
return strip.Color(255 - WheelPos * 3, 0, WheelPos * 3);
}
if(WheelPos < 170) {
WheelPos -= 85;
return strip.Color(0, WheelPos * 3, 255 - WheelPos * 3);
}
WheelPos -= 170;
return strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
}
void TempToLEDs(void)
{
int numberOfLeds = 16;
// for (int i = 0; i < abs(tempC);)
// {
Serial.print("aaaa");
int tempC=20;
if (tempC > 0)
{
for (int l = numberOfLeds/2; l < abs(tempC);)
{
// Light up warm side
int PixelIndex = map(tempC,0,30,8,16);
Serial.print(l);
int wheelIndex = map(l, 0, numberOfLeds, 255, 0); // map pixel position to a wheel position
strip.setPixelColor(l, Wheel(wheelIndex ));
l++;
}
}
else if (tempC < 0)
{
// Light up cold side
}
else
{
// Light up 1 and -1 LED
}
// }
strip.show();
Serial.print("Slut");
}
Yea, im a noob LOL!
/Ma3a