Giter VIP home page Giter VIP logo

Comments (1)

Nobutarou avatar Nobutarou commented on September 26, 2024
#define cbi(addr, bit) addr &= ~(1 << bit)  // addrのbit目を'0'にする
#define sbi(addr, bit) addr |= (1 << bit)   // addrのbit目を'1'にする
#define _PULSE_PIN 2

// シリアル通信について TinkerCad と実物で相違がある。
// Linux の改行が <LF><CR> だかで、文字+Enter という送信になってるのかも
// まあ Windows 使わないからいいや

const char PULSE_PIN = 2;
const char ANALOG_READ_PIN = A5;

void setup()
{
  Serial.begin(9600);
  Serial.setTimeout(2^32-1); //no timeout
  pinMode( PULSE_PIN , OUTPUT);
  digitalWrite(PULSE_PIN, LOW);

  // PIC と違い レジスタ名bits.ビット名での操作はできない。
  // & は 1&1=1, 1&0=0, 0&1=0, 0&0=0 
  // まず上位 5ビットを保持したまま、下位3ビットをクリア
  ADCSRA = ADCSRA & 0b11111000;

  // 第2 ビットを 1 に変える。
  // | or は 1&1=1, 1&0=1, 0&1=1, 0&0=0 だから、第2 ビット以外は元の値が保持される
  ADCSRA = ADCSRA | 0b00000010;
  discharge(1000);
}


void discharge(unsigned int td) { 
  digitalWrite(PULSE_PIN, LOW);
  delay(td);
}

unsigned long charge() {
//  digitalWrite(PULSE_PIN, HIGH);
  sbi(PORTD, PORTD2);
  return micros();
}

void loop() {
  double _R;
  _R=r_setup();

  // TinkerCad では文字を送る必要があったが、実物では Enter で反応する
  Serial.println("Enter to reset registor");

  // TinkerCad では不要だったけど、これがないと Serial.available()>0 になってしまう。
  Serial.end();
  Serial.begin(9600);
  
  while(Serial.available()==0){

    unsigned long time_start = charge();

    while ( analogRead(ANALOG_READ_PIN) < 647){};//1023*64.2=646.5。

    double T = micros() - time_start; // T: 時定数
    double c=T/_R; // [uF]
    unsigned int td=-_R*c*log(1.0/500.0)/1.0e3;
    td = (td>1000)*td + (td<=1000)*1000;
    Serial.println(td);
    discharge(td);
    Serial.println(c,16);
    digitalWrite(PULSE_PIN, LOW);
  }
  Serial.end();
  Serial.begin(9600);
}

double r_setup()
{
  double registor;
  double x; // general purpose
  short i; //
  char a; // general purpose
  
  Serial.println("Input the registor [Ohm]");
  Serial.println("Scientific notation only (ex, 1.23e-4)");

  while (Serial.available() == 0) {};
  x=Serial.parseFloat();
  a=Serial.read();
  i=Serial.parseInt();
  
  registor=x*pow(10.0,i);
  
  Serial.print("Registor: ");
  Serial.print(registor,16);
  Serial.println(" Ohm");
  return registor;
}

待ち時間が長いから付け替える方が便利かな。

from arduinodeasobu.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.