Fun with Arduino 1


I got an Arduino Uno starter kit on loan from school for a physical computing project for the Experimental User Interfaces -course. To refresh my Arduino skills from last year (when I did a semi-big project with it on another course), I created a simple set-up and code for controlling LEDs with a potentiometer and buttons. Also the LEDs aren’t just toggled on or off, but instead the voltages are pulse-width modulated to achieve fading in and out.

Here’s the code:

const int ledPin1 = 11;
const int ledPin2 = 10;
const int ledPin3 = 9;
const int ledPin4 = 6;
const int ledPin5 = 5;

const int startPin = 2;
const int modePin = 3;
volatile int startState = 0;
volatile int modeState = 0;

int start = 0;
int mode = 0;

void setup() {
  Serial.begin(9600);
  pinMode(startPin, INPUT);
  attachInterrupt(0, start_ISR, RISING);
  attachInterrupt(1, mode_ISR, RISING);
}

void mode_ISR() {
  modeState = digitalRead(modePin);
  if (modeState == HIGH) {
    mode = (mode + 1) % 5; // 0-4
  }
  Serial.print("Mode: ");
  Serial.println(mode);
}

void start_ISR() {
  startState = digitalRead(startPin);
  if (startState == HIGH && start == 1) {
    start = 0;
  }
  else {
    start = 1;
  }
  Serial.print("Start: ");
  Serial.println(start);
}

// luetaan potikan arvo ja skaalataan
int nopeus() {
  float input = analogRead(A0); // lue arvo. Arvot 0-1023
  int nopeus = map(input, 0, 1023, 600, 16000);
  //Serial.print("Nopeus: ");
  //Serial.println(nopeus); // tulostetaan serial monitoriin
  return nopeus;
}

void fadeIN(int pin) {
  int nop = nopeus();
  for (int fadeValue = 0 ; fadeValue <= 255; fadeValue += 5) { 
    analogWrite(pin, fadeValue); 
    delayMicroseconds(nop / 2); } 
} 
void fadeOUT(int pin) { 
    int nop = nopeus(); 
    for (int fadeValue = 255 ; fadeValue >= 0; fadeValue -= 5) {
        analogWrite(pin, fadeValue);
        delayMicroseconds(nop / 2);
  }
}

void jono() {
  fadeIN(ledPin1);
  fadeIN(ledPin2);
  fadeIN(ledPin3);
  fadeIN(ledPin4);
  fadeIN(ledPin5);
  fadeOUT(ledPin1);
  fadeOUT(ledPin2);
  fadeOUT(ledPin3);
  fadeOUT(ledPin4);
  fadeOUT(ledPin5);
}

void chase() {
  int nop = nopeus();
  for (int fadeValue = 0 ; fadeValue <= 255; fadeValue += 5) {
    analogWrite(ledPin1, fadeValue);
    delayMicroseconds(nop / 2);
  }
  for (int fadeValue = 0 ; fadeValue <= 255; fadeValue += 5) {
    analogWrite(ledPin2, fadeValue);
    analogWrite(ledPin1, 255 - fadeValue);
    delayMicroseconds(nop / 2);
  }
  for (int fadeValue = 0 ; fadeValue <= 255; fadeValue += 5) {
    analogWrite(ledPin3, fadeValue);
    analogWrite(ledPin2, 255 - fadeValue);
    delayMicroseconds(nop / 2);
  }
  for (int fadeValue = 0 ; fadeValue <= 255; fadeValue += 5) {
    analogWrite(ledPin4, fadeValue);
    analogWrite(ledPin3, 255 - fadeValue);
    delayMicroseconds(nop / 2);
  }
  for (int fadeValue = 0 ; fadeValue <= 255; fadeValue += 5) {
    analogWrite(ledPin5, fadeValue);
    analogWrite(ledPin4, 255 - fadeValue);
    delayMicroseconds(nop / 2);
  }
  for (int fadeValue = 0 ; fadeValue <= 255; fadeValue += 5) {
    analogWrite(ledPin5, 255 - fadeValue);
    delayMicroseconds(nop / 2);
  }
}

