<file_sep># arduino_interactive
Ergebnisse der Creative Media Summer School an der FH St.Pölten im Jahr 2017
<file_sep>import { Component } from '@angular/core';
import { IonicPage, NavController, NavParams } from 'ionic-angular';

import { BluetoothSerial } from '@ionic-native/bluetooth-serial';
import { ToastController } from 'ionic-angular';

/**
 * Generated class for the FotomindMiniBtPage page.
 *
 * See http://ionicframework.com/docs/components/#navigation for more info
 * on Ionic pages and navigation.
 */
@IonicPage()
@Component({
  selector: 'page-fotomind-mini-bt',
  templateUrl: 'fotomind-mini-bt.html',
})
export class FotomindMiniBtPage {

  public status: string = "no";

  constructor(public navCtrl: NavController, public navParams: NavParams, public bluetoothSerial: BluetoothSerial, public toastCtrl: ToastController) {
  }

  ionViewDidLoad() {
    console.log('ionViewDidLoad FotomindMiniBtPage');
  }

  ionViewWillEnter() {
    console.log('ionViewWillEnter FotomindMiniBtPage');
    this.checkBluetooth('modi-manuell');
  }

  takePicture() {
    this.bluetoothSerial.write("trigger-camera\n");
  }

  checkBluetooth(modus){
    this.bluetoothSerial.isEnabled().then(
      (data)=> {
        console.log("Bluetooth is enabled!");
        this.bluetoothSerial.isConnected().then((data) => {
          this.bluetoothSerial.write(modus + '\n')
          this.subscribeBluetooth().subscribe(
            (data)=> {
              this.onDataReceived(data);
            }
          );
        });
      },
      (data)=> {
        console.log("Bluetooth is disabled!");
      });
  }

  subscribeBluetooth(){
      console.log("Subscribe");
      return this.bluetoothSerial.subscribe('\n');
  }

  onDataReceived(data) {
      console.log(data);
      if(data.indexOf("Modus") == 0){
        console.log(data.substr(data.indexOf("-")+1));
        this.status = data.substr(data.indexOf("-")+1);
      }
      if(data.indexOf("photo-taken") == 0){
        let toast = this.toastCtrl.create({
          message: 'Photo was taken successfully!',
          duration: 3000,
          position: 'bottom'
        });
        toast.present();
      }
  }

}
<file_sep>import { NgModule } from '@angular/core';
import { IonicPageModule } from 'ionic-angular';
import { ZeitrafferPage } from './zeitraffer';

@NgModule({
  declarations: [
    ZeitrafferPage,
  ],
  imports: [
    IonicPageModule.forChild(ZeitrafferPage),
  ],
  exports: [
    ZeitrafferPage
  ]
})
export class ZeitrafferPageModule {}
<file_sep>import { Component } from '@angular/core';
import { IonicPage, NavController, NavParams } from 'ionic-angular';

import { BluetoothSerial } from '@ionic-native/bluetooth-serial';
//import { FotomindMiniBtPage } from '../fotomind-mini-bt/fotomind-mini-bt';

/**
 * Generated class for the BluetoothPage page.
 *
 * See http://ionicframework.com/docs/components/#navigation for more info
 * on Ionic pages and navigation.
 */
@IonicPage()
@Component({
  selector: 'page-bluetooth',
  templateUrl: 'bluetooth.html',
})
export class BluetoothPage {

  public var2: string;
  public bluetoothDeviceList = [];

  constructor(public navCtrl: NavController, public navParams: NavParams, public bluetoothSerial: BluetoothSerial) {

    this.checkBluetooth();

  }

  ionViewDidLoad() {
    console.log('ionViewDidLoad BluetoothPage');
  }

  itemSelected(item: string) {
    this.connectBluetoothDevice(item).subscribe((data)=> {
      console.log("connected");
      this.bluetoothSerial.write('bt-connected\n')
      /*this.subscribeBluetooth().subscribe(
        (data)=> {
          this.onDataReceived(data);
        }
      );*/
      this.navCtrl.parent.select(1);
      //this.navCtrl.push(FotomindMiniBtPage);
    },
    (data)=> {
      console.log("not connected");
    });
  }

  checkBluetooth(){
    this.bluetoothSerial.isEnabled().then(
      (data)=> {
        console.log("Bluetooth is enabled!");
        this.getAllBluetoothDevices();
      },
      (data)=> {
        console.log("Bluetooth is diabled!");
        this.enableBluetooth();
      });
  }

