Ir ao conteúdo
  • Cadastre-se

doidoqw

Membro Júnior
  • Posts

    1
  • Cadastrado em

  • Última visita

Reputação

0
  1. Estou fazendo um projeto na faculdade que é o seguinte: uma fonte de tensão simetrica de -12 a 12… COmo quis colocar coisas a mais, estou usando arduino para fazer o controle do cooler. No lcd 16x2 quero mostrar a tensão e temperatura…a tensão positiva esta indo tranquilo, porém a negativa nao… Segue abaixo codigo em arduino…se alguem puder dar uma luz agradeco #include#include#include//debug flag avoid enabling it makes your device slower//#define DEBUG//pinsconst int PIN_BUZZER = 7;const int PIN_VOLTAGE = 2;const int PIN_CURRENT = 1;const int PIN_BUTTON_UP = 6;const int PIN_BUTTON_SETUP = 8;const int PIN_BUTTON_DOWN = 9;const int sensorTemp = 0; // Pino analógico em que o sensor de temperatura está conectadoconst int SENSOR_INTERVAL = 500;const int BUTTON_HOLDTIME = 1000;const int SETUP_MAXITEMS = 2;const int SETUP_BLINKINTERVAL = 300;const byte EEPROM_VALIDATOR = 73; //random numberconst float VOLTAGE_STEP = 0.1;const float CURRENT_STEP = 0.1;//configurationconst byte EEPROM_CONFIGADDRESS = 0;struct config_t{byte Validator;float VOLTAGE_MAP;float CURRENT_MAP;byte ValidatorX2;} EEPROM_DATA;const int protecao = 10;const int res =5;int reset=5;int valorreset =0;int valorres = INT_MAX;int fan = 13; // Declara o pino digital 13 para acionar o coolerint tempPin = 0; // Declara o pino analógico 0 para ler o valor do sensor de temperaturaint valorSensorTemp = 0; // Variável usada para ler o valor de temperaturaint valorTemp = INT_MAX; // Variável usada para armazenar o menor valor de temperatura//variables//voltageint VOLTAGE_CURRENT;int VOLTAGE_LAST=99999;unsigned long VOLTAGE_MILLIS;float VOLTAGE_CALCULATED;float VOLTAGE_MAP = 50; //default voltage map calibration needed//currentint CURRENT_CURRENT;int CURRENT_LAST=99999;unsigned long CURRENT_MILLIS;float CURRENT_CALCULATED;float CURRENT_MAP = 10; //default current map calibration needed//buttonsboolean BUTTON_PRESSED = false;unsigned long BUTTON_MILLIS = false;byte BUTTON_LAST;boolean SETUP_MODE = false;byte SETUP_ITEM;boolean SETUP_DELAYBEEP;unsigned long MILLIS;unsigned long SETUP_BLINKMILLIS;boolean SETUP_BLINKSTATE;//parametersLiquidCrystal lcd(12, 11, 5, 4, 3, 2); // Cria um objeto LCD e atribui os pinosvoid setup() {//configure pinspinMode(PIN_BUZZER, OUTPUT);pinMode(PIN_VOLTAGE, INPUT);pinMode(PIN_CURRENT, INPUT);pinMode(PIN_BUTTON_UP, INPUT);pinMode(PIN_BUTTON_SETUP, INPUT);pinMode(PIN_BUTTON_DOWN, INPUT);pinMode (fan, OUTPUT); // Define o pino 13 como saídalcd.begin(16, 2); // Define o display com 16 colunas e 2 linhaspinMode (protecao, OUTPUT);#ifdef DEBUGdelay(2000);lcd.setCursor(0, 1);lcd.print("Debug enabled! ");Serial.begin(9600);Serial.println("Debug messages:");Serial.println("—————————-");#endif//try to load the configurationloadConfiguration();//show initial message for a while then clear and beep//delay(2000);lcd.clear();showLabels();//beepbeepStart();}void loop() {/*Para evitar as grandes variações de leitura do componenteLM35 são feitas 6 leitura é o menor valor lido prevalece*/processButtons();MILLIS = millis();valorTemp = INT_MAX; // Inicializando a variável com o maior valor int possívelfor (int i = 1; i <= 6; i++) { // Lendo o valor do sensor de temperaturavalorSensorTemp = analogRead(sensorTemp);valorSensorTemp *= 0.54 ; // Transforma o valor lido do sensor de temperatura em graus Celsius aproximadosif (valorSensorTemp 35) // Indica condição para acionamento do cooler{lcd.setCursor(0,1); // Define a coluna 0 e linha 1 do LCD onde será impresso a stringlcd.write("Fan ligado !"); // Imprime no LCDdigitalWrite(fan, HIGH); // Quando condição verdadeira, liga o cooler}else{lcd.setCursor(0,1);lcd.write("Fan desligado !");digitalWrite(fan, LOW); // Desliga cooler quando este estiver a baixo da valorTemp, indicando no LCD que esta desligado}delay(1000);// Exibindo valor da leitura do sensor de temperatura no display LCDlcd.clear(); // Limpa o display do LCDlcd.print("Temperatura:"); // Imprime a string no display do LCDlcd.print(valorTemp);lcd.write(B11011111); // Símbolo de graus Celsiuslcd.print("C");delay(1000); // Aguarda 2 segundosif ( (MILLIS – VOLTAGE_MILLIS) >= SENSOR_INTERVAL ){readVoltage();if (!SETUP_MODE || SETUP_ITEM!=1) {showVoltage();}VOLTAGE_MILLIS = MILLIS;}if ( (MILLIS – CURRENT_MILLIS) >= SENSOR_INTERVAL ){readCurrent();if (!SETUP_MODE || SETUP_ITEM!=2) {showCURRENT();}CURRENT_MILLIS = MILLIS;}if (SETUP_MODE){if ( (MILLIS – SETUP_BLINKMILLIS) >= SETUP_BLINKINTERVAL ){if (SETUP_BLINKSTATE){if (SETUP_ITEM==1)showVoltage();else if (SETUP_ITEM==2)showCURRENT();SETUP_BLINKSTATE = false;} else {if (SETUP_ITEM==1)hideVoltage();else if (SETUP_ITEM==2)hideCURRENT();SETUP_BLINKSTATE = true;}SETUP_BLINKMILLIS = MILLIS;}}}void processButtons(){if (digitalRead(PIN_BUTTON_UP) == HIGH){if (!BUTTON_PRESSED){#ifdef DEBUGshowDebug("Pressed UP");#endifBUTTON_LAST = PIN_BUTTON_UP;BUTTON_PRESSED = true;}}else if (digitalRead(PIN_BUTTON_SETUP) == HIGH){if (!BUTTON_PRESSED){#ifdef DEBUGshowDebug("Pressed SETUP");#endifbeepButton();BUTTON_LAST = PIN_BUTTON_SETUP;BUTTON_MILLIS = millis();BUTTON_PRESSED = true;SETUP_DELAYBEEP = false;} else {if ((millis() – BUTTON_MILLIS) > BUTTON_HOLDTIME)if (!SETUP_DELAYBEEP){beepButton();SETUP_DELAYBEEP = true;}}}else if (digitalRead(PIN_BUTTON_DOWN) == HIGH){if (!BUTTON_PRESSED){#ifdef DEBUGshowDebug("Pressed DOWN");#endifBUTTON_LAST = PIN_BUTTON_DOWN;BUTTON_PRESSED = true;}}else{if (BUTTON_PRESSED) {if (BUTTON_LAST == PIN_BUTTON_SETUP){#ifdef DEBUGshowDebug("Released SETUP");#endifif (!SETUP_MODE && (millis() – BUTTON_MILLIS) > BUTTON_HOLDTIME) {#ifdef DEBUGshowDebug("Entered setup mode!");#endiflcd.setCursor(0, 1);lcd.print(" Setup Mode ");SETUP_MODE = true;SETUP_ITEM = 1;}else {if (SETUP_ITEM == SETUP_MAXITEMS) {#ifdef DEBUGshowDebug("Exited setup mode!");#endifshowLabels();SETUP_MODE = false;SETUP_ITEM = 0;saveConfiguration();}else {SETUP_ITEM++;}showVoltage();showCURRENT();}}else if (BUTTON_LAST == PIN_BUTTON_UP) {#ifdef DEBUGshowDebug("Released UP");#endifif (SETUP_MODE) {beepButton();if (SETUP_ITEM==1) { //voltageVOLTAGE_MAP+=VOLTAGE_STEP;readVoltage();#ifdef DEBUGstartDebug("New VOLTAGE_MAP: ");Serial.println(VOLTAGE_MAP,6);#endif} else if (SETUP_ITEM==2) { //currentCURRENT_MAP+=CURRENT_STEP;readCurrent();#ifdef DEBUGstartDebug("New CURRENT_MAP: ");Serial.println(CURRENT_MAP,6);#endif}}}else if (BUTTON_LAST == PIN_BUTTON_DOWN) {#ifdef DEBUGshowDebug("Released DOWN");#endifif (SETUP_MODE) {beepButton();if (SETUP_ITEM==1) { //voltageVOLTAGE_MAP-=VOLTAGE_STEP;readVoltage();#ifdef DEBUGstartDebug("New VOLTAGE_MAP: ");Serial.println(VOLTAGE_MAP,6);#endif} else if (SETUP_ITEM==2) { //currentCURRENT_MAP-=CURRENT_STEP;readCurrent();#ifdef DEBUGstartDebug("New CURRENT_MAP: ");Serial.println(CURRENT_MAP,6);#endif}}}BUTTON_PRESSED = false;}}}#ifdef DEBUGvoid showDebug(char* Message){Serial.print(millis());Serial.print(": ");Serial.println(Message);}void startDebug(char* Message){Serial.print(millis());Serial.print(": ");Serial.print(Message);}#endifvoid showLabels(){//lcd.setCursor(0, 1);//lcd.print("Volts Amps"); comentado}void showVoltage(){lcd.clear();lcd.setCursor(0, 0);lcd.print(VOLTAGE_CALCULATED, 2);lcd.print(" V");//if (VOLTAGE_CALCULATED<10)//lcd.print("");}void hideVoltage(){//lcd.setCursor(0, 0);//lcd.print("");}void showCURRENT(){valorres = INT_MAX;valorreset = analogRead(res);valorreset /= 204.6;if (valorreset < valorres) {valorres = valorreset;}lcd.setCursor(9, 0);//if (CURRENT_CALCULATED0.5){digitalWrite(protecao, HIGH);while (valorres!=0){digitalWrite(protecao, HIGH);valorreset = analogRead(res);if (valorreset < valorres) {valorres = valorreset;}}digitalWrite(protecao, LOW);}else{//digitalWrite(protecao, LOW);}}void hideCURRENT(){//lcd.setCursor(9, 0);//lcd.print("");}void beepStart(){for (int i=0; i<300; i++) {digitalWrite(PIN_BUZZER, HIGH);delayMicroseconds(200);digitalWrite(PIN_BUZZER, LOW);delayMicroseconds(200);}}void beepButton(){for (int i=0; i<20; i++) {digitalWrite(PIN_BUZZER, HIGH);delayMicroseconds(700);digitalWrite(PIN_BUZZER, LOW);delayMicroseconds(700);}}void readVoltage(){VOLTAGE_CURRENT = analogRead(PIN_VOLTAGE);if ( VOLTAGE_CURRENT != VOLTAGE_LAST || SETUP_MODE ) {VOLTAGE_LAST = VOLTAGE_CURRENT;VOLTAGE_CALCULATED = fmap(VOLTAGE_CURRENT, 0, 1023, 0.0, VOLTAGE_MAP);#ifdef DEBUGif (!SETUP_MODE){startDebug("New voltage: ");Serial.print(VOLTAGE_CALCULATED);Serial.println("V");}#endif}}void readCurrent(){CURRENT_CURRENT = analogRead(PIN_CURRENT);if ( CURRENT_CURRENT != CURRENT_LAST || SETUP_MODE ) {CURRENT_LAST = CURRENT_CURRENT;CURRENT_CALCULATED = fmap(CURRENT_CURRENT, 0, 1023, 0.0, CURRENT_MAP);#ifdef DEBUGif (!SETUP_MODE){startDebug("New current: ");Serial.print(CURRENT_CALCULATED);Serial.println("A");}#endif}}float fmap(float x, float in_min, float in_max, float out_min, float out_max){return (x – in_min) * (out_max – out_min) / (in_max – in_min) + out_min;}int EEPROM_writeConf(){byte Address = EEPROM_CONFIGADDRESS;const byte* p = (const byte*)(const void*)&EEPROM_DATA;int i;for (i = 0; i < sizeof(EEPROM_DATA); i++)EEPROM.write(Address++, *p++);return i;}int EEPROM_readConf(){byte Address = EEPROM_CONFIGADDRESS;byte* p = (byte*)(void*)&EEPROM_DATA;int i;for (i = 0; i < sizeof(EEPROM_DATA); i++)*p++ = EEPROM.read(Address++);return i;}void loadConfiguration(){//read data from eepromEEPROM_readConf();//verify validatorsif (EEPROM_DATA.Validator == EEPROM_VALIDATOR && EEPROM_DATA.ValidatorX2 == EEPROM_VALIDATOR*2){//copy dataVOLTAGE_MAP = EEPROM_DATA.VOLTAGE_MAP;CURRENT_MAP = EEPROM_DATA.CURRENT_MAP;#ifdef DEBUGshowDebug("Configuration loaded from EEPROM!");startDebug(" VOLTAGE_MAP: ");Serial.println(VOLTAGE_MAP,6);startDebug(" CURRENT_MAP: ");Serial.println(CURRENT_MAP,6);#endif} else {#ifdef DEBUGshowDebug("Configuration NOT loaded from EEPROM!");#endif}}void saveConfiguration(){if ( EEPROM_DATA.VOLTAGE_MAP != VOLTAGE_MAP ||EEPROM_DATA.CURRENT_MAP != CURRENT_MAP) {//copy validatorsEEPROM_DATA.Validator = EEPROM_VALIDATOR;EEPROM_DATA.ValidatorX2 = EEPROM_VALIDATOR*2;//copy dataEEPROM_DATA.VOLTAGE_MAP = VOLTAGE_MAP;EEPROM_DATA.CURRENT_MAP = CURRENT_MAP;//save data to eepromEEPROM_writeConf();#ifdef DEBUGshowDebug("Configuration saved!");#endif} else {#ifdef DEBUGshowDebug("Configuration not changed!");#endif}}

Sobre o Clube do Hardware

No ar desde 1996, o Clube do Hardware é uma das maiores, mais antigas e mais respeitadas comunidades sobre tecnologia do Brasil. Leia mais

Direitos autorais

Não permitimos a cópia ou reprodução do conteúdo do nosso site, fórum, newsletters e redes sociais, mesmo citando-se a fonte. Leia mais

×
×
  • Criar novo...