void jokaToinen() {
  int nop = nopeus();
  for (int fadeValue = 0 ; fadeValue <= 255; fadeValue += 5) {
    analogWrite(ledPin1, fadeValue);
    analogWrite(ledPin3, fadeValue);
    analogWrite(ledPin5, fadeValue);
    delayMicroseconds(nop);
  }
  for (int fadeValue = 0 ; fadeValue <= 255; fadeValue += 5) {
    analogWrite(ledPin2, fadeValue);
    analogWrite(ledPin4, fadeValue);
    analogWrite(ledPin1, 255 - fadeValue);
    analogWrite(ledPin3, 255 - fadeValue);
    analogWrite(ledPin5, 255 - fadeValue);
    delayMicroseconds(nop);
  }
  for (int fadeValue = 0 ; fadeValue <= 255; fadeValue += 5) {
    analogWrite(ledPin2, 255 - fadeValue);
    analogWrite(ledPin4, 255 - fadeValue);
    delayMicroseconds(nop);
  }
}

void kaikki() {
  int nop = nopeus();
  for (int fadeValue = 0 ; fadeValue <= 255; fadeValue += 5) { 
    analogWrite(ledPin1, fadeValue); 
    analogWrite(ledPin2, fadeValue); 
    analogWrite(ledPin3, fadeValue); 
    analogWrite(ledPin4, fadeValue); 
    analogWrite(ledPin5, fadeValue); 
    delayMicroseconds(nop * 2);
  } 
  for (int fadeValue = 255 ; fadeValue >= 0; fadeValue -= 5) {
    analogWrite(ledPin1, fadeValue);
    analogWrite(ledPin2, fadeValue);
    analogWrite(ledPin3, fadeValue);
    analogWrite(ledPin4, fadeValue);
    analogWrite(ledPin5, fadeValue);
    delayMicroseconds(nop * 2);
  }
}

void Random() {
  for (int i = 0; i <= 2; i += 1) {
    switch (random(1, 12)) {
      case 1:
        fadeIN(ledPin1);
        delayMicroseconds(nopeus());
        fadeOUT(ledPin1);
        break;
      case 2:
        fadeIN(ledPin2);
        delayMicroseconds(nopeus());
        fadeOUT(ledPin2);
        break;
      case 3:
        fadeIN(ledPin3);
        delayMicroseconds(nopeus());
        fadeOUT(ledPin3);
        break;
      case 4:
        fadeIN(ledPin4);
        delayMicroseconds(nopeus());
        fadeOUT(ledPin4);
        break;
      case 5:
        fadeIN(ledPin5);
        delayMicroseconds(nopeus());
        fadeOUT(ledPin5);
        break;
      case 6:
        fadeIN(ledPin1);
        fadeIN(ledPin2);
        delayMicroseconds(nopeus());
        fadeOUT(ledPin1);
        fadeOUT(ledPin2);
        break;
      case 7:
        fadeIN(ledPin1);
        fadeIN(ledPin3);
        fadeIN(ledPin5);
        delayMicroseconds(nopeus());
        fadeOUT(ledPin3);
        fadeOUT(ledPin1);
        fadeOUT(ledPin5);
        break;
      case 8:
        fadeIN(ledPin3);
        fadeIN(ledPin2);
        delayMicroseconds(nopeus());
        fadeOUT(ledPin2);
        fadeOUT(ledPin3);
        break;
      case 9:
        fadeIN(ledPin2);
        fadeIN(ledPin4);
        delayMicroseconds(nopeus());
        fadeOUT(ledPin4);
        fadeOUT(ledPin2);
        break;
      case 10:
        fadeIN(ledPin1);
        fadeIN(ledPin5);
        fadeIN(ledPin4);
        fadeIN(ledPin2);
        delayMicroseconds(nopeus());
        fadeOUT(ledPin1);
        fadeOUT(ledPin4);
        fadeOUT(ledPin2);
        fadeOUT(ledPin5);
        break;
      case 11:
        fadeIN(ledPin1);
        fadeIN(ledPin5);
        delayMicroseconds(nopeus());
        fadeOUT(ledPin5);
        fadeOUT(ledPin1);
        break;
    }
  }
}
void loop() {
  if (start == 1) {
    switch (mode) {
      case 0:
        jono();
        break;
      case 1:
        chase();
        break;
      case 2:
        jokaToinen();
        break;
      case 3:
        kaikki();
        break;
      case 4:
        Random();
        break;
    }
  }
}

 


Leave a Reply

One thought on “Fun with Arduino