Arduino Starter Package

Submitted by nvovn on Fri, 05/26/2017 - 07:48

Project 2: 1 Green LED, 2 Red LED & a Switch

In this project the switch toggles between green and the two alternatively lit red LED. In the manual this is done via a delay(250) call which is either dumb (busy waiting) and not lasting (it is repeated just one time). 

Here is the improved version which relies on the modulo operator and the millis() function. On top of that it minimises the number of writes to the ports, since I believe (http://forum.arduino.cc/index.php?topic=4324.0) writing to a port is far more expensive (in fact more then 20x) then checking an integer value...

// Copyright 2017 Norbert Vossiek, VP Consulting GmbH
// 
//    Licensed under the Apache License, Version 2.0 (the "License");
//    you may not use this file except in compliance with the License.
//    You may obtain a copy of the License at
// 
//      http://www.apache.org/licenses/LICENSE-2.0
// 
//    Unless required by applicable law or agreed to in writing, software
//    distributed under the License is distributed on an "AS IS" BASIS,
//    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//    See the License for the specific language governing permissions and
//    limitations under the License.

const int duration = 1000; // in milliseconds
int greenLED;
int redLEDToggle;

void setup() {
  
  // initialise pins (input/output)
  
  pinMode(3, OUTPUT);
  pinMode(4, OUTPUT);
  pinMode(5, OUTPUT);
  pinMode(2, INPUT);

  greenLED = LOW;
  
}

void loop() {

  // read pin 2, check if open and if pin status is open as well
  if (LOW == digitalRead(2)) {
    if (LOW == greenLED) {
      // then switch not pressed & branch not visited
      // green on & red off
      
      // first set state values
      greenLED = HIGH;
      redLEDToggle = LOW;
  
      // write status to pins
      digitalWrite(3, greenLED);
      digitalWrite(4, LOW);
      digitalWrite(5, LOW);
 
    }
  }
  else {
    // else the switch is pressed

    // if branch not visited, switch of green
    if (HIGH == greenLED) {
      // first set state array values
      greenLED = LOW;
      // green off
      digitalWrite(3, greenLED);
    }    
    // toggle red LEDs
    if (millis() % duration < duration / 2) {
      // if not visited
      if (LOW == redLEDToggle) {
        digitalWrite(4, HIGH);
        digitalWrite(5, LOW);
        redLEDToggle = HIGH;
      }
    }
    else {
      // if not visited
      if (HIGH == redLEDToggle) {
        digitalWrite(4, LOW);
        digitalWrite(5, HIGH);
        redLEDToggle = LOW;
      }
    }
  }
}

Project 3: Measuring Temperature

// Copyright 2017 Norbert Vossiek, VP Consulting GmbH 
// 
// Licensed under the Apache License, Version 2.0 (the "License"); 
// you may not use this file except in compliance with the License. 
// You may obtain a copy of the License at 
// 
// http://www.apache.org/licenses/LICENSE-2.0 
// 
// Unless required by applicable law or agreed to in writing, software 
// distributed under the License is distributed on an "AS IS" BASIS, 
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
// See the License for the specific language governing permissions and 
// limitations under the License.

/*
 * Difference to book version:
 * 
 * 1. put the setup and write functionality into functions
 * 2. extract constants, so they can be edited more easily
 * 3. convert display to integer value
 * 4. just enter the display part when somthing has changed (old value != new value)
 *
 */

const int sensorPin = A0;
const float baselineTemp = 23.;
const float shift = 3;
const bool debugMode = true;
const float factorVolt = 5./1024.;

int iOldTemperature = 0;

void digitalWriteAll(int a, int b,  int c) {
    digitalWrite(2, a);
    digitalWrite(3, b);
    digitalWrite(4, c);
}

void pinModeAll(void) {
    pinMode(2, OUTPUT);
    pinMode(3, OUTPUT);
    pinMode(4, OUTPUT);
}

void setup() {
    Serial.begin(9600);
    pinModeAll(); 
}

void loop() {
  
  int sensorValue = analogRead(sensorPin);
  float voltage = sensorValue * factorVolt;
  float temperature = (voltage -.5) * 100.;
  
  // round to int
  int iTemperature = temperature;

  // just change display if something happened...
  if (iOldTemperature != iTemperature) {
    if (debugMode) {
      Serial.print(":: Sensor Value: "); Serial.print(sensorValue);
      Serial.print(":: Voltage: "); Serial.print(voltage);
      Serial.print(":: temperature: "); Serial.print(iTemperature);
      Serial.print("\n");
    }

    iOldTemperature = iTemperature;
  
    if (iTemperature < baselineTemp) {
      digitalWriteAll(LOW, LOW, LOW);
    }
    else if (iTemperature < baselineTemp + shift) {
      digitalWriteAll(HIGH, LOW, LOW);
    }
    else if (iTemperature < baselineTemp + shift * 2) {
      digitalWriteAll(HIGH, HIGH, LOW);
    }
    else {
      digitalWriteAll(HIGH, HIGH, HIGH);
    }
  
  }
  // a minimum of 1 ms delay is suggested bcause otherwise the sensor
  // could show weird results
  delay(100);
  
}