Analog/Joy Stick Programming?

First of, im a newbie. Like real newbie.

The issue:

So i have got an Keyes Sjoy Joystick/Analog stick.

HERE: Keyes Sjoy Product Page

I am currently trying to program it to affect the facial expression shown on my Nokia 5110 LCD display i got hooked up to my arduino

Unfortunately, when the program loads onto the arduino. The program doesnt work :confused:
Any ideas as to why guys.

Im not very good at forum posting and am new to all this so please excuse the lack of info or details :confused: I dont really know how to describe my issue very well.

Anyways the code was too long to post, i have attached the arduino project :confused:

LBandT.ino (9.03 KB)

The program doesnt work :/ Any ideas as to why guys.

Difficult without a clue as to what is wrong and seeing your whole program.

I can already see at least one thing wrong though.

if (vertical = 1023)

= is for assignment. == is for comparison

Thank you for that. I cant believe i didnt notice it :/

The issue i am having is as follows.

I have a FaceLoop void that cycles through various facial expressions using the adafruit gfx library. These then display on my nokia 5110 screen.

I want the analog stick to change the facial expression depending on the position that the analog stick is in. For instance Analog stick up will display a sneaky face on the lcd. etc

my code for the whole program i have so far is in the main post now Apologies if its messy

Okay, ive narrowed down my issues more with a bit of testing. If i create a seperate void called Analog Stick and call that in the loop on its own, it works

If i call the FaceLoop void at the same time within that loop, only the FaceLoop void works

