Programming help

Hello all

First I must say is I’m not a programmer, just taking a basic logic class right now.
Have a project found on the net and trying to piece two pieces of code together to get it to do what I need.
The project is a set of robot eyes that use two 8x8(64) LED matrixes.
Below is part of the code, some is left out as I feel it is not needed and the forums will not let me post the full code.

The program as it is will run through the loop.
That places a eye like shape on the LED matrix (neutral()) then holds that for 4 sec. and then blinks (blink-eyes()) the eyes and continues through the loop.
All works ok for that.
What I am trying to do is add the “intake()” into the program so that I can send serial commands from another microcontroller.
I want the program to take the serial command (example the letter “D” in “intake” which sends it to “dizzy()”, which makes the eyes look dizzy and then return to the loop.
Hope this isn’t too confusing.

I have given it my best shot and just can’t figure it out.
Thought I’d ask you pros.

Thanks … Herr Ball (aka Ted)

Code:

#include "LedControl.h"
LedControl lc=LedControl(12,11,10,2);

#include <Servo.h>


int incomingByte;      // a variable to read incoming serial data 

void setup() {
  // initialize serial communication:
  Serial.begin(9600);
  
lc.shutdown(0,false);
lc.setIntensity(0,1);
lc.clearDisplay(0);

lc.shutdown(1,false);
lc.setIntensity(1,1);
lc.clearDisplay(1);
}

void loop() {
  scene();
    }

 void scene()
{
neutral();
delay(4000);
lc.clearDisplay(0);  
blink_eyes();
delay(4000);
lc.clearDisplay(0);
neutral();
delay(3000);
lc.clearDisplay(0);
blink_eyes();
delay(4000);
lc.clearDisplay(0);
blink_eyes();
delay(4000);
lc.clearDisplay(0);
neutral();
delay(3000);
lc.clearDisplay(0);
blink_eyes();
delay(4000);
lc.clearDisplay(0);
blink_eyes();
delay(4000);
lc.clearDisplay(0);
neutral();
delay(3000);
lc.clearDisplay(0);
blink_eyes();
delay(4000);
lc.clearDisplay(0);
blink_eyes();
delay(4000);
lc.clearDisplay(0);
neutral();
delay(3000);
lc.clearDisplay(0);
blink_eyes();
delay(4000);
lc.clearDisplay(0);
}

void intake() {
// see if there's incoming serial data:
  if (Serial.available() > 0) {
    // read the oldest byte in the serial buffer:
    incomingByte = Serial.read();
    // if it's a capital S (ASCII 83), go to void scene:
    if (incomingByte == 'S') scene();
    }
    // if it's a capital D (ASCII 68), go to void dizzy:
    if (incomingByte == 'D') dizzy();
    // if it's a capital V (ASCII 86), go to void suchen_V:
    if (incomingByte == 'V') suchen_V();         
    // if it's a capital 1 (ASCII 49), go to void upward:
    if (incomingByte == '1') upward();
    // if it's a capital 2 (ASCII 50), go to void right_upward:
    if (incomingByte == '2') right_upward();
    // if it's a capital 3 (ASCII 51), go to void right:
    if (incomingByte == '3') right();
    // if it's a capital 4 (ASCII 52), go to void right_downward:
    if (incomingByte == '4') right_downward();
    // if it's a capital 5 (ASCII 53), go to void downward:
    if (incomingByte == '5') downward();
    // if it's a capital 6 (ASCII 54), go to void left_downward:
    if (incomingByte == '6') left_downward();
    // if it's a capital 7 (ASCII 55), go to void left:
    if (incomingByte == '7') left();
    // if it's a capital 8 (ASCII 56), go to void left_upward:
    if (incomingByte == '8') left_upward();
    }

void neutral()
{
lc.setRow(0,1,60);
lc.setRow(0,2,126);
lc.setRow(0,3,102);
lc.setRow(0,4,102);
lc.setRow(0,5,126);
lc.setRow(0,6,60);
lc.setRow(1,1,60);
lc.setRow(1,2,126);
lc.setRow(1,3,102);
lc.setRow(1,4,102);
lc.setRow(1,5,126);
lc.setRow(1,6,60);
delay(50);
}

  
 void blink_eyes()
{
lc.setRow(0,1,60);     // parte 1
lc.setRow(0,2,126);
lc.setRow(0,3,102);
lc.setRow(0,4,102);
lc.setRow(0,5,126);
lc.setRow(0,6,60);
lc.setRow(1,1,60);
lc.setRow(1,2,126);
lc.setRow(1,3,102);
lc.setRow(1,4,102);
lc.setRow(1,5,126);
lc.setRow(1,6,60);
delay(10);
lc.clearDisplay(0);  

lc.setRow(0,1,60);     // parte 2
lc.setRow(0,2,62);
lc.setRow(0,3,38);
lc.setRow(0,4,38);
lc.setRow(0,5,62);
lc.setRow(0,6,60);
lc.setRow(1,1,60);
lc.setRow(1,2,62);
lc.setRow(1,3,38);
lc.setRow(1,4,38);
lc.setRow(1,5,62);
lc.setRow(1,6,60);
delay(10);
lc.clearDisplay(0);  

lc.setRow(0,1,28);     // parte 3
lc.setRow(0,2,30);
lc.setRow(0,3,6);
lc.setRow(0,4,6);
lc.setRow(0,5,30);
lc.setRow(0,6,28);
lc.setRow(1,1,28);
lc.setRow(1,2,30);
lc.setRow(1,3,6);
lc.setRow(1,4,6);
lc.setRow(1,5,30);
lc.setRow(1,6,28);
delay(10);
lc.clearDisplay(0);  

lc.setRow(0,1,12);     // parte 4
lc.setRow(0,2,14);
lc.setRow(0,3,6);
lc.setRow(0,4,6);
lc.setRow(0,5,14);
lc.setRow(0,6,12);
lc.setRow(1,1,12);
lc.setRow(1,2,14);
lc.setRow(1,3,6);
lc.setRow(1,4,6);
lc.setRow(1,5,14);
lc.setRow(1,6,12);
delay(10);
lc.clearDisplay(0);  

lc.setRow(0,1,4);     // parte 5
lc.setRow(0,2,6);
lc.setRow(0,3,6);
lc.setRow(0,4,6);
lc.setRow(0,5,6);
lc.setRow(0,6,4);
lc.setRow(1,1,4);
lc.setRow(1,2,6);
lc.setRow(1,3,6);
lc.setRow(1,4,6);
lc.setRow(1,5,6);
lc.setRow(1,6,4);
delay(10);
lc.clearDisplay(0);  

lc.setRow(0,2,2);     // parte 6
lc.setRow(0,3,2);
lc.setRow(0,4,2);
lc.setRow(0,5,2);
lc.setRow(1,1,0);
lc.setRow(1,2,2);
lc.setRow(1,3,2);
lc.setRow(1,4,2);
lc.setRow(1,5,2);
lc.setRow(1,6,0);
delay(500);
lc.clearDisplay(0);  

lc.setRow(0,1,4);     // parte 5
lc.setRow(0,2,6);
lc.setRow(0,3,6);
lc.setRow(0,4,6);
lc.setRow(0,5,6);
lc.setRow(0,6,4);
lc.setRow(1,1,4);
lc.setRow(1,2,6);
lc.setRow(1,3,6);
lc.setRow(1,4,6);
lc.setRow(1,5,6);
lc.setRow(1,6,4);
delay(10);
lc.clearDisplay(0);

lc.setRow(0,1,12);     // parte 4
lc.setRow(0,2,14);
lc.setRow(0,3,6);
lc.setRow(0,4,6);
lc.setRow(0,5,14);
lc.setRow(0,6,12);
lc.setRow(1,1,12);
lc.setRow(1,2,14);
lc.setRow(1,3,6);
lc.setRow(1,4,6);
lc.setRow(1,5,14);
lc.setRow(1,6,12);
delay(10);
lc.clearDisplay(0);  

lc.setRow(0,1,28);     // parte 3
lc.setRow(0,2,30);
lc.setRow(0,3,6);
lc.setRow(0,4,6);
lc.setRow(0,5,30);
lc.setRow(0,6,28);
lc.setRow(1,1,28);
lc.setRow(1,2,30);
lc.setRow(1,3,6);
lc.setRow(1,4,6);
lc.setRow(1,5,30);
lc.setRow(1,6,28);
delay(10);
lc.clearDisplay(0);  

lc.setRow(0,1,60);     // parte 2
lc.setRow(0,2,62);
lc.setRow(0,3,38);
lc.setRow(0,4,38);
lc.setRow(0,5,62);
lc.setRow(0,6,60);
lc.setRow(1,1,60);
lc.setRow(1,2,62);
lc.setRow(1,3,38);
lc.setRow(1,4,38);
lc.setRow(1,5,62);
lc.setRow(1,6,60);
delay(10);
lc.clearDisplay(0);  

lc.setRow(0,1,60);     // parte 1
lc.setRow(0,2,126);
lc.setRow(0,3,102);
lc.setRow(0,4,102);
lc.setRow(0,5,126);
lc.setRow(0,6,60);
lc.setRow(1,1,60);
lc.setRow(1,2,126);
lc.setRow(1,3,102);
lc.setRow(1,4,102);
lc.setRow(1,5,126);
lc.setRow(1,6,60);
delay(10);
}


void dizzy()
{
// Code that makes the eyes look dizzy goes here.
}


void downward()
{
// Code that makes the eyes look downward goes here.
}


void left()
{
// Code that makes the eyes look left goes here.
}


void left_downward()
{
// Code that makes the eyes look down and left goes here.
}


void left_upward()
{
// Code that makes the eyes look up and left goes here.
}


void right()
{
// etc
}


void right_downward()
{
// etc.
}


void right_upward()
{
// etc
}


void suchen_V()
{
// etc
}


void upward()
{
// etc
}

You will get plenty of help here but please note the following :

  1. Read and follow the advice in the stickies above as to how to post code. 2 Your program does not contain any voids. It does have are functions that return no value and are, therefore, void.
  2. You do not say what is wrong with your program, what it does/does not do that it should/shouldn't.
  3. It is a mistake not to post or attach all of your program as the problem may be in the bit that you don't post.

Sorry for the trouble. I rewrote the 1st post hoping it makes a little more sense. The forums will not let me post the full code. I will be happy to give any more info that someone might need to help me out.

Ted

If too long to post: Reply, Additional Options, Browse to your locally stored file, Attach it.

Have you written a short sketch that does something simple (such as turn a single LED on or off) in response to input from another micro-controller. If not, start there.

If you can't get it to work it will be easier to get help here with a short sketch.

If it does work you will probably understand how to apply the technique to your larger project or you will be able to pinpoint the problem area for us.

...R

Here is a shorter program that works.
With it I have been able to send the serial commands from the microcontroller to the Arduino and make the eyes move.
I don’t have a problem doing that.
The problem I have is putting the intake() into the program.
The program did NOT come with the intake() I need to add that.
I have also attached the complete program.

int ledPin = 13; // the pin that the LED is attached to
int incomingByte;      // a variable to read incoming serial data into

void setup() {
  // initialize serial communication:
  Serial.begin(9600);
  // initialize the LED pin as an output:
  pinMode(ledPin, OUTPUT);
}

void loop() {
  // see if there's incoming serial data:
  if (Serial.available() > 0) {
    // read the oldest byte in the serial buffer:
    incomingByte = Serial.read();
    // if it's a capital H (ASCII 72), turn on the LED:
    if (incomingByte == 'H') {
      digitalWrite(ledPin, HIGH);
    } 
    // if it's an L (ASCII 76) turn off the LED:
    if (incomingByte == 'L') {
      digitalWrite(ledPin, LOW);
    }
  }
}code]

