~alienagain/tejedora

3b38a2d579e0cb28ccdbbbc1ce451cfc261b44e0 — paula a month ago 739065e
adding code and wiring
2 files changed, 407 insertions(+), 0 deletions(-)

A code/tejedora.ino
A code/wiring.md
A code/tejedora.ino => code/tejedora.ino +386 -0
@@ 0,0 1,386 @@
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels

// Declaration for SSD1306 display connected using software SPI (default case):
#define OLED_MOSI   9
#define OLED_CLK   10
#define OLED_DC    11
#define OLED_CS    12
#define OLED_RESET 13
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT,
  OLED_MOSI, OLED_CLK, OLED_DC, OLED_RESET, OLED_CS);

#define NUMFLAKES     10 // Number of snowflakes in the animation example

#define LOGO_HEIGHT   16
#define LOGO_WIDTH    16


#define VRX_PIN  A0 // Arduino pin connected to VRX pin
#define VRY_PIN  A1 // Arduino pin connected to VRY pin

#define LEFT_THRESHOLD  400
#define RIGHT_THRESHOLD 800
#define UP_THRESHOLD    400
#define DOWN_THRESHOLD  800

#define COMMAND_NO     0x00
#define COMMAND_LEFT   0x01
#define COMMAND_RIGHT  0x02
#define COMMAND_UP     0x04
#define COMMAND_DOWN   0x08

int xValue = 0 ; // To store value of the X axis
int yValue = 0 ; // To store value of the Y axis
int command = COMMAND_NO;

static const unsigned char PROGMEM logo_bmp[] =
{ 0b00000000, 0b11000000,
  0b00000001, 0b11000000,
  0b00000001, 0b11000000,
  0b00000011, 0b11100000,
  0b11110011, 0b11100000,
  0b11111110, 0b11111000,
  0b01111110, 0b11111111,
  0b00110011, 0b10011111,
  0b00011111, 0b11111100,
  0b00001101, 0b01110000,
  0b00011011, 0b10100000,
  0b00111111, 0b11100000,
  0b00111111, 0b11110000,
  0b01111100, 0b11110000,
  0b01110000, 0b01110000,
  0b00000000, 0b00110000 };

void setup() {
  Serial.begin(9600);

  // SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally
  if(!display.begin(SSD1306_SWITCHCAPVCC)) {
    Serial.println(F("SSD1306 allocation failed"));
    for(;;); // Don't proceed, loop forever
  }

  // Show initial display buffer contents on the screen --
  // the library initializes this with an Adafruit splash screen.
  display.display();
  delay(2000); // Pause for 2 seconds

  // Clear the buffer
  display.clearDisplay();

  // Draw a single pixel in white
  //display.drawPixel(10, 10, SSD1306_WHITE);

  // Show the display buffer on the screen. You MUST call display() after
  // drawing commands to make them visible on screen!
  display.display();
  delay(2000);
  // display.display() is NOT necessary after every single drawing command,
  // unless that's what you want...rather, you can batch up a bunch of
  // drawing operations and then update the screen all at once by calling
  // display.display(). These examples demonstrate both approaches...



  testdrawstyles2("START");    // Draw 'stylized' characters


  display.invertDisplay(true);
  delay(1000);
  display.invertDisplay(false);
  delay(1000);

  //testanimate(logo_bmp, LOGO_WIDTH, LOGO_HEIGHT); // Animate bitmaps
}

// required variables for later
int counter = 0;
String operation = ".";
String last_c = "";

// this one is only set to true when DEL FUT is chosen
bool bomba = false;