  enableBluetooth(){
    this.bluetoothSerial.enable().then(
      (data)=> {
        console.log("Bluetooth is enabled!");
        this.getAllBluetoothDevices();
      },
      (data)=> {
        console.log("The user did *not* enable Bluetooth");
      });
  }

  isBluetoothConnected(){
    this.bluetoothSerial.isConnected().then((data) => {
      return true;
    },
    (data) => {
      return false;
    });
  }

  getAllBluetoothDevices(){
      this.bluetoothSerial.list().then((allDevices) => {
        this.bluetoothDeviceList = allDevices;
        if(!(this.bluetoothDeviceList.length > 0)){
          this.var2 = "could not find any bluetooth devices";
        }
      });
  }
  connectBluetoothDevice(deviceID) {
      console.log("Requesting connection to " + deviceID);
      return this.bluetoothSerial.connect(deviceID);
  }

  disconnectBluetooth() {
      console.log("trennen");
      this.bluetoothSerial.disconnect().then(
        (data)=> {
          console.log(data);
        });
  }

  subscribeBluetooth(){
      //bluetoothSerial.unsubscribe(console.log("Unsuscribe Success"), console.log("Unsuscribe Error"));
      console.log("Subscribe");
      return this.bluetoothSerial.subscribe('\n');
  }

  onDataReceived(data) {
      console.log(data);
      if(data.indexOf("Modus") == 0){
        console.log(data.substr(data.indexOf("-")+1));
        //$scope.setActualModus(message.substr(message.indexOf("-")+1));
      }
  }

}
<file_sep>import { NgModule } from '@angular/core';
import { IonicPageModule } from 'ionic-angular';
import { SelbstausloeserPage } from './selbstausloeser';

@NgModule({
  declarations: [
    SelbstausloeserPage,
  ],
  imports: [
    IonicPageModule.forChild(SelbstausloeserPage),
  ],
  exports: [
    SelbstausloeserPage
  ]
})
export class SelbstausloeserPageModule {}
<file_sep>import { Component } from '@angular/core';
import { IonicPage, NavController, NavParams } from 'ionic-angular';

import { BluetoothSerial } from '@ionic-native/bluetooth-serial';

/**
 * Generated class for the SelbstausloeserPage page.
 *
 * See http://ionicframework.com/docs/components/#navigation for more info
 * on Ionic pages and navigation.
 */
@IonicPage()
@Component({
  selector: 'page-selbstausloeser',
  templateUrl: 'selbstausloeser.html',
})
export class SelbstausloeserPage {

status: string = "no";
  timerDelay: number = 10;
  timerPictures: number = 1;

  constructor(public navCtrl: NavController, public navParams: NavParams, public bluetoothSerial: BluetoothSerial) {
  }

  ionViewDidLoad() {
    console.log('ionViewDidLoad SelbstausloeserPage');
  }

  ionViewWillEnter() {
    console.log('ionViewWillEnter SelbstausloeserPage');
    this.checkBluetooth('modi-timer');
  }

  startTimer() {
    console.log("timer-start+" + this.timerDelay + "#" + this.timerPictures + "\n");
    this.bluetoothSerial.write("timer-start+" + this.timerDelay + "#" + this.timerPictures + "\n");
  }

  checkBluetooth(modus){
    this.bluetoothSerial.isEnabled().then(
      (data)=> {
        console.log("Bluetooth is enabled!");
        this.bluetoothSerial.isConnected().then((data) => {
          this.bluetoothSerial.write(modus + '\n')
          this.subscribeBluetooth().subscribe(
            (data)=> {
              this.onDataReceived(data);
            }
          );
        });
      },
      (data)=> {
        console.log("Bluetooth is disabled!");
      });
  }

  subscribeBluetooth(){
      console.log("Subscribe");
      return this.bluetoothSerial.subscribe('\n');
  }

  onDataReceived(data) {
      console.log(data);
      if(data.indexOf("Modus") == 0){
        console.log(data.substr(data.indexOf("-")+1));
        this.status = data.substr(data.indexOf("-")+1);
      }
  }
}
<file_sep>import { Component } from '@angular/core';
import { IonicPage, NavController, NavParams } from 'ionic-angular';

import { BluetoothSerial } from '@ionic-native/bluetooth-serial';

/**
 * Generated class for the ZeitrafferPage page.
 *
 * See http://ionicframework.com/docs/components/#navigation for more info
 * on Ionic pages and navigation.
 */
@IonicPage()
@Component({
  selector: 'page-zeitraffer',
  templateUrl: 'zeitraffer.html',
})
export class ZeitrafferPage {

  status: string = "no";
  timelapseStatus: string = "inaktiv";
  timelapseDelayMinutes: number = 0;
  timelapseDelaySeconds: number = 10;
  timelapseDelay: number = 0;

