アメブロでArduinoソースコードを見やすく表示する方法 その2 | トドお父さん通信

トドお父さん通信

北部九州在住 高BMI中高年のオタク趣味の活動記録

こないだの Arduinoこたつタイマーのプログラム、インデントがなくて分りにくかったですね。
プログラムをブログにインデント付きで分り易く表示する方法をググりました。
先の方法より、もっと簡単な方法はないのか? とさらにググりました。
SAKULIFE
 さん

ソースコードを表示するにはhighlight.jsが軽量ですごくいい!

Crayonとも互換性あり

とか、色々調べてたら、Twitterで貴重なアドバイスをゲト!

外部のサイト使わなくても、IDEの編集メニューに「HTML用にコピー」とかありませんでしたっけ?

それ使ってテキスト編集画面に貼り付けたら良いんじゃないかと(^^;;


あっ、そんな簡単な方法があったんですね。初心者なので知りませんでした。

 

やってみましたよ、ハイライトもインデントもArduino開発環境のIDEの表示のまま、

 

IDE画面を立ち上げ、ソースコードを表示している状態で

 

編集 ⇒ HTML形式でコピーする  

簡単にコピペできました!

 

まりす @maris_HY  さん、ありがとうございました!

以下、こたつタイマーのプログラムですよ、見え方がいいですね。

 

それでは、まったねー。

const int cathode_pins[] = {1,2,3,4,5,0,6};    // カソードに接続
const int buttonPin = 7;    // the number of the pushbutton pin
const int LED1  = 13 ;    // LED blink 1sec duration
const int speker = 8 ; //speker on pin 8
const int digit1 = 9; // Display digit 1
const int digit2 = 10; // Display digit 2

const int RELAY_ON = 12; // AC Relay Control
//int ledState = HIGH;         // the current state of the output pin

int k=0 ;  // Buzzer counter
int Count_up =0 ; // Count up state 
int buttonState;             // the current reading from the input pin
int lastButtonState = LOW;   // the previous reading from the input pin

const int number_of_cathode_pins = sizeof(cathode_pins) / sizeof(cathode_pins[0]);// 配列の数
int start_num= 20  ;  // Number to countdown from 20min=20*600sec initial 2HOURS
int start_num1 = start_num;  

// the following variables are long's because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long lastDebounceTime = 0;  // the last time the output pin was toggled
long debounceDelay = 50;    // the debounce time; increase if the output flickers
unsigned long time;

// setup() は,最初に一度だけ実行される
void setup() {
  for (int i = 0; i < number_of_cathode_pins; i++) {
    pinMode(cathode_pins[i], OUTPUT);  // cathode_pinsを出力モードに設定する
  }
  pinMode(digit1, OUTPUT);
  pinMode(digit2, OUTPUT);

  pinMode(speker, OUTPUT); // speker on pin 8
  pinMode(RELAY_ON, OUTPUT); 
  pinMode(LED1, OUTPUT); 
  pinMode(buttonPin,INPUT);  // Button Input
  
      // set initial AC Relay State
  digitalWrite(RELAY_ON,HIGH);
}



void loop() {
  
    // read the state of the switch into a local variable:
   int reading = digitalRead(buttonPin);

  // If the switch changed, due to noise or pressing:
   if (reading != lastButtonState) {
     // reset the debouncing timer
     lastDebounceTime = millis();
   } 
   
     if ((millis() - lastDebounceTime) > debounceDelay) {
     // whatever the reading is at, it's been there for longer
     // than the debounce delay, so take it as the actual current state:

     // if the button state has changed:
    if (reading != buttonState) {
       buttonState = reading;

       // only increment ciunrter if the new button state is HIGH
       if (buttonState == HIGH) {
//         ledState = !ledState;
//     If button is pressed, increase timer number 
         if (Count_up == 0 ) {
         start_num = start_num + 5 ;// If button is pushed add 30min
//             if ((start_num -  (millis()/1000))> 50) { 
//              start_num = 50 + (millis()/1000) ;
//         360sec =6min  Upper Limit = 50 * 6min = 300min (5 Hourrs)
           if (start_num - (millis()/360000) > 50 ) { 
           start_num = (50 + (millis()/360000)) ;
            }
         }
//     If state is count up, restart from initial.
         else
         {
         Count_up = 0;
         k = 0;      // Buzzer is Initialized
         digitalWrite(RELAY_ON,HIGH); // Power_Relay is ON
//         start_num = 10 + (millis()/1000);
//      restart from 10 x 6min = 1 Hour
         start_num = 10 + (millis()/360000);
         }
 
  
       }
     }
   }
   
           // save the reading.  Next time through the loop,
   // it'll be the lastButtonState:
        lastButtonState = reading;
  //start_num limitation 50
 
  //long startTime = millis();
//  if((millis()/1000) < start_num){
//    displayNumber(start_num -(millis()/1000));
// 360sec =6 min =36000 millis() count 
  if((millis()/360000) < start_num){
    displayNumber(start_num -(millis()/360000)); 
    if ((millis()/500)% 2 == 1 ) {
      digitalWrite(LED1, HIGH); 
    }
    else {
      digitalWrite(LED1, LOW);  
    }
  }
  else  {
    // reached zero, flash the display
    Count_up =1; // count up state
    k=k+1;   // count_up buzzer count
    time=millis();
    while(millis() < time+200) {
      displayNumber(0);  // display 0 for 0.2 second
      digitalWrite(RELAY_ON,LOW); //TIME UP Relay OFF
     if(k<6) { tone (8, 1000, 100);
     }
    }
    time=millis();    
    while(millis() < time+200) {
      lightNumber(10);  // Turn display off for 0.2 second
    }
  }  

}


// 2ケタの表示を計算する
void displayNumber(int toDisplay) {

  long beginTime = millis();

  for(int digit = 2 ; digit > 0 ; digit--) {

    //Turn on a digit for a short amount of time
    switch(digit) {
    case 1:
      digitalWrite(digit1, HIGH);
      break;    
    case 2:
      digitalWrite(digit2, HIGH);
      break;

    }

//Turn on the right segments for this digit
//    lightNumber(toDisplay % 6);
//    toDisplay /= 6;
    lightNumber(toDisplay % 10);
    toDisplay /= 10;

//    delayMicroseconds(DISPLAY_BRIGHTNESS); 
    //Display digit for fraction of a second (1us to 5000us, 500 is pretty good)

    //Turn off all segments
    delay(2) ;
    lightNumber(0); 

    //Turn off all digits
    digitalWrite(digit1, LOW);
    digitalWrite(digit2, LOW);
  }
//
//  while( (millis() - beginTime) < 10) ; 
  //Wait for 20ms to pass before we paint the display again
}

// 7SEG表示パターン
const int digits[] = {
  0b00111111, // 0
  0b00000110, // 1
  0b01011011, // 2
  0b01001111, // 3
  0b01100110, // 4
  0b01101101, // 5
  0b01111101, // 6
  0b00100111, // 7
  0b01111111, // 8
  0b01101111, // 9
  0b00000000, // 10
};
 // 1けたの数字(n)を表示する
void lightNumber (int n) {
  for (int i = 0; i < number_of_cathode_pins; i++) {
    digitalWrite(cathode_pins[i], digits[n] & (1 << i) ? LOW : HIGH);
  }
}