Goodv.1.txt (9.7 KB)

HerrBall:

Where in your code do you call intake()? During all those delays your Arduino just sits there doing nothing, so it won't read any inputs. To overcome this, see the 'blink without delay' example in the IDE. Also look up 'switch case' in the reference section and Finite State Machine to get rid of those ugly 'go to' statements.

Hello

Below is a link to what the eyes look like and what they do.
Disregard the servo movements, I have removed them, not needed.

The program just runs through all the different movements of the eyes.
The original program did not have the “intake()”, it just looped through “scene()”.
My question here in the forums, I guess I confused people, sorry for that, was how do I add the “intake()” into the program?
I wish to add the “intake()” so I can call up the different position of the eyes when needed.
I will look up the program you mentioned.

Thanks … Ted

Looking at your original sketch it seems to me that you need to adopt a different approach to your program.

You need to have a series of variables each of which represents a value received from the PC. Then your different actions will work, or not depending on the value stored in its particular variables. (These are often referred to as "state" variables).

So the overall style of the program will be something like this pseudo code ...

boolean sceneSelected = false;
boolean suchen_VSelected = false;

void loop(){
  readSerialInput();
  scene();
  suchen_V;
  etc.
}

void readSerialInput() {
  if s is received sceneSelected = true
  if v is received suchen_VSelected = true
  etc
}

void scene() {
   if (sceneSelected == false) {
      return; // do nothing
   }
   else 
      do the scene stuff
   }
}

...R