  constructor(public navCtrl: NavController, public navParams: NavParams, public bluetoothSerial: BluetoothSerial) {
  }

  ionViewDidLoad() {
    console.log('ionViewDidLoad ZeitrafferPage');
  }

  ionViewWillEnter() {
    console.log('ionViewWillEnter ZeitrafferPage');
    this.checkBluetooth('modi-timelapse');
  }


  startTimelapse() {
    this.timelapseDelay = (this.timelapseDelaySeconds + (this.timelapseDelayMinutes * 60));
    console.log(this.timelapseDelay);

    this.bluetoothSerial.write("timelapse-start+" + this.timelapseDelay + "\n");
  }

  stopTimelapse() {
    this.bluetoothSerial.write("timelapse-stop\n");
  }

  checkBluetooth(modus){
    this.bluetoothSerial.isEnabled().then(
      (data)=> {
        console.log("Bluetooth is enabled!");
        this.bluetoothSerial.isConnected().then((data) => {
          this.bluetoothSerial.write(modus + '\n')
          this.subscribeBluetooth().subscribe(
            (data)=> {
              this.onDataReceived(data);
            }
          );
        });
      },
      (data)=> {
        console.log("Bluetooth is disabled!");
      });
  }

  subscribeBluetooth(){
      console.log("Subscribe");
      return this.bluetoothSerial.subscribe('\n');
  }

  onDataReceived(data) {
      console.log(data);
      if(data.indexOf("Modus") == 0){
        console.log(data.substr(data.indexOf("-")+1));
        this.status = data.substr(data.indexOf("-")+1);
      }
      if(data.indexOf("timelapse-active") == 0){
        this.setTimelapseStatus("Zeitraffer gestartet!");
        console.log(this.timelapseStatus);
      }
      else if(data.indexOf("timelapse-inactive") == 0){
        this.setTimelapseStatus("Zeitraffer gestoppt!");
        console.log(this.timelapseStatus);
      }
  }

  setTimelapseStatus(status){
    this.timelapseStatus = status;
  }

}
<file_sep>#include <Arduino.h>

//INCLUDES
#include <Wire.h>
#include <DS3231.h>

//PINS
const int ledYellowPin = 6;
const int ledGreenPin = 7;
const int potiPin = 2;
const int piezoPin = A0;
const int photoResPin = A1;
const int pirPin = 4;
const int flashPin = 8;
const int shutterPin = 9;
const int focusPin = 10;
const int buttonPin = 11;
const int switchPin = 12;
const int laserPin = 3;

//constant Variables
const int threshold = 500;

//Variables
int potiVal = 0, photoResVal;
int photoResThreshold = 600;
int sensorReading = 0;
int ledState = LOW;
int pirVal;
int shutterDelay = 0;
String message = "";
int buttonState = 0;
int switchState = 0;
int piezoVal = 0;
int lichtschrankeActive = 0;
String actualModi = "Manuell";
DS3231 clock;
RTCDateTime dt;
unsigned long timelapseDelay = 10, timelapseStart, actualTime, timeDiff;
unsigned long timerDelay = 3, timerStart, timerPictureCount = 0, timerPictures= 1;

bool timelapseActive = false, timerActive = false;

String watchControllTime, actualClock;

String test;


// the setup routine runs once when you press reset:
void setup() {
  // initialize
  pinMode(ledYellowPin, OUTPUT);
  pinMode(ledGreenPin, OUTPUT);
  pinMode(pirPin, INPUT);
  pinMode(potiPin, INPUT);
  pinMode(shutterPin, OUTPUT);
  pinMode(focusPin, OUTPUT);
  pinMode(buttonPin, INPUT);
  pinMode(switchPin, INPUT);
  pinMode(flashPin, OUTPUT);
  pinMode(laserPin, OUTPUT);
  Serial.begin(9600);

  clock.begin();
  clock.setDateTime(__DATE__, __TIME__);

  digitalWrite(ledYellowPin, HIGH);
  delay(100);
  digitalWrite(ledYellowPin, LOW);
  digitalWrite(ledGreenPin, HIGH);
  delay(100);
  digitalWrite(ledGreenPin, LOW);
  digitalWrite(ledYellowPin, HIGH);
  delay(100);
  digitalWrite(ledYellowPin, LOW);
  digitalWrite(ledGreenPin, HIGH);
  delay(100);
  digitalWrite(ledGreenPin, LOW);

  analogWrite(laserPin, 150);
  delay(200);
  analogWrite(laserPin, 0);

  actualModi = "Manuell";
  //actualModi = "Lichtschranke";
  //watchControllTime = "15:02";
  //timerStart = seconds();
  //timerPictures = 2;
  //timerPictureCount = 0;
}

