O'zgaruvchini saqlash kerakmi?

I want to have a switch, that when on, will grab an analog number and essentially "freeze" it. Similarly to how STO> works on a TI83. What should I be using? Excuse my lack of technical terms, I'm not sure what to search and therefore do not know what to use. Thank you :)

0
Uni doimiy ravishda saqlang/muzlatib qo'ying, shuning uchun uni o'chirishdan keyin qaytarib olish mumkin yoki energiya yoqilganda uni xotirada ushlab turasizmi?
qo'shib qo'ydi muallif af., manba
Keyinchalik maxsus narsa qilishingizga hojat yo'q - bu do o'zgaruvchilar. Buni o'qing: int value = analogRead (...); . value endi siz o'qigan qiymatdan iborat.
qo'shib qo'ydi muallif af., manba
Faqat buni aytsangiz. Bu tugma bosilganda o'qishni faqatgina aytib berishning usulini o'ylay olasizmi? (Agar tugma bosilganda hech narsa qilishni aytib berishning usulini o'ylay olasizmi?)
qo'shib qo'ydi muallif af., manba
@MarkSmith uzr so'ragan bo'lishi kerak. Quvvat yoqilganda.
qo'shib qo'ydi muallif Luis Rojas, manba
@MarkSmith, lekin u doimo o'zgarib ketadigan analog kontentni o'qiydi. Men shuni xohlayman, shuning uchun men tugmani bosganimda, u raqamni egallaydi va analogdan o'qishni davom ettirmaydi
qo'shib qo'ydi muallif Luis Rojas, manba
@MarkSmith Men bir tugmani emas, balki bir tugmani ishlatishni rejalashtirmoqdaman, shuning uchun agar if ifodasini ishlatmoqchi bo'lsam, lekin u loop bo'ladi.
qo'shib qo'ydi muallif Luis Rojas, manba

6 javoblar

Kalitni kuzatish va boshqarishingiz kerak bo'lgan holatni boshqarishning oddiy usuli:

int analogPin = 0;
int switchPin = 3;
int switched = 0;//the last-known state of the switch
int lastRead = 0;//the last-desired analog sample

void setup() {
  pinMode(switchPin, INPUT_PULLUP);//connect switch to switchPin+GND w/o resistors
}

void loop() {

  if(digitalRead(switchPin)){//switch on:
    if(!switched){//switch on, but was unknown
      lastRead = analogRead(analogPin);//the wanted leading-edge value
      switched = 1;//seen it on
    }//end if switch changed?
  }else{//switch off:
    if(switched) switched = 0;     // remember that switch is off
    lastRead = analogRead(analogPin);//the latest value
  }


  Serial.println("Value is " + String(lastRead));
  delay(500);
}
1
qo'shib qo'ydi

Arduino'da EEPROM dan foydalanishingiz mumkin. Misol uchun:

#include 

int valAddr = 0;

#define analogPin 0
#define buttPin 3
void setup() {
  pinMode(buttPin,INPUT);
}

void loop() {
  /***
    Need to divide by 4 because analog inputs range from
    0 to 1023 and each byte of the EEPROM can only hold a
    value from 0 to 255.
  ***/
  if(digitalRead(buttPin)){
     int val = analogRead(analogPin)/4;
     EEPROM.write(valAddr, val);
     //access this value again by using int newvalue = EEPROM.read(valAddr)*4;

  }
}

By using EEPROM your value will be stored in non-volatile memory, meaning it will stay in memory even after a power cycle which is similar to how STO> works.

1
qo'shib qo'ydi
Arduino.cc saytidagi EEPROMWrite »sahifasida qayd etilganidek,« EEPROM xotirasi belgilangan 100.000 yozuv yozish/o'chirish davrining hayoti, shuning uchun siz unga qanchalik tez-tez yozishingiz haqida ehtiyot bo'lishingiz mumkin. "Yuqoridagi dastur ehtiyot emas. EEPROM boshiga 3,3 msda, yuqorida ko'rsatilgan dastur 100 tugmachani tugatadi, agar PIN 3 baland bo'lsa, taxminan 6 daqiqada valAddr yozadi.
qo'shib qo'ydi muallif Martin C. Martin, manba
Ha, albatta bunga ehtiyot bo'ling. Sizning tugmani arizangizda ushlab tursangiz, faqatgina bir marta yozganligiga ishonch hosil qilish uchun kod yozishingiz kerak va faqat tugmani bir marta bosishingiz kerak.
qo'shib qo'ydi muallif Randolf Richardson, manba

Analog kiritishni o'qing va uni doimiy saqlang. Kommutatsiya o'chirilgan bo'lsa, qiymati "dondur" ga yozing:

void loop(){
   int value, freeze;

   value = analogRead(ANALOGPIN);
   if( !digitalRead(SWITCHPIN) ){
      freeze = value;  //sw is off, upd freeze value
   }
}

Bu kalitni o'chirishni hisobga olmaydi. Analog inputning haqiqiy miqdorini va muzlatilgan chiqishi bilan nima qilmoqchiligingizni aniq belgilashga bog'liq bo'lishi mumkin yoki mumkin emas, chunki kalit zo'riqish inson tanqisligidan ancha qisqa bo'ladi, ammo MCU uchun 10 soniyadan kamroq vaqt o'zgarishlarni ko'rish va javob berish.

0
qo'shib qo'ydi

Faqat tugma bosilganda analog pinni o'qing.

#define analogPin 0
#define buttPin 3

int analogVal = 0;

void setup() {
  pinMode(buttPin,INPUT);
}

void loop() {
   //You can do stuff here

   //If the button is pressed...
    if (digitalRead(buttPin)
    {
       //..read the analogue pin
        analogVal = analogRead(analogPin)/4;
    }

   //You can do stuff here
}
0
qo'shib qo'ydi
"Erni bo'shatish" degani nimani anglatadi? Raqamni uzinglarmi? Doimiy o'qing
qo'shib qo'ydi muallif af., manba
Men tugmachani ishlatmayapman, men kalitni ishlataman. Men faqat avval kalit yoqilgan paytda raqamni muzlatib qo'yishni xohlayman va qachon yopilsa uni o'chiradi.
qo'shib qo'ydi muallif Luis Rojas, manba

mening analog yozuvimni doimiy ravishda o'qiydi.

oson:

  1. bepul ishlaydigan rejimda adcga ega va isr adc qiymatini buferga yozadi.

  2. tugmachaning holatini izrda tomosha qiling. agar u o'zgartirilsa, bufer qiymatini boshqa narsaga saqlang.

uni amalga oshirishning oddiy usuli bor, lekin undan kam real vaqt. ko'p baytlık qiymatga ega bo'lsa, atomlik uchun ehtiyot bo'lish kerak.

0
qo'shib qo'ydi

Mening tavsiyam:

  1. Configure an external interrupt on the raising edges of the input where the button/switch is wired. Note that only pins 2 and 3 of Uno are capable of external interrupts.

  2. Write a very simple ISR that just sets to True a global volatile variable (a flag to signal that the button/switch has been actuated) and does nothing else.

  3. In the loop() check if the flag variable has been changed by the interrupt. If that's the case, then:

    • Debounce/deglitch the button/switch (optional, but highly recommended).
    • Read the analog input of your choice and store its value or do whatever you want to do with it.
    • Reset the flag variable to False so it can be changed the next time the interrupt is fired.
0
qo'shib qo'ydi