본문 바로가기

Arduino

superloop ADC

superloop ADC.ino

int potentiometer_en = 0;
int tmp36sensor_en = 0;
int photosensor_en = 0;
int distancesensor_en = 0;

void setup() {
  serial_setup();
//  BTserial_setup();  
  potentiometer_setup();
  tmp36sensor_setup();
  photosensor_setup();
  distancesensor_setup();
}

void loop() {
  serial_loop();
//  BTserial_loop();
  if(potentiometer_en == 1) 
    potentiometer_loop();
  if(tmp36sensor_en == 1) 
    tmp36sensor_loop();
  if(photosensor_en == 1) 
    photosensor_loop();
//  if(distancesensor_en == 1) 
//    distancesensor_loop();
}

potentiometer.ino

const int pot_analogInPin = A0;  
const int pot_analogOutPin = 11; 

int pot_sensorValue = 0;        
int pot_outputValue = 0;

long pot_previousMillis = 0;
long pot_interval = 2; 
long pot_prevPrint = 0;
long pot_printInterval = 1000;

void potentiometer_setup() {}

void potentiometer_loop() {
  unsigned long currentMillis = millis();
 
  if(currentMillis - pot_previousMillis >= pot_interval) { 
    pot_previousMillis = currentMillis;   

    pot_sensorValue = analogRead(pot_analogInPin);
    pot_outputValue = map(pot_sensorValue, 0, 1023, 0, 255);
    analogWrite(pot_analogOutPin, pot_outputValue);
    if(currentMillis-pot_prevPrint >= pot_printInterval){
      pot_prevPrint = currentMillis ;
    Serial.print("potentiometer value = " );
    Serial.print(pot_sensorValue);
    Serial.print(", led brightness = ");
    Serial.println(pot_outputValue);
    }
  }
}

temp36.ino

int tmp36sensorPin = A1;
int tmp36ledPin = 13; 
int tmp36sensorValue = 0;

long tmp36_previousMillis = 0;
long tmp36_interval = 0;
long tmp_prevPrint=0;
long tmp_printInterval=1000;

int tmp36sensor_ledState = 1;

void tmp36sensor_setup() {  
  pinMode(tmp36ledPin, OUTPUT);  
}

void tmp36sensor_loop() {
  unsigned long currentMillis = millis();
 
  if(currentMillis - tmp36_previousMillis >= tmp36_interval) { 
    tmp36_previousMillis = currentMillis;   

    if(tmp36sensor_ledState != 0) {
      tmp36sensorValue = analogRead(tmp36sensorPin);
      tmp36_interval = tmp36sensorValue;
      if(currentMillis - tmp_prevPrint >= tmp_printInterval){
        tmp_prevPrint = currentMillis ;      
      Serial.print("temperature sensor value = " ); 
      Serial.print(tmp36sensorValue);
      
      float voltage = (tmp36sensorValue/1024.0) * 5.0;   
      Serial.print(", volts = " );
      Serial.print(voltage);
      
      float temperature = (voltage - .5) * 100;    
      Serial.print(", degrees C = " );
      Serial.println(temperature);
      }
    }
    
    digitalWrite(tmp36ledPin, tmp36sensor_ledState);
    tmp36sensor_ledState = !tmp36sensor_ledState; 
  }
}

photosensor.ino

int photosensor_Pin = A2;    
int photosensor_ledPin[3] = {12,7,8}; 
int photosensor_Value = 0; 

long photosensor_previousMillis = 0;
long photosensor_interval = 1000;

void photosensor_setup() {
  for(int i=0;i<3;i++)  {
    pinMode(photosensor_ledPin[i], OUTPUT);
    digitalWrite(photosensor_ledPin[i],HIGH);
  }
}

void photosensor_loop() {
  unsigned long currentMillis = millis();
 
  if(currentMillis - photosensor_previousMillis >= photosensor_interval) { 
    photosensor_previousMillis = currentMillis;   

    photosensor_Value = analogRead(photosensor_Pin);
    Serial.print("photosensor value = " );
    Serial.println(photosensor_Value);
    
    if(photosensor_Value < 300){ 
      for(int i=0;i<3;i++) {
        digitalWrite(photosensor_ledPin[i], LOW);
      }
    }
    else{
      for(int i=0;i<3;i++){
        digitalWrite(photosensor_ledPin[i], HIGH);    
      }
    }
  }
}

serial.ino

void serial_showMenu() {
  Serial.println("1. Potentiomter Led Brightness Enable/Disable");
  Serial.println("2. Tmperature sensor Led Blink Enable/Disable");
  Serial.println("3. Photo Sensor RGB Led On/Off Enable/Disable");
//  Serial.println("4. Distance Sensor RGB Led Brighness Enable/Disable");
}

void serial_setup() {  
  Serial.begin(115200);
  serial_showMenu();
}

void serial_loop() {
  if (Serial.available() > 0) {
    int userCmd = Serial.read();

    switch(userCmd) {
      case '1': potentiometer_en = !potentiometer_en;    break;
      case '2': tmp36sensor_en = !tmp36sensor_en;        break; 
      case '3': photosensor_en = !photosensor_en;        break;
//      case '4': distancesensor_en = !distancesensor_en;  break;
      default: break;
    }
  }
}

'Arduino' 카테고리의 다른 글

superloop PWM  (0) 2022.03.22
superloop ADC_BT serial  (0) 2022.03.18
Interrupt를 이용한 btn_toggle  (0) 2022.03.17
BT input  (0) 2022.03.15
LED control with SerialRead  (0) 2022.03.14