(they're called "function"s, not "void"s) Can you post your code as it looks now? Try using ctrl-T before posting it

#include <SPI.h>
#include <Adafruit_GFX.h>
#include <Adafruit_PCD8544.h>
 
// pin 7 - Serial clock out (SCLK)
// pin 6 - Serial data out (DIN)
// pin 5 - Data/Command select (D/C)
// pin 4 - LCD chip select (CS)
// pin 3 - LCD reset (RST)
Adafruit_PCD8544 display = Adafruit_PCD8544(7, 6, 5, 4, 3);
 
#define LOGO16_GLCD_HEIGHT 16
#define LOGO16_GLCD_WIDTH  16

//############################ CONSTANTS #######################

const int VERT = 0; //ANALOG INPUT 515 neutral
const int HORIZ = 1; //ANALOG INPUT 510 neutral
const int SEL = 2; //DIGITAL not pressed neutral

//############################ VARIABLES ####################### 

int faceLoopEnable = 1;
int counter5 = 0; //Counters
int ct3 = 0;
int ct4 = 0;
int ct6 = 0;
int ct7 = 0;
int ct10 = 0;
int ctInactivity = 70;
int ctStartup = 9;
 
//##############################################################
//######################  SETUP VOID  ##########################
//##############################################################
 
void setup()  {
pinMode(SEL,INPUT);
digitalWrite(SEL,HIGH);
Serial.begin(9600);


// #####################LOADING SECTION ################################ 
display.begin();
 
display.setContrast(50); //Sets Display Contrast (Best viewing Setting)
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(BLACK);
display.setCursor(7,4); //Set Display Cursor Location to initialise typing
display.println("Initialising");
display.setCursor(24,16); //Shift Typing down 2 pixels x axis same y Position (X,y)
display.println("Robots");
display.display();
delay(2500);
 
display.drawRect(0, 26, 84, 20, BLACK);
display.display();
delay(200);  
display.drawRect(3, 28, 2, 16, BLACK);
display.display();
delay(200);
display.drawRect(5, 28, 2, 16, BLACK);
display.display();
delay(100);
display.drawRect(7, 28, 2, 16, BLACK);
display.display();
delay(500);
 
while (ctStartup < 45)
  {
    display.fillRect(ctStartup, 28, 4, 16, BLACK);
    display.display();
    ctStartup = (ctStartup + 4);        
  }
 
delay(600);
display.drawRect(45, 28, 2, 16, BLACK);
display.display();
delay(100);
display.drawRect(47, 28, 2, 16, BLACK);
display.display();
delay(800);
 
while (ctStartup < 80)
 {
    display.fillRect(ctStartup, 28, 4, 16, BLACK);
    display.display();
    ctStartup = (ctStartup + 4);    // Displays the cool loading animation uppon startup
    delay(500);
 }
 
delay(800);
display.clearDisplay(); //BATTLE ROBOTS Text
display.setTextSize(1); //Sets Larger text (MAY NEED TO CHANGE)
display.setTextColor(BLACK);
display.setCursor (25,12); //MAY NEED TO CHANGE TO FIT TEXT!!
display.println("BATTLE");
display.setCursor (25,20); //MAY NEED TO CHANGE!!!
display.println("ROBOTS");
display.setCursor (12,28);
display.println("INITIALISED");
display.display();
delay(2500);
}
//################################################################################### 


//########################## FACE LOOP DECLERATIONS #################################
void inactivityFaceLoop()
{
  if ((ctInactivity > 10) and (faceLoopEnable == 1))
  {
    if (ct10 == 0){Passive();}
    if (ct10 == 1){SneakyLeft();}
    if (ct10 == 2){SneakyRight();}
    if (ct10 == 3){SmileClosedEyes();}
    if (ct10 == 4){UpRight();}
    if (ct10 == 5){UpLeft();}  
    if (ct10 == 6){DownLeft();}
    if (ct10 == 7){DownRight();}
    if (ct10 == 8){EyeSmile();}
    if (ct10 == 9){SneakyRight();}      
   
    if (ct10 == 5)  
    {
      if ((ct7 == 0) or (ct7 == 6)) {SmileClosedEyes();}
      if ((ct7 > 0) or (ct7 < 6)) {EyeSmile();}      
    }
  }
}
void FaceLoop(void)
{
  Passive();
  delay(2000);  
  Smile();
  delay(2000);  
  EyeSmile();
  delay(2000);  
  BigSmileTeeth();  
  delay(2000);  
  BigSmile();
  delay(2000);  
  Suprised();
  delay(2000);
  SmileClosedEyes();
  delay(2000);
  Frown();
  delay(2000);
  FrownEyebrowDown();
  delay(2000);
  FrownXEyes();
   delay(2000);
  SneakyLeft();
  delay(2000);
  SneakyRight();
  delay(2000);
  UpRight();
  delay(2000);
  UpLeft();
  delay(2000);
  DownLeft();  
  delay(2000);
  DownRight();  
  delay(2000);  
}
 
  void Passive(void)
{
  display.clearDisplay();
  display.fillCircle(20, 15, 2, BLACK);
  display.fillCircle(64, 15, 2, BLACK);
  display.drawLine(36, 25, 50, 25, BLACK);
  display.display();
}
 
void Smile(void) {
  display.clearDisplay();
  display.fillCircle(20, 15, 2, BLACK);
  display.fillCircle(64, 15, 2, BLACK);
  display.fillCircle(43, 24, 8, BLACK);
  display.fillCircle(43, 21, 8, WHITE);
  display.display();}
 
void EyeSmile(void) {
  display.clearDisplay(); //Eye smile
  display.fillCircle(20, 10, 7, BLACK); //Eye
  display.fillCircle(64, 10, 7, BLACK); //Eye
  display.fillCircle(20, 5, 10, WHITE); //Eye
  display.fillCircle(64, 5, 10, WHITE); //Eye  
  display.fillCircle(43, 24, 8, BLACK);
  display.fillCircle(43, 20, 10, WHITE);
  display.display();}
 
void BigSmileTeeth(void) {  
  display.clearDisplay(); //Big Smile with teeth
  display.fillCircle(20, 15, 2, BLACK); //Eye
  display.fillCircle(64, 15, 2, BLACK); //Eye
  display.fillCircle(43, 24, 10, BLACK); //Black circle for mouth
  display.fillRect(33, 14, 21, 9,WHITE); //White rectangle to square the top of mouth
  display.fillRect(35, 24, 17, 2,WHITE); //Teeth
  display.display();}
 
void BigSmile(void) {
  display.clearDisplay(); //Big Smile
  display.fillCircle(20, 15, 2, BLACK);
  display.fillCircle(64, 15, 2, BLACK);
  display.fillCircle(43, 24, 8, BLACK);
  display.fillRect(35, 15, 17, 8,WHITE);
  display.display();}  
 
void Suprised(void) {
  display.clearDisplay();
  display.fillCircle(20, 15, 2, BLACK);
  display.fillCircle(64, 15, 2, BLACK);
  display.fillCircle(43, 26, 7, BLACK);
  display.fillCircle(43, 26, 5, WHITE);
  display.display(); }
 
void SmileClosedEyes(void) {
  display.clearDisplay();
  display.fillCircle(20, 16, 5, BLACK);
  display.fillCircle(64, 16, 5, BLACK);
  display.fillCircle(20, 20, 7, WHITE);
  display.fillCircle(64, 20, 7, WHITE);
  display.fillCircle(43, 24, 8, BLACK);
  display.fillCircle(43, 21, 8, WHITE);
  display.display(); }
 
void Frown(void) {
  display.clearDisplay();
  display.fillCircle(20, 15, 2, BLACK);
  display.fillCircle(64, 15, 2, BLACK);
  display.fillCircle(43, 34, 8, BLACK);
  display.fillCircle(43, 37, 8, WHITE);
  display.display(); }
 
void FrownEyebrowDown(void) {
  display.clearDisplay();
  display.fillCircle(20, 15, 2, BLACK);
  display.drawLine(17, 13, 23, 13, BLACK);    
  display.fillCircle(64, 15, 2, BLACK);
  display.drawLine(61, 13, 67, 13, BLACK);  
  display.fillCircle(43, 34, 8, BLACK);
  display.fillCircle(43, 37, 8, WHITE);
  display.display(); }
 
void FrownXEyes(void) {
  display.clearDisplay();
  display.drawLine(16, 11, 24, 19, BLACK);
  display.drawLine(60, 11, 68, 19, BLACK);
  display.drawLine(24, 11, 16, 19, BLACK);
  display.drawLine(68, 11, 60, 19, BLACK);
  display.fillCircle(43, 34, 8, BLACK);
  display.fillCircle(43, 37, 8, WHITE);
  display.display();}
 
void SneakyLeft(void) {
  display.clearDisplay();
  display.fillCircle(18, 15, 2, BLACK); //Eye
  display.drawLine(15, 13, 25, 13, BLACK);
  display.drawLine(15, 17, 25, 17, BLACK);  
  display.fillCircle(62, 15, 2, BLACK); //Eye
  display.drawLine(59, 13, 69, 13, BLACK);  
  display.drawLine(59, 17, 69, 17, BLACK);  
  display.drawLine(34, 25, 52, 25, BLACK); //Mouth
  display.display(); }
 
void SneakyRight(void) {
  display.clearDisplay();
  display.fillCircle(22, 15, 2, BLACK); //Eye
  display.drawLine(15, 13, 25, 13, BLACK);
  display.drawLine(15, 17, 25, 17, BLACK);  
  display.fillCircle(66, 15, 2, BLACK); //Eye
  display.drawLine(59, 13, 69, 13, BLACK);  
  display.drawLine(59, 17, 69, 17, BLACK);  
  display.drawLine(34, 25, 52, 25, BLACK); //Mouth
  display.display(); }
 
void UpRight(void) {
  display.clearDisplay(); // Look up right
  display.fillCircle(30, 7, 2, BLACK);
  display.fillCircle(74, 7, 2, BLACK);
  display.drawLine(36, 25, 57, 25, BLACK);
  display.display();}
 
void UpLeft(void) {
  display.clearDisplay(); // Look up left
  display.fillCircle(10, 7, 2, BLACK);
  display.fillCircle(54, 7, 2, BLACK);
  display.drawLine(28, 25, 50, 25, BLACK);
  display.display();}
 
void DownLeft(void) {  
  display.clearDisplay(); // Look down left
  display.fillCircle(10, 23, 2, BLACK);
  display.fillCircle(54, 23, 2, BLACK);
  display.drawLine(28, 35, 50, 35, BLACK);
  display.display();}
 
void DownRight(void) {  
  display.clearDisplay(); // Look down right
  display.fillCircle(30, 23, 2, BLACK);
  display.fillCircle(74, 23, 2, BLACK);
  display.drawLine(36, 35, 57, 35, BLACK);
  display.display(); }
  
//###################################################################################  
  
void AnalogStick(void) {
   int vertical, horizontal, select; //READS VALUES FROM JOYSTICK
  
  vertical = analogRead(VERT);
  horizontal = analogRead(HORIZ);
  select = digitalRead(SEL); // either High (1/off) LOW (0/on)
  
  if (vertical < 600)
  SneakyLeft();
  display.display();
  
  if (vertical > 500)
  SneakyRight();
  display.display();
 
}


void loop(){
  AnalogStick();
  FaceLoop();
}

NO!!!

void FaceLoop(void) { Passive(); delay(2000); Smile(); delay(2000); EyeSmile(); delay(2000); BigSmileTeeth(); delay(2000); BigSmile(); delay(2000); Suprised(); delay(2000); SmileClosedEyes(); delay(2000); Frown(); delay(2000); FrownEyebrowDown(); delay(2000); FrownXEyes(); delay(2000); SneakyLeft(); delay(2000); SneakyRight(); delay(2000); UpRight(); delay(2000); UpLeft(); delay(2000); DownLeft(); delay(2000); DownRight(); delay(2000); }

Please look at the example sketch *Blink Without Delay! * File -> Examples -> Digital

GAAAAAAAAAAAAAAAH! IM SO SORRY :'(

How am i meant to change the FaceLoop function into the Blink without delay? :/

Its late, and ive been working on this all day. Sorry for seeming thick :/

NO!!!

I agree, that is one ugly quote! 8)