// the loop routine runs over and over again forever:
void loop() {

  if(Serial.available()){
    message = Serial.readStringUntil('\n');
    //Serial.print(message + '\n');

    if (message == "trigger-camera")
    {
      triggerCamera(1000, shutterDelay);
    }
    else if (message == "modi-manuell")
    {
      setModi("Manuell");
    }
    else if (message.indexOf("modi-timelapse") == 0)
    {
      //Serial.print("Zeitraffer\n");
      setModi("Zeitraffer");
    }
    else if (message.indexOf("timelapse-start") == 0)
    {
      timelapseDelay = message.substring(message.indexOf('+') + 1).toInt();
      timelapseStart = seconds();
      timelapseActive = true;
      Serial.print("timelapse-started\n");
    }
    else if (message == "timelapse-stop")
    {
      timelapseActive = false;
      Serial.print("timelapse-stopped\n");
    }
    else if (message.indexOf("modi-timer") == 0)
    {
      setModi("Selbstausloeser");
    }
    else if (message.indexOf("timer-start") == 0)
    {
      timerDelay = message.substring(message.indexOf('+'), message.indexOf('#')).toInt();
      timerPictures = message.substring(message.indexOf('#') + 1).toInt();
      timerStart = seconds();
      timerPictureCount = 0;
      timerActive = true;
    }
    else if (message == "modi-watchcontroll+20:00")
    {
      setModi("Uhrzeitsteuerung");
      watchControllTime = message.substring(message.indexOf('+') + 1);
    }
  }

  actualTime = seconds();

  if(actualModi =="Zeitraffer"){
      switchState = digitalRead(switchPin);
      if(timelapseActive || switchState == HIGH){
          Serial.print("timelapse-active\n");
          timeDiff = actualTime - timelapseStart;
          if(timeDiff%timelapseDelay == 0){
            shootFotoWithoutFocus(500);
            delay(600);
          }
      }
      else{
          Serial.print("timelapse-inactive\n");
      }
  }
  else if(actualModi =="Uhrzeitsteuerung"){
     dt = clock.getDateTime();
     actualClock = clock.dateFormat("H:i", dt);
     //Serial.println(actualClock);
     //Serial.println(watchControllTime);
     if(actualClock == watchControllTime){
         shootFotoWithoutFocus(200);
     }
  }
  else if(actualModi =="Selbstausloeser" && timerActive){
    timeDiff = actualTime - timerStart;
      if(timeDiff%timerDelay == 0 && timeDiff > 0 && timerPictureCount < timerPictures){
        shootFotoWithoutFocus(200);
        delay(600);
        timerPictureCount++;
      }
      if(timerPictureCount == timerPictures){
          timerActive = false;
      }
  }
  else if(actualModi == "Lichtschranke"){
      buttonState = digitalRead(buttonPin);
      switchState = digitalRead(switchPin);

      photoResVal = analogRead(photoResPin);
      Serial.println(photoResVal);

      if (switchState == HIGH)
      {
          analogWrite(laserPin, 200);
      }
      else{
          analogWrite(laserPin, 0);
      }
      if (switchState == HIGH && buttonState == HIGH)
      {
          lichtschrankeActive = 1;
      }
      if (switchState == HIGH && lichtschrankeActive == 1){
          digitalWrite(shutterPin, HIGH);
          digitalWrite(ledGreenPin, HIGH);
          photoResVal = analogRead(photoResPin);
          Serial.print(photoResVal + '\n');
          if(photoResVal < photoResThreshold){
              delay(25);
              shootFlash();
              delay(100);
              digitalWrite(shutterPin, LOW);
              digitalWrite(ledGreenPin, LOW);
              lichtschrankeActive = 0;
          }
      }
      else{
          digitalWrite(shutterPin, LOW);
          digitalWrite(ledGreenPin, LOW);
      }
  }
  else if(actualModi == "Highspeed"){
      buttonState = digitalRead(buttonPin);
      switchState = digitalRead(switchPin);

      photoResVal = analogRead(photoResPin);
      Serial.println(photoResVal);

      if (switchState == HIGH)
      {
          analogWrite(laserPin, 200);
      }
      else{
          analogWrite(laserPin, 0);
      }
      if (switchState == HIGH && buttonState == HIGH)
      {
          lichtschrankeActive = 1;
      }
      if (switchState == HIGH && lichtschrankeActive == 1){
          if(photoResVal < photoResThreshold){
              digitalWrite(shutterPin, HIGH);
              digitalWrite(ledGreenPin, HIGH);
              delay(10);
              digitalWrite(shutterPin, LOW);
              digitalWrite(ledGreenPin, LOW);
              lichtschrankeActive = 0;
          }
      }
  }
  else{
    buttonState = digitalRead(buttonPin);
    switchState = digitalRead(switchPin);


    potiVal = analogRead(potiPin);
    shutterDelay = map(potiVal, 0, 1023, 0, 8000);
    //Serial.println(shutterDelay);

    piezoVal = 1024 - analogRead(piezoPin);

    pirVal = digitalRead(pirPin);
    //Serial.println(pirVal);

    if (buttonState == HIGH && switchState == HIGH)
    {
        //shootFlash();
        triggerCamera(1000, shutterDelay);
    }
  }
  delay(50);
  if(actualTime%3 == 0){
    Serial.print("Modus-" + actualModi + '\n');
  }
}

