Code from Arduino-Group Meetings

  • 16 Sep 2015 3:41 PM
    Message # 3529629

    2015 Aug 27 - Blinky

    /*
      Blink
      Turns on an LED on for one second, then off for one second, repeatedly.

      This example code is in the public domain.
    */

    // Pin 13 has an LED connected on most Arduino boards.
    // give it a name:
    // const int led = 13;
    #define led 13
    int count = 0;


    // The setup routine runs once when you press reset:
    void setup() {
       // Initialize the digital pin as an output.
       pinMode(led, OUTPUT);
       Serial.begin(9600);
      
    }

    // The loop routine runs over and over again, forever:
    void loop() {
       Serial.println("Blink!");
       digitalWrite(led, HIGH);   // turn the LED on (HIGH is the voltage level)
       delay(1000);

       digitalWrite(led, LOW);    // turn the LED off by making the voltage LOW
       delay(1000);
    }


  • 16 Sep 2015 3:50 PM
    Reply # 3529640 on 3529629

    2015 Sep 3 - LM35 Thermostat

    In this exercise, we built a heating thermostat, with an LED representing a furnace. I picked a setpoint of 30 degC so the ambient temperature would be below that, but we could force the sensor higher than that with the heat of our fingers.

    This evening's challenge: Alter the code to make a cooling, or air-conditioning, thermostat.

    // LM35Thermostat
    // MAxT Arduino, meeting 2, 9/3/15

    #define LM35PIN         A0 // [10 mv/degC]
    #define HVACPIN   7  // external LED
    #define LEDPIN    13 // external LED


    void setup(void){
       pinMode(LEDPIN, OUTPUT); // init LED output pin
       pinMode(HVACPIN,  OUTPUT); // init LED indicator

       analogReference(EXTERNAL);    // connected to +5v
       delay(1000);                 // let the hardware settle

       Serial.begin(9600);
    }


    void loop(void){
       uint16_t t_raw = 0;
       float t_degC = 0.0;

       t_raw = analogRead(LM35PIN);     // read raw temp, 1mv == 1degC
       t_degC = (float)t_raw * 500/1024.;
       Serial.print("t_raw= ");     Serial.print(t_raw);
       Serial.print(", T_degC= ");  Serial.println(t_degC);

       // If it's cold, turn the "heat" on
       // If it's hot,  turn the "heat" off
       // otherwise it must be in between; don't change it.
       if( t_degC < 30.0 ){
          digitalWrite(HVACPIN, HIGH);
       }
       else if( t_degC > 30.5 ){
          digitalWrite(HVACPIN, LOW);
       }

       delay(2000L);
    }




  • 16 Sep 2015 3:53 PM
    Reply # 3529643 on 3529629

    2015 Sep 9 - Blinky

    Review of Blinky (Aug 27) with three people who joined us for the first time this evening.

    Last modified: 16 Sep 2015 3:54 PM | Jeff Stone
  • 17 Sep 2015 6:13 PM
    Reply # 3531576 on 3529629

    17 Sept 2015
    In this exercise we move the temperature reading and conversion into a separate function. We also add an LED blink function.


    // LM35Thermostat with function calls
    // MAxT Arduino, meeting 4, 9/17/15

    #define LM35PIN         A0 // [10 mv/degC]
    #define HVACPIN   7  // external LED
    #define LEDPIN    13 // internal LED
    #define TONEPIN      12 // Piezo buzzer

    void nblink(uint8_t pin, uint8_t count);
    float lm35TemprC(uint8_t pin);


    void setup(void){
       pinMode(LEDPIN, OUTPUT); // init LED output pin
       pinMode(HVACPIN,  OUTPUT); // init LED indicator

       analogReference(EXTERNAL);    // connected to +5v
       delay(1000);                 // let the hardware settle

       Serial.begin(9600);
    }


    void loop(void){
       uint16_t tempC = lm35TemprC(LM35PIN);    // read the tempr

       // If it's cold, turn the heat on
       // If it's hot,  turn the heat off
       // otherwise it must be in between; don't change it.
       if( tempC < 30.0 ){
          digitalWrite(HVACPIN, HIGH);
       }
       else if( tempC > 30.5 ){
          digitalWrite(HVACPIN, LOW);
       }

       // Blink while the heat is on
       if( digitalRead(HVACPIN == HIGH) ){
          nblink(LEDPIN, 3);
       }
       delay(2000L);
    }

    // Return the temperature in degC from an LM35 digital thermometer
    float lm35TemprC(uint8_t pin)
    {
       uint16_t t_raw = 0;
       float t_degC = 0.0;

       t_raw = analogRead(LM35PIN);     // read raw temp, 1mv == 1degC
       t_degC = (float)t_raw * 500/1024.;

       Serial.print("t_raw= ");     Serial.print(t_raw);    // debug pring
       Serial.print(", T_degC= ");  Serial.println(t_degC);



       return(t_degC);
    }


    // Blink an LED a specified number of times
    void nblink(uint8_t pin, uint8_t count)
    {
       while( count-- != 0 ){
          digitalWrite(pin, HIGH);
          delay(50);
          digitalWrite(pin, LOW);
          delay(50);
       }
    }

    Last modified: 17 Sep 2015 6:17 PM | Jeff Stone
  • 01 Oct 2015 6:13 PM
    Reply # 3555863 on 3529629
    Here is the source code for October 1, 2015:


    // LM35 Temperature alarm
    // MAxT Arduino class, 10/1/15

    /*
     * This session builds on our LM35 thermostat code to add a couple of features
     * of the Arduino libraries and the C language:
     * - We will generate tones using a piezo-buzzer;
     * - We'll introduce structs, C's facility to create a data structure, a
     *   data-type composed of several pieces data of different type, and
     *   refer to it as a single entity;
     * - We'll introduce typedef, C's facility for naming (new) data types;
     * - And we'll use boolean (logic) variables.
     */



    /*****< Definitions >*****/
    // Macro to evaluate the number of things in an array, regardless of their type.
    #define countof(a) (sizeof(a)/sizeof(a[0]))

    // I/O pin assignments
    #define LM35PIN      A0      // [10 mv/degC]
    #define ALARMLEDPIN   7   // external LED
    #define LEDPIN    13      // internal LED
    #define TONEPIN      9       // piezo buzzer

    // The audio frequencies of a few musical notes from which we'll make a tune.
    #define NC6      1032
    #define ND6      1175
    #define NE6      1319
    #define NF6      1397
    #define NG6      1568
    #define NA6      1760

    #define HIGHTEMP  23.0

    // A trivial composite data structure to define a musical score.
    // Notice we've grouped an int and a byte into a new entity
    // called a NOTE_T.
    // "SOMETHING_T" is a convention for naming new data types.
    // A struct is a collection of data types grouped into
    // a new entity.
    typedef struct {
       uint16_t pitch;
       uint8_t beats;
    } NOTE_T;



    /*****< Function Declarations >*****/
    void playTune(NOTE_T tune[], uint8_t count);
    void nblink(uint8_t count, uint16_t on_ms, uint16_t off_ms);


    // Initialize our I/O
    void setup(void){
       pinMode(LEDPIN, OUTPUT); // init LED output pin
       pinMode(TONEPIN, OUTPUT);    // TONEPIN is one of the PWM pins.
       pinMode(ALARMLEDPIN, OUTPUT);

       analogReference(EXTERNAL);    // connected to +5v
       delay(1000);                 // let the hardware settle

       Serial.begin(9600);
    }


    // Start monitoring
    void loop(void){
       uint16_t t_raw = 0;
       float t_degC = 0.0;
       static bool did_alarm = false;

       static NOTE_T tune[] = {
          {NG6, 3},
          {NA6, 1},
          {NG6, 1},
          {NF6, 1},
          {NE6, 2},
          {NG6, 2},
          {NC6, 2}
       };

       t_raw = analogRead(LM35PIN);     // read raw temp, 1mv == 1degC
       t_degC = (float)t_raw * 500/1024.;
       Serial.print("t_raw= ");     Serial.print(t_raw);
       Serial.print(", T_degC= ");  Serial.println(t_degC);
       nblink(1, 150, 1);
      
       // Alarm if the temperature is too high
       if( t_degC > HIGHTEMP ){
          digitalWrite(ALARMLEDPIN, HIGH);

          // Sound alarm, but only once per overheat
          if( !did_alarm ){
             digitalWrite(ALARMLEDPIN, HIGH);
         playTune(tune, countof(tune));
         did_alarm = true;
          }
       }
       else if( t_degC < (HIGHTEMP - 1.0) ){
          digitalWrite(ALARMLEDPIN, LOW);
          did_alarm = false;
       }
      
       delay(3000L);
    }



    // Play a tune on the piezo buzzer
    void playTune(NOTE_T tune[], uint8_t count)
    {
       uint8_t i;
      
       for( i = 0; i < count; ++i ){
          tone(TONEPIN, tune[i].pitch);
          delay(300L * tune[i].beats);
          noTone(TONEPIN);
       }
    }


    // Blink the onboard LED
    void nblink(uint8_t count, uint16_t on_ms, uint16_t off_ms)
    {
       while( --count > 0 ){
          digitalWrite(LEDPIN, HIGH);
          delay((uint32_t)on_ms);
         
          digitalWrite(LEDPIN, LOW);
          delay((uint32_t)off_ms);
       }
    }


    Last modified: 01 Oct 2015 8:32 PM | Jeff Stone
Powered by Wild Apricot Membership Software