Try this. It may be a little advanced, but the comments should help.

//-----------Functions---------------
void show1()
{
  Serial.println("One");
}

void show2()
{
  Serial.println("Two");
}

void show3()
{
  Serial.println("Three");
}

void show4()
{
  Serial.println("Four");
}
//------End of Functions------------

#define _Size 4 // number of functions to be used
#define interval 1000 // the interval to switch indexes at.

void (*Smile[_Size])(void) = {show1, show2, show3, show4}; // create a void pointer array of the functions
byte i = 0; //  index
unsigned long time = 0, C_time; // vars to keep track of time

void setup()
{
  // put your setup code here, to run once:
  Serial.begin(115200);
}

void loop()
{
  // put your main code here, to run repeatedly:
  if (((C_time = millis()) - time) >= interval)
  {
    if (i >= _Size) // check if i is greater than the max num of functions in Smile
      i = 0; // if so, then reset it back to zero
      
    Smile[i](); // execute the function @ index
    
    i++; // increment index
    time += interval; // increment time
    
    if(C_time < time) // checks for rollover
      time = 0; // if the millis() timer has rolled over, then reset it back to zero
  }
}

Thank you so much Hazards!!! ^^

Ive implemented that into my code now and it works great! I have sorted the analog stick issue for the minute too. I am just trying to figure out how i am going to control two motors with the joystick now :/ I definately underestimated this project and my coding isnt very efficient to say the least