void setModi(String modus){
  actualModi = modus;
  Serial.print("Modus-" + actualModi + '\n');
}

unsigned long seconds(){
  return millis()/1000;
}

void shootFotoWithoutFocus(int shutterDelayTime){
  digitalWrite(shutterPin, HIGH);
  digitalWrite(ledGreenPin, HIGH);
  delay(shutterDelayTime);
  digitalWrite(ledGreenPin, LOW);
  digitalWrite(shutterPin, LOW);
  delay(100);
}

void shootFlash(){
  digitalWrite(flashPin, HIGH);
  digitalWrite(ledYellowPin, HIGH);
  delay(50);
  digitalWrite(ledYellowPin, LOW);
  digitalWrite(flashPin, LOW);
  delay(50);
}


void triggerCamera(int focusDelay, int shutterDelayTime){
  digitalWrite(focusPin, HIGH);
  digitalWrite(ledYellowPin, HIGH);
  delay(focusDelay);
  digitalWrite(focusPin, LOW);
  digitalWrite(ledYellowPin, LOW);
  delay(200);
  digitalWrite(shutterPin, HIGH);
  digitalWrite(ledGreenPin, HIGH);
  delay(shutterDelayTime);
  digitalWrite(ledGreenPin, LOW);
  digitalWrite(shutterPin, LOW);
  Serial.print("photo-taken\n");
  delay(200);
}
<file_sep>import { NgModule, ErrorHandler } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { IonicApp, IonicModule, IonicErrorHandler } from 'ionic-angular';
import { MyApp } from './app.component';

import { BluetoothPage } from '../pages/bluetooth/bluetooth';
import { FotomindMiniBtPage } from '../pages/fotomind-mini-bt/fotomind-mini-bt';
import { ZeitrafferPage } from '../pages/zeitraffer/zeitraffer';
import { SelbstausloeserPage } from '../pages/selbstausloeser/selbstausloeser';
import { TabsPage } from '../pages/tabs/tabs';

import { StatusBar } from '@ionic-native/status-bar';
import { SplashScreen } from '@ionic-native/splash-screen';

import { BluetoothSerial } from '@ionic-native/bluetooth-serial';
import { Toast } from '@ionic-native/toast';

@NgModule({
  declarations: [
    MyApp,
    BluetoothPage,
    FotomindMiniBtPage,
    ZeitrafferPage,
    SelbstausloeserPage,
    TabsPage
  ],
  imports: [
    BrowserModule,
    IonicModule.forRoot(MyApp)
  ],
  bootstrap: [IonicApp],
  entryComponents: [
    MyApp,
    BluetoothPage,
    FotomindMiniBtPage,
    ZeitrafferPage,
    SelbstausloeserPage,
    TabsPage
  ],
  providers: [
    StatusBar,
    SplashScreen,
    BluetoothSerial,
    Toast,
    {provide: ErrorHandler, useClass: IonicErrorHandler}
  ]
})
export class AppModule {}
<file_sep>import { Component } from '@angular/core';

import { BluetoothPage } from '../bluetooth/bluetooth';
import { FotomindMiniBtPage } from '../fotomind-mini-bt/fotomind-mini-bt';
import { ZeitrafferPage } from '../zeitraffer/zeitraffer';
import { SelbstausloeserPage } from '../selbstausloeser/selbstausloeser';

@Component({
  templateUrl: 'tabs.html'
})
export class TabsPage {

  tab1Root = BluetoothPage;
  tab2Root = FotomindMiniBtPage;
  tab3Root = ZeitrafferPage;
  tab4Root = SelbstausloeserPage;

  constructor() {

  }
}
