Friday, September 13, 2013

First Project

My First projects

I started at the start.

I added a RED LED and wrote a couple of lines to make Robbie flash :-)
Next I added a green one and made them alternate.
Already I was struggling with the construct of C++ (as it was very different to the code I was used to programming)

Next I made the LEDs dim slowly. I discovered this is how you also make a servo motor move, so I connected a small motor. OMG - my first Robot! - Robbie - you have an arm !
All of the above took me about a couple of hours to get working.

My next project was slightly more ambitious  A green LED flashes, as you move your hand over a light sensor, an LED flashes and a servo motor moves, then a Piezo plays a short tune. Sounds basic. Here is my first bit of code to make this work. (Skip on past if this is no interest to you). Robbie now has one arm, two eyes and a sad sounding voice box !







//#include <iostream>
//using namespace std;

//Darrens first Arduino C++ Program

//#define myConstVar 12345

//servo green to Power 3v
//servo orqnge to Pin 11
//servo red to GND
//Light Sensor Yellow to Analogue 0
//Light Sensor Red to 5v
//Light Sensor Black to Ground on Breadboard (stays)
//Sound White to 7
//Breadboard Blue to power GND
//Green LED Blue to 4
//red LED Blue to 12

 //Global Vars
//int a,b;
//unsigned int c; //Allows for c to be postive values only
//signed int d; //Allows for d to be negative and postive values
//String mystring(20);

//Config-Debug Vars
boolean lverbose = false; //True for debuf info

//Variables
int motordelay = 10;
int motormaxpos = 100;
int duration = 40; //Music
int notes[] = {
  261,293,329,349,392,440,493};
int a,loopcount,lightlevel,knock = 0; //Zero variables
int greenblinkdelay = 30;

//Pin Inputs
int ledGreen = 4;
int ledRed = 12;

//Pin Outputs
int piezo = 7;
int motorpin = 11;
  
void setup() {

  Serial.begin(9600);
  Serial.println("Setup Starts");
  loopcount = 0;
  //mystring = "some stuff";

  pinMode(ledRed, OUTPUT);
  pinMode(ledGreen, OUTPUT);
  pinMode(piezo, OUTPUT);

  //Code playing testing area
  //if (mystring.substring(1) == "stuff") {
  //the word 'stuff' is found after the position 1 of mystring
  //}
  //a = b = c = 5; //All values now equal 5

}

void loop () {
  //Stuff you want to run all the time

  //Serial.println("Loop Starts<<<<<<" + loopcount);

  loopcount++;

  // read the analog input on pin 0. If dark. Flash the RED LED and run the motor
  int lightlevel = analogRead(0);
  delay(10);

  blink_Greenled();

  if( loopcount % 20 == 10 && lverbose) // every 20 loops. show the loop count
  {
    Serial.print("Loop : ");
    Serial.println(loopcount);
  }

  if (lightlevel < 10) //Dark
  {

    blink_Redled(); 
    Motormove();
    Beep();
    RedOff();
  }
}

void Motormove() {
  Serial.println("Motor Move");
  byte pos = 0;
  while (pos < motormaxpos)
  {
    analogWrite(motorpin,pos);
    delay(motordelay);
    pos = pos + 1;
  }
  //Reset Motor Orientation
  analogWrite(motorpin,0);
  delay(motordelay);
}

void RedOn()
//Update the RED LED
{
  //Serial.println("Red ON");
  digitalWrite(ledRed, HIGH);
}

void RedOff()
//Update the RED LED
{
  if(lverbose)
  {
    Serial.println("Red OFF");
  }
  digitalWrite(ledRed, LOW);
}

void Beep()
{
  for (int i = 0;i < 7;i++)
  {
    tone(piezo, notes[i],duration);
    delay(duration);
  }
}

void blink_Redled()
//Update the RED LED
{
  if(lverbose)
  {
    Serial.println("Red ON");
  }

  digitalWrite(ledRed, HIGH);
  delay(500);   //half a second          
  if(lverbose)
  {
    Serial.println("Red OFF");
  }
  digitalWrite(ledRed, LOW); 
  //delay(100);             
}

//Update the Green LED
void blink_Greenled()
{
  if(lverbose)
  {
    Serial.println("Green ON");
  }
  digitalWrite(ledGreen, HIGH);
  delay(greenblinkdelay);             
  if(lverbose)
  {
    Serial.println("Green OFF");
  }

  digitalWrite(ledGreen, LOW); 
  delay(100);             
}

void myfunc()
{
  //Just a routine uses a glabal var 'a' to test variable scope in C++
  for (int i = 0; i < 20; i++) {
    a = a + 1;
  }
}

As I stated above - the first thing I did was write a program that could test the limitations of the device and ports. These I figured would be the key restrictions in terms of potential. I knew the device used C++ (of which I knew nothing). I have a great software developer background but mainly in 4GL and database orientated languages. So C++ was going to take be back to my roots (nearly machine language if you ask me!).

No comments:

Post a Comment