Skip to content

Physical Computing Lab: Analog In

September 20, 2011

In the video above, I walk through the steps in the Analog In lab and then show my creative application of the analogIn functionality of the Arduino. Here’s a look at the finished code I wrote from going through the lab:

#define LEDPIN 9

int potVal;
int brightVal;

void setup() {
  Serial.begin(9600);
  pinMode(LEDPIN, OUTPUT);  
}

void loop() {
  potVal = analogRead(0);
  brightVal = map(potVal, 450, 850, 0, 255);
  analogWrite(LEDPIN, brightVal);
  Serial.print("Photo cell: ");
  Serial.print(potVal);
  Serial.print("  mapped to:");
  Serial.println(brightVal);
}

To make the TV Etch-a-Sketch sketch, I used the example code from the TellyMate site and modified it to make this project. Below is the code I ended up with. The functions I kept from the example code are indicated by the comment after the loop().

#define CHAR_ESC "\x1B"

int x;
int y;

void setup()
{
  Serial.begin(57600); // set to 57600 baud
  screen_clear();
  cursor_show(false);
}

void loop()
{
  x = map(analogRead(0), 0, 1023, 0, 25); // determine x from the pot
  y = map(analogRead(1), 0, 1023, 0, 38); // determine y from the pot
  cursor_move( x , y ) ; // move to those coordinates
  Serial.print( '\xDB' ) ; // draw a block

}

// Helper functions from the TellyMate example code:

void cursor_move( uint8_t row , uint8_t col )
{ // <ESC>Yrc
  Serial.print( CHAR_ESC "Y" ) ;
  Serial.print((unsigned char)(32 + row)) ;
  Serial.print((unsigned char)(32 + col)) ;
}

void cursor_show( bool show )
{ // <ESC>e or <ESC>f
  Serial.print( CHAR_ESC ) ;
  Serial.print( show?'e':'f' ) ;
}

void screen_clear( void )
{ // <ESC>E
  Serial.print( CHAR_ESC "E" );
}
Advertisements
No comments yet

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: