WEEK 10

outputs






OUTPUTS







Use some output components connected to your board




For this assignment I have been playing with my Helloworld and some outputs, such as a piezo and a servo. I used Arduino IDE for programming since I am more familiar with it than programming in C. After that I started to play with a stepper and its driver, following some tutorial and understanding if I can use these output for my final project.

First Output = Piezo + Helloworld




I started connecting my helloboard to piezo I took from my Arduino Starter Kit, soldering a PIN from the ATTINY44 (2) with a jump wire and to ground. Since not so familiar with C language I used Arduino IDE, starting from some sketches on the library contening some melodies.
Here is the video of the outcome.






Since I did not use any library for the notes, I create references to connect the notes (a,b,c,d,e,f...) and the TIMEHIGH, which is the value the piezo needs. To know this value it is important to know the Frequency of the notes, and than do some calculation to obtain finally the TIMEHIGH. I have followed some tutorial and webpages for note frequencies and arduino melodies.










This is the script from Arduino. You can download also the script here.

int button = 3;
int led = 7;           // the PWM pin the LED is attached to
int brightness = 0;    // how bright the LED is
int fadeAmount = 5;    // how many points to fade the LED by 
int buttonState = 0;
int piezo = 2;
int length = 45;
char notes[] = "abcaccbabmbcdcddcbaaAgAgffedeafdecddcba "; // katusha
int beats[] = { 4, 1, 3, 2, 1, 1, 1, 1, 2, 4, 2, 1, 3, 1, 1, 1, 1, 1, 4, 2, 2, 2, 1, 1, 1, 1, 1, 1, 2, 4, 2, 1, 3, 1, 1, 1, 1, 1, 2};
int tempo = 150;

//______________________________________________

  void playTone(int tone, int duration) {
  for (long i = 0; i < duration * 1000L; i += tone * 2) {
    digitalWrite(piezo, HIGH);
    delayMicroseconds(tone);
    digitalWrite(piezo, LOW);
    delayMicroseconds(tone);
  }
}

void playNote(char note, int duration) {
  char names[] = { 'm', 'a', 'b', 'c' , 'd', 'e', 'f', 'g', 'A', 'B', 'C' };
  int tones[] = { 3030, 2272, 2024, 1915, 1700, 1519, 1432, 1275, 1136, 1014, 956 };

  // play the tone corresponding to the note name
  for (int i = 0; i < 8; i++) {
    if (names[i] == note) {
      playTone(tones[i], duration);
    }
  }
}


//______________________________________________

void setup() {
  // declare pin 9 to be an output:
  pinMode(led, OUTPUT);
  pinMode(button, INPUT);
  pinMode(piezo, OUTPUT);
}

void loop() {
  buttonState = digitalRead(button);
  if (buttonState == HIGH) {
  analogWrite(led, brightness);
  }

  // change the brightness for next time through the loop:
  brightness = brightness + fadeAmount;

  // reverse the direction of the fading at the ends of the fade:
  if (brightness == 0 || brightness == 255) {
    fadeAmount = -fadeAmount ;
  }
  // wait for 30 milliseconds to see the dimming effect
  delay(30);


  if (buttonState == LOW) {
    {

 digitalWrite(led, HIGH);
  delay(100);
  digitalWrite(led, LOW);
  delay(500);
   digitalWrite(led, HIGH);
  delay(100);
  digitalWrite(led, LOW);
  delay(500);
  digitalWrite(led, HIGH);
  delay(100);
  digitalWrite(led, LOW);
  delay(500);
  digitalWrite(led, HIGH);
  delay(100);
  digitalWrite(led, LOW);
  delay(500);
   digitalWrite(led, HIGH);
  delay(100);
   digitalWrite(led, LOW);
  delay(500);
   digitalWrite(led, HIGH);
  }

  for (int i = 0; i < length; i++)
  {if (notes[i] == ' ')
    {delay(beats[i] * tempo);}
    else {playNote(notes[i], beats[i] * tempo);
    }
     delay(tempo / 2);
  }
  }
}



.



Arduino + Driver UNL2003A + Stepper 28BYJ-48



