uno adc+lcd

  • 0 Replies
  • 1070 Views
uno adc+lcd
« on: June 24, 2015, 11:48:48 am »
Quote
#include <Wire.h>
#include <LCD.h>
#include <LiquidCrystal_I2C.h>

#define I2C_ADDR    0x3F
#define BACKLIGHT_PIN     3
#define En_pin  2
#define Rw_pin  1
#define Rs_pin  0
#define D4_pin  4
#define D5_pin  5
#define D6_pin  6
#define D7_pin  7
LiquidCrystal_I2C   lcd(I2C_ADDR,En_pin,Rw_pin,Rs_pin,D4_pin,D5_pin,D6_pin,D7_pin);


float volt;
int rawmap;

int led1r=255;
int led1g=0;
int led1b=0;

int led2r=255;
int led2g=0;
int led2b=0;

int led3r=255;
int led3g=0;
int led3b=0;

void setup() {
  lcd.begin (16,2);
  lcd.setBacklightPin(BACKLIGHT_PIN,POSITIVE);
  lcd.setBacklight(HIGH);
  pinMode(A0, INPUT);
 
  pinMode(2,OUTPUT);
  pinMode(3,OUTPUT);
  pinMode(4,OUTPUT);
  pinMode(5,OUTPUT);
  pinMode(6,OUTPUT);
  pinMode(7,OUTPUT);
  pinMode(8,OUTPUT);
  pinMode(9,OUTPUT);
  pinMode(10,OUTPUT);
 

}

void loop() {
 
  analogWrite(2,led1r);
  analogWrite(3,led1g);
  analogWrite(4,led1b);
 
  analogWrite(5,led2r);
  analogWrite(6,led2g);
  analogWrite(7,led2b);
 
  analogWrite(8,led3r);
  analogWrite(9,led3g);
  analogWrite(10,led3b);
 
  rawmap=analogReadAvg(A0,0,1023,0,1023);
 lcd.setCursor (0,0);
 lcd.print("Voltage:");
 lcd.print(fmap(rawmap,0,1023,0,5));
 lcd.print(" V");
 lcd.setCursor (0,1);
 lcd.print("ADC:");
 lcd.print(rawmap);
 lcd.print("  ");

}

int analogReadAvg(int anPort,int rang1,int rang2,int perMin,int perMax) {
  float Avg=0;
  int k,Inv=5;
  for(k=1;k<=Inv;k++) {
    Avg+=analogRead(anPort);
    delay(3);
  }
  Avg=(Avg/Inv);
  Avg=map(Avg,rang1,rang2,perMin,perMax);
 // if(Avg > 100) {return 100;}
//  if(Avg < 0 ) {return 0;}
  return int(Avg);
}

float fmap(float x, float x1, float x2, float y1, float y2)
{
  return (x - x1) * (y2 - y1) / (x2 - x1) + y1;
}