void loop() {

// read analog X and Y analog values
  xValue = analogRead(VRX_PIN);
  yValue = analogRead(VRY_PIN);

  

  // converts the analog value to commands
  // reset commands
  command = COMMAND_NO;

  // check left/right commands
  if (xValue < LEFT_THRESHOLD)
    command = command | COMMAND_LEFT;
  else if (xValue > RIGHT_THRESHOLD)
    command = command | COMMAND_RIGHT;

  // check up/down commands
  if (yValue < UP_THRESHOLD)
    command = command | COMMAND_UP;
  else if (yValue > DOWN_THRESHOLD)
    command = command | COMMAND_DOWN;

  // NOTE: AT A TIME, THERE MAY BE NO COMMAND, ONE COMMAND OR TWO COMMANDS

  // HERE'S A MAP OF THE INSTRUCTIONS:
  /*
   *                               END OF MAP
   *                                    ^
   *                                    | 
   *                  FUTURE <-------   2 -----------> ALL
   *                                    |
   *              LAST OPERTATION    DELETE            1 KNOT  
   *                    ^                |               ^
   *                    |                v               |
   *   END OF MAP <---- 4 <---PRINT---- START ---ADD---> 1 -----> 2 KNOTS
   *                    |                 |              | 
   *                    v                 v              v
   *                ALL STACK    DOWN <-- 3 --> Up      3 KNOTS
   *                                      |
   *                                      v
   *                                  END OF MAP
   *                                  
   *    Note: every attempt of returning from 1,2,3 or 4 to START is considered a cancelled
   *    operation
   */

  // print command to serial and process command
  if (command & COMMAND_LEFT) {
    if (counter == 0 ) {
    
      testdrawstyles2("PRINT ");
      counter = counter+4;
    
    } else if (counter == 1 ){
      
        testdrawstyles2("CANCELLED");
        
        counter = 0;
        delay(2000);
        testdrawstyles2("START");

        
    } else if (counter == 2 ){
        testdrawstyles2("FUT");
        if (!bomba){
          operation = operation + "DEL FUT.";
          last_c = "DEL FUT";
          bomba = true;
        } else {
          bomba = false;
        }
        
        counter = 0;
        delay(2000);
        testdrawstyles2("START");
        
    } else if (counter == 3 ){
      testdrawstyles2("DOWN");
      if (!bomba){    
        operation = operation + "MOV DOWN.";
        last_c = "MOV DOWN.";
      }else{bomba = false;}
        
        counter = 0;
        delay(2000);
        testdrawstyles2("START");
        
    } else if (counter == 4 ){
        testdrawstyles2("END OF MAP");
        
        counter = 0;
        delay(2000);
        testdrawstyles2("START");
    }
  }
  // ADD ...
  if (command & COMMAND_RIGHT) {
    if (counter == 0 ) {
    
      testdrawstyles2("ADD ");
      counter = counter+1;
    
    } else if (counter == 1 ){
      testdrawstyles2("2 knots");
        if (!bomba){  
          operation = operation + "2k.";
          last_c = "ADD 2K";
        }else{bomba=false;}
        
        counter = 0;
        delay(2000);
        testdrawstyles2("START");

        
    } else if (counter == 2 ){
      testdrawstyles2("ALL.");
      if (!bomba){
        last_c = "DEL ALL";
        operation = " ";
      }else{bomba=false;}
        
        counter = 0;
        delay(2000);
        testdrawstyles2("START");
        
    } else if (counter == 3 ){
      testdrawstyles2("UP");
      if (!bomba){
        operation = operation + "MOV UP.";
        last_c = "MOV UP.";
      }else{bomba = false;}
      
        counter = 0;
        delay(2000);
        testdrawstyles2("START");
        
    } else if (counter == 4 ){
        testdrawstyles2("CANCELED...");
        
        counter = 0;
        delay(2000);
        testdrawstyles2("START");
    }
  }

  if (command & COMMAND_UP) {
    if (counter == 0 ) {
    
      testdrawstyles2("DEL ");
      counter = counter+2;
    
    } else if (counter == 1 ){
      if (!bomba){
        testdrawstyles2("1 knot");
        operation = operation + "1k.";
        last_c = "ADD 1K";
      }else{bomba=false;}
        
        counter = 0;
        delay(2000);
        testdrawstyles2("START");

        
    } else if (counter == 2 ){
        testdrawstyles2("END OF MAP");
        
        counter = 0;
        delay(2000);
        testdrawstyles2("START");
        
    } else if (counter == 3 ){
        testdrawstyles2("CANCELED...");
        
        counter = 0;
        delay(2000);
        testdrawstyles2("START");
        
    } else if (counter == 4 ){
        testdrawstyles2(last_c);
        
        counter = 0;
        delay(2000);
        testdrawstyles2("START");
    }
  }

  if (command & COMMAND_DOWN) {
    if (counter == 0 ) {
    
      testdrawstyles2("MOV ");
      counter = counter+3;
    
    } else if (counter == 1 ){
      testdrawstyles2("3 knots");
      if (!bomba){
        operation = operation + "3k.";
        last_c = "ADD 3K";
      }else{bomba=false;}
        
        counter = 0;
        delay(2000);
        testdrawstyles2("START");

        
    } else if (counter == 2 ){
        testdrawstyles2("CANCELED...");
        
        counter = 0;
        delay(2000);
        testdrawstyles2("START");
        
    } else if (counter == 3 ){
        testdrawstyles2("END OF MAP");
        
        counter = 0;
        delay(2000);
        testdrawstyles2("START");
        
    } else if (counter == 4 ){
        testdrawstyles2(operation);
        
        counter = 0;
        delay(5000);
        testdrawstyles2("START");
    }
  }

}



void testdrawchar(void) {
  display.clearDisplay();

  display.setTextSize(1);      // Normal 1:1 pixel scale
  display.setTextColor(SSD1306_WHITE); // Draw white text
  display.setCursor(0, 0);     // Start at top-left corner
  display.cp437(true);         // Use full 256 char 'Code Page 437' font

  // Not all the characters will fit on the display. This is normal.
  // Library will draw what it can and the rest will be clipped.
  for(int16_t i=0; i<256; i++) {
    if(i == '\n') display.write(' ');
    else          display.write(i);
  }

  display.display();
  delay(2000);
}


void testdrawstyles2(String mytest) {
  display.clearDisplay();

  display.setTextSize(2);             // Normal 1:1 pixel scale
  display.setTextColor(SSD1306_WHITE);        // Draw white text
  display.setCursor(0,0);             // Start at top-left corner
  display.println(mytest);

  display.display();
  delay(2000);
}


void testdrawstyles(void) {
  display.clearDisplay();

  display.setTextSize(2);             // Normal 1:1 pixel scale
  display.setTextColor(SSD1306_WHITE);        // Draw white text
  display.setCursor(0,0);             // Start at top-left corner
  display.println(F("Hello, world!"));

  display.display();
  delay(2000);
}

A code/wiring.md => code/wiring.md +21 -0
@@ 0,0 1,21 @@
# WIRING

ARDUINO UNO               SCREEN             

D9                       DATA
D10			 CLK
D11			 DC/SA0
D13			 RST
D12			 CS
5V			 VIN
GND			 GND

ARDUINO UNO             JOYSTICK
GND                       GND
5V                        5V
A0                        VRx
A1                        VRy
D2                        SW


Note: you will need a protoboard for the 5V