I tried to go further and to use a driver and a stepper motor since my final project could use a lot of them. I followed a very nice and complete tutorial, but, unfortunately it is only in italian. Here is the link.
I also read the Datasheet for the step motor and for its driver.
I have understood that this stepper (UNIPOLAR) need to be programmed in a certain way, that why we also need the driver, which 4 pins are connected to our board. The are 4 magnets inside the step motor which we have to activate in the right sequence to make the stepper what we want to do. Because of the lack of enough pins on my helloboard, I used arduino. So I connected the 4 pins from the driver to 4 pins of arduino, in this case 8,9,10,11. Then I connected ground and vcc (5v). I have followed the Arduino script from the tutorial and loaded to my Arduino IDE.





//
// controllo motorino passo passo 28BYJ-48
// versione 1.0 del 24.09.14
// (c) 2014 Paolo Luongo
// http://aspettandoilbus.blogspot.com
//
// Versione per Arduino UNO R3
//

const int  IN1 = 8;   // filo motore BLUE
const int  IN2 = 9;   // filo motore ROSA
const int  IN3 = 10;  // filo motore GIALLO
const int  IN4 = 11;  // filo motore ARANCIO

const int MotoreOFF = 99; // motore spento

void Uscita( int i4, int i3, int i2, int i1)
{
  if (i1 == 1) digitalWrite(IN1, HIGH); else digitalWrite(IN1, LOW);
  if (i2 == 1) digitalWrite(IN2, HIGH); else digitalWrite(IN2, LOW);
  if (i3 == 1) digitalWrite(IN3, HIGH); else digitalWrite(IN3, LOW);
  if (i4 == 1) digitalWrite(IN4, HIGH); else digitalWrite(IN4, LOW);
}

void EseguiPasso(int stato)
{
  int i1, i2, i3, i4;

  switch ( stato )
  { // vedi tabella nel pdf del motore passo passo
    case 0: Uscita(0, 0, 0, 1); break;
    case 1: Uscita(0, 0, 1, 1); break;
    case 2: Uscita(0, 0, 1, 0); break;
    case 3: Uscita(0, 1, 1, 0); break;
    case 4: Uscita(0, 1, 0, 0); break;
    case 5: Uscita(1, 1, 0, 0); break;
    case 6: Uscita(1, 0, 0, 0); break;
    case 7: Uscita(1, 0, 0, 1); break;
    case MotoreOFF: //OFF
      Uscita(0, 0, 0, 0); break;
  }
  delay(1); //ritardo almeno 1 mS
}

void RitardoAccensione()
{ //attesa prima di attivare il motore
  EseguiPasso(MotoreOFF);
  for (int i = 0; i < 20; i++)
  {
    digitalWrite(13, HIGH);
    delay(250);
    digitalWrite(13, LOW);
    delay(250);
  }
}

void setup()
{
  pinMode(IN1, OUTPUT);
  pinMode(IN2, OUTPUT);
  pinMode(IN3, OUTPUT);
  pinMode(IN4, OUTPUT);
  pinMode(13, OUTPUT);
  RitardoAccensione();  //5 secondi di pausa prima di iniziare
}

void loop()
{
  int stato;

  stato = 0; //inizio da uno stato arbitrario
  digitalWrite(13, HIGH); //acceso LED13
  for (int k = 0; k < 4; k++) //Rotazione Oraria
  {
    for (int i = 0; i < 1024; i++) //90 gradi a ciclo
    {
      EseguiPasso(stato);
      stato += 1; // avanza nella tabella
      if ((stato) > 7) stato = 0;
    }
    EseguiPasso(MotoreOFF);
    delay(500);  // pausa di mezzo secondo
  }
  delay(1000); // pausa di 1 secondo
  digitalWrite(13, LOW); //spento LED13
  for (int k = 0; k < 4; k++) //Rotazione AntiOraria
  {
    for (int i = 0; i < 1024; i++) //90 gradi a ciclo
    {
      EseguiPasso(stato);
      stato -= 1; //torna indietro nella tabella
      if ((stato) < 0) stato = 7;
    }
    EseguiPasso(MotoreOFF);
    delay(500);
  }
  delay(1000);
}