vtech babys first laptop brain replacement

So about a month ago I started working on my nephews toy made by vtech, seems the microcontroller that was onboard is completely shot.
Being encased in epoxy there is no way to truly check it, So I decided to replace the brain with an atmega8… The toy has 9 led’s and 9 buttons for the interface. I have currently got a few light patterns written but cant seem to get the buttons to function as they should. The pinout is listed in my code below. The buttons are being pulled to ground but as they dont seem to be doing anything… not sure if its in the code or if I have made a mistake on the wiring, Currently I have the buttons disabled because of this. As I dont have a schematic for the device, nor do I for the replaced circuit. It is very hard to figure this out… Please any guidance in the correct direction would be appreciated. Also the are labeled TODO: change manual loop to random pin trigger… I have no idea how to implement if someone could point me in the right direction for that, would be appreciated as well… Thanks in advance

/*#############################################################

  ATMEGA8            |    
  TREY'S_TOY         |
  Brain_Replacement  |

                      Reset   |@------|A5Heart
                      uartRXD |   A   |A4Star
                      uartTXD |   T   |A3Square
                      Pin4GRN |   M   |A2Triangle
                      Pin5O/W |   E   |A1Rectangle
                      Pin6Bn/w|   G   |A0Oval
                      VCC     |   A   |GND
                      GND     |   8   |AREF
                      XTAL1   |   P   |VCC
                      XTAL2   |	  U   |PIN13/SCK blu/w
                      MOONpin5|	      |PIN12/MISO grn/w
                      Pin3BLU |	      |PIN11/MOSI grn
                      Pin2Bl/W|	      |PIN10/Enter aqua
                      Pin1gn/w|_______|PIN9/circle blu

#############################################################*/
 
//######Buttons########
int moon = 5;
int circle = 9;
int enter = 10;
int oval = A0;
int rectangle = A1;
int triangle = A2;
int Square = A3;
int star = A4;
int heart = A5;
int buttonlist[]={moon,circle,enter,oval,rectangle,triangle,Square,star,heart};
//BUTTON SETUP
int buttonState;
int lastButtonState = LOW;
// DEBOUNCE SETUP
long lastDebounceTime = 0;
long debounceDelay = 50;
// cathodes columns
int pin_cols[3]={
  6,7,8};
// anode rows
int pin_rows[3]={
  2,3,4};
  
void setup(){
  //define columns leds (cathode)
  int x;
  for (x = 9; x>0; x--){
    pinMode(buttonlist[x], INPUT);
  }
  pinMode(6, OUTPUT);
  pinMode(7, OUTPUT);
  pinMode(8, OUTPUT);
  //define rows  leds (anode)
  pinMode(2, OUTPUT);
  pinMode(3, OUTPUT);
  pinMode(4, OUTPUT);
  delay(100);
}


void loop(){ 
for (int x = 0; x<5; x++){  
  starPat();
  delay(50);
}
delay(100);
alternating();
delay(100);
flash();
delay(100);
flash();
delay(100);
scrolly();
delay(1000);
  
 
}


void starPat(){
  //STAR Rotating PATTERN
  //MIDDLE ROW VERTICAL
  digitalWrite(2, LOW);
  digitalWrite(3, HIGH);
  digitalWrite(4, LOW);
  digitalWrite(6, LOW);
  digitalWrite(7, LOW);
  digitalWrite(8, LOW);
  delay(50);
  // DIAG ROW TOP LEFT TO BOTTOM RIGHT
  digitalWrite(4, HIGH);
  digitalWrite(6, LOW);
  delay(10);
  digitalWrite(4, LOW);
  digitalWrite(6, HIGH);
  delay(10);
  digitalWrite(3, HIGH);
  digitalWrite(7, LOW);
  delay(10);
  digitalWrite(3, LOW);
  digitalWrite(7, HIGH);
  delay(10);
  digitalWrite(2, HIGH);
  digitalWrite(8, LOW);
  delay(10);
  digitalWrite(2, LOW);
  digitalWrite(8, HIGH);
  delay(10);
  // MIDDLE ROW HORIZONTAL
  digitalWrite(2, HIGH);
  digitalWrite(3, HIGH);
  digitalWrite(4, HIGH);
  digitalWrite(6, HIGH);
  digitalWrite(7, LOW);
  digitalWrite(8, HIGH);
  delay(50);
  // DIAG ROW BOTTOM LEFT TO TOP RIGHT
  digitalWrite(4, HIGH);
  digitalWrite(8, LOW);
  delay(10);
  digitalWrite(4, LOW);
  digitalWrite(8, HIGH);
  delay(10);
  digitalWrite(3, HIGH);
  digitalWrite(7, LOW);  
  delay(10);
  digitalWrite(3, LOW);
  digitalWrite(7, HIGH);
  delay(10);
  digitalWrite(2, HIGH);
  digitalWrite(6, LOW);
  delay(10);
  digitalWrite(2, LOW);
  digitalWrite(6, HIGH);
  delay(10);
}


// Multiple alternating light effect 
// TODO: Change manual digitalWrite() to a loop 
// with random() generated pin for write()
void alternating(){
  for(int x=0; x<3; x++){
    digitalWrite(2, HIGH);
    digitalWrite(3, HIGH);
    digitalWrite(4, HIGH);
    digitalWrite(6, LOW);
    digitalWrite(7, LOW);
    digitalWrite(8, LOW);
    delay(120);
    digitalWrite(2, LOW);
    digitalWrite(3, LOW);
    digitalWrite(4, LOW);
    digitalWrite(6, LOW);
    digitalWrite(7, LOW);
    digitalWrite(8, LOW);
    delay(120);
  }
  for(int x=0; x<3; x++){
    digitalWrite(2, LOW);
    digitalWrite(3, HIGH);
    digitalWrite(4, LOW);
    digitalWrite(6, LOW);
    digitalWrite(7, LOW);
    digitalWrite(8, LOW);
    delay(120);
    digitalWrite(2, LOW);
    digitalWrite(3, LOW);
    digitalWrite(4, LOW);
    digitalWrite(6, LOW);
    digitalWrite(7, LOW);
    digitalWrite(8, LOW);
    delay(120);
  }
  for(int x=0; x<3; x++){
    digitalWrite(2, HIGH);
    digitalWrite(3, LOW);
    digitalWrite(4, HIGH);
    digitalWrite(6, LOW);
    digitalWrite(7, LOW);
    digitalWrite(8, LOW);
    delay(120);
    digitalWrite(2, LOW);
    digitalWrite(3, LOW);
    digitalWrite(4, LOW);
    digitalWrite(6, LOW);
    digitalWrite(7, LOW);
    digitalWrite(8, LOW);
    delay(120);
  }
  for(int x=0; x<3; x++){
    digitalWrite(2, HIGH);
    digitalWrite(3, LOW);
    digitalWrite(4, LOW);
    digitalWrite(6, LOW);
    digitalWrite(7, LOW);
    digitalWrite(8, LOW);
    delay(120);
    digitalWrite(2, LOW);
    digitalWrite(3, HIGH);
    digitalWrite(4, HIGH);
    digitalWrite(6, LOW);
    digitalWrite(7, LOW);
    digitalWrite(8, LOW);
    delay(120);
  }
  for(int x=0; x<3; x++){
    digitalWrite(2, HIGH);
    digitalWrite(3, LOW);
    digitalWrite(4, LOW);
    digitalWrite(6, LOW);
    digitalWrite(7, LOW);
    digitalWrite(8, LOW);
    delay(120);
    digitalWrite(2, HIGH);
    digitalWrite(3, HIGH);
    digitalWrite(4, LOW);
    digitalWrite(6, LOW);
    digitalWrite(7, LOW);
    digitalWrite(8, LOW);
    delay(120);
  }
  for(int x=0; x<3; x++){
    digitalWrite(2, LOW);
    digitalWrite(3, LOW);
    digitalWrite(4, LOW);
    digitalWrite(6, LOW);
    digitalWrite(7, LOW);
    digitalWrite(8, LOW);
    delay(120);
    digitalWrite(2, HIGH);
    digitalWrite(3, LOW);
    digitalWrite(4, LOW);
    digitalWrite(6, LOW);
    digitalWrite(7, LOW);
    digitalWrite(8, LOW);
    delay(120);
  }
  for(int x=0; x<3; x++){
    digitalWrite(2, LOW);
    digitalWrite(3, LOW);
    digitalWrite(4, HIGH);
    digitalWrite(6, LOW);
    digitalWrite(7, LOW);
    digitalWrite(8, LOW);
    delay(120);
    digitalWrite(2, LOW);
    digitalWrite(3, LOW);
    digitalWrite(4, LOW);
    digitalWrite(6, LOW);
    digitalWrite(7, LOW);
    digitalWrite(8, LOW);
    delay(120);
  }
}
void interface(){
  //TODO: debounce and interpret button presses for led display function
  // 9 buttons - 9 different flash patterns
  // if (button press == 0-9){
  //do something different each button press}
  int button;
  
  if (buttonlist[0] == HIGH){
    //MOON
    int x=0;
    int y=0;
    ledOn(x,y);
    }
    else if (buttonlist[1] == HIGH){
    int x=0;
    int y=0;
    ledOff(x,y);
    }
    else if (buttonlist[2] == HIGH){
      scrolly();
    }
    else if (buttonlist[3] == HIGH){
      alternating();
    }
    else if (buttonlist[4] == HIGH){
      flash();
    }
    else if (buttonlist[5] == HIGH){
	starPat();
    }
    else{
      int x=1;
      int y=1;
      ledOn(x,y);
      delay(150);
      ledOff(x,y);
      delay(150);
    }
}

void flash(){
  
  // Flash a pattern
  digitalWrite(2, HIGH);
  digitalWrite(3, HIGH);
  digitalWrite(4, HIGH);
  digitalWrite(6, LOW);
  digitalWrite(7, LOW);
  digitalWrite(8, LOW);
  delay(500);
  digitalWrite(2, LOW);
  digitalWrite(3, LOW);
  digitalWrite(4, LOW);
  digitalWrite(6, LOW);
  digitalWrite(7, LOW);
  digitalWrite(8, LOW);
  delay(500);
}


// Use row to enbable led's
void ledOn(int row, int col){
  digitalWrite(pin_rows[row], HIGH);
  digitalWrite(pin_cols[col], LOW);
}
// Use row to disable led's
void ledOff(int row, int col){
  digitalWrite(pin_rows[row], LOW);
  digitalWrite(pin_cols[col], LOW);
}
// Scrolling effect left to right then back to left
void scrolly(){
  for (int x = 0; x < 5; x++){
  for (int x = 2; x > 0; x--){
    for (int y = 0; y < 3; y++){
      ledOn(x,y);  
      delay(15);    
      ledOff(x,y);      
      delay(15);
    }
  }
  for (int x = 0; x < 3; x++){
    for (int y = 0; y < 3; y++){
      ledOn(x,y); 
      delay(15);     
      ledOff(x,y); 
      delay(15);      
    }
  }
}
}

Sorry if my code seems to be a mess I am still kinda new to programming and the arduino
Also sorry about the double post

I have currently got a few light patterns written but cant seem to get the buttons to function as they should.

You have some code. It does something. You haven't explained what it does. You want the code to do something. You haven't explained that either.

It's hard to give directions from point A to point B with no idea where A or B are.

  for (x = 9; x>0; x--){
    pinMode(buttonlist[x], INPUT);
  }

Array indices start at 0, not 1. Why is the array traversed in reverse order? There is no benefit in that.

There are no internal pullup resistors being used, so you need to have external pullup or pulldown resistors. Which do you have? How ARE the switches wired?

Use Tools + Auto Format before posting code again. Code jammed in column 0 is hard to read.

Sorry about the confusion . The buttons are using pull up resistors to ground… What I am trying to accomplish is instead of just flashing the random patterns on the device, I would like for each button to call a pattern to be displayed. such as if button[1] == pressed{ do star pattern};
etc… Also the part of code that says TODO: Change manual digitalWrite() to a loop
// with random() generated pin for write()… How would I go about doing something like this… say to catch a button press then after code is run for button press then do the random pin digitalWrite()… in a random pattern… no special effect needed, just flashing lights…

Also i have attached the code which i have run autoformat on once more b4 upload

treyToy.cpp (7.24 KB)

What I am trying to accomplish is instead of just flashing the random patterns on the device, I would like for each button to call a pattern to be displayed. such as if button[1] == pressed{ do star pattern};

What have you tried? Reading a switch state is easy - digitalRead(). Determining whether that state represents pressed or released is easy - a simple if statement. Calling a function is easy - star().

Also the part of code that says TODO: Change manual digitalWrite() to a loop
// with random() generated pin for write()… How would I go about doing something like this.

What have you tried? How many times do you want to loop? A for loop is very easy. Using random() to select a pin number can be a challenge. Using random() to select an index into an array is trivial.

This is what I have tried for the button's part anyway.... But as I stated previously when buttons are pressed nothing happens just keeps going through random patterns.... Which is why I disabled that section, till I have a better understanding of what I should be doing.. Again thank you for the quick responses. What I would like it to do is run through random patterns till catches button press then do button press function followed by return to random pattern, or blank and go to sleep for batt savings.

void interface(){ //TODO: debounce and interpret button presses for led display function // 9 buttons - 9 different flash patterns // if (button press == 0-9){ //do something different each button press} int button;

if (buttonlist[0] == HIGH){ //MOON int x=0; int y=0; ledOn(x,y); } else if (buttonlist[1] == HIGH){ int x=0; int y=0; ledOff(x,y); } else if (buttonlist[2] == HIGH){ scrolly(); } else if (buttonlist[3] == HIGH){ alternating(); } else if (buttonlist[4] == HIGH){ flash(); } else if (buttonlist[5] == HIGH){ starPat(); } else{ int x=1; int y=1; ledOn(x,y); delay(150); ledOff(x,y); delay(150); } }

Write a sketch that just uses [u]one[/u] switch input to turn [u]one[/u] LED on and off, and prove that the hardware, pullup/pulldown resistors etc are right before you go any further. Then do the same for each of the other switch inputs. Don't try to use that to control more complex behaviour until you have proved that the input is working correctly.

Peter, I have verified that the buttons do actually function as requested if connected individually, But not when completely installed in device.. Which is why I havent been able to button this project up and move to another... I have The buttons hooked up same way as Previously when individually connected using pullup resistor to ground.. But when the entire project is in the case and all connected... I get no response from buttons in question...

The pics that I have attached show on the inside of device the left board contains 3 buttons one wire to ground and the other 3 wires are connected to input on atmega8 same with middle board it contains 6 buttons connected in same manner... the top right board is the power switch which is a dual pole switch with on off and third setting to program with arduino as isp which is why there is a rs232 port... It was what i had on hand to use as programming interface....

The problem has me stumped and regretably not able to complete. Please advise on where to go from here...

  if (buttonlist[0] == HIGH){

buttonlist[ 0 ] was assigned the value in moon. moon contains 5. 5 is not equal to HIGH. The number of a pin and the state of a pin are not the same thing. Given a pin number, digitalRead() returns the state of that pin, as has already been mentioned.

Paul Thank You for the response. That has made it a little more clear as to what I should Do... I will Give this a try and report back Success/failure again Thank You all For the help and quick responses.. This will make a baby very happy when completed as this was his favorite toy b4 it decided to crap out....

I have The buttons hooked up same way as Previously when individually connected using pullup resistor to ground.

The buttons are either pulled up to 5V or down to ground. They cannot be pulled up to ground. You can do away with the resistors entirely by using the Arduino internal pullup resistors. See http://arduino.cc/en/Tutorial/InputPullupSerial

strigoi8183: Peter, I have verified that the buttons do actually function as requested if connected individually, But not when completely installed in device.. Which is why I havent been able to button this project up and move to another... I have The buttons hooked up same way as Previously when individually connected using pullup resistor to ground.. But when the entire project is in the case and all connected... I get no response from buttons in question...

I may be mis-reading your post but you seem to be saying that when you change the hardware and install a different sketch, it doesn't work. One of the fundamental guidelines for fault finding is to change one thing at a time. If you have a test sketch that works when the Arduino is connected outside of the device, does that same test sketch then work correctly when it is run within the device? Until that works, you're wasting your time IMO trying more complex sketches.

For all who have replied, I want to thank you again for the helpful pointers...

Peter, What I was trying to say is that yes the buttons work as expected individually when tested on circuit with same hardware, but when I tried to test them with full code it was a failure on my part as UKhelibob pointed out that the buttons were being pulled up to ground instead of being pulled down to ground... As I believe this was the issue I have went back and rewrote some of the code as follows:

void setup(){
  //define columns leds (cathode)
  int x;
  for (x = 9; x>0; x--){
    pinMode(buttonlist[x], INPUT_PULLUP);
  }
  pinMode(6, OUTPUT);
  pinMode(7, OUTPUT);
  pinMode(8, OUTPUT);
  //define rows  leds (anode)
  pinMode(2, OUTPUT);
  pinMode(3, OUTPUT);
  pinMode(4, OUTPUT);
  delay(100);
}
Void interface(){
    int button = buttonlist[];
    bool buttonState;
    buttonState = digitalRead(buttonList[0]);
    if(button[0] = HIGH{
        //MOON
        int x = 1;
        int y = 1;
        ledOn(x,y);
    }
    else{
        int x = 1;
        int y = 1;
        ledOn(x,y);
        delay(250);
        ledOff(x,y);
        delay(250);
    }
}

As I said I believe it was this of my own error to follow the guide to using pullup and pulldown resistors that originally caused my issue...

Also Paul thank you for pointing out the error in my code which was setting the buttonstate to a number outside of what I was trying to accomplish instead of a boolean value, which is what I truly needed.

I have posted the code above as an example, If anyone would like to review it and tell me if there may be anything else that I was missing That would be appreciated... I am now about to compile and verify the changes above and see if any other unexpected error arrises.... Thank you all again

Ok so the code that i tried did not work but I did test a single button while in the current circuit configuration, with promising results as shown in the pictures attached. While not pressing the button the led stays lit and when the button is pressed the led turns off just as predicted it would.
I know that I am kinda flooding this forum post with updates but as I have been doing this as trial and error I would like to update and keep everyone posted on the progress… That way if anyone has an idea or opinion on something to do differently They will know exactly where I stand in the project…

The code for the single button is as follows:

int ledCath = 6;
int ledAnode = 2;
// set pin numbers:
const int buttonPin = 5;     // the number of the pushbutton pin

// variables will change:
int buttonState = 0;         // variable for reading the pushbutton status

void setup() {
  // initialize the LED pin as an output:
  pinMode(ledCath, OUTPUT); 
  pinMode(ledAnode, OUTPUT);  
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT_PULLUP);     
}

void loop(){
  // read the state of the pushbutton value:
  buttonState = digitalRead(buttonPin);

  // check if the pushbutton is pressed.
  // if it is, the buttonState is HIGH:
  if (buttonState == HIGH) {     
    // turn LED on:    
    digitalWrite(ledCath, LOW);  
    digitalWrite(ledAnode, HIGH);
  } 
  else {
    // turn LED off:
    digitalWrite(ledCath, LOW); 
    digitalWrite(ledAnode, LOW);
  }
}

So now I am at the point of migrating the button code back into the toy code to find out results… Heres hoping it works

Void interface(){

Void? Never hear of that. void, yes. Void, no.

interface doesn't tell us squat about what the function does.

it was a failure on my part as UKhelibob pointed out that the buttons were being pulled up to ground instead of being pulled down to ground

I still don't know whether they are being pulled up to 5V or down to ground !

Buttons Are being pulled down to ground with pulldown resistors attached to circuit on buttons... Sorry should have put that void interface() is part of original code that is in the first listing I put here....

And also buttons are being pulled to ground so Yes no need to use INPUT_Pullup just using a pull down resistor on the button...

UKHeliBob:

it was a failure on my part as UKhelibob pointed out that the buttons were being pulled up to ground instead of being pulled down to ground

I still don't know whether they are being pulled up to 5V or down to ground !

I think strigoi8183 has a bit of confusion about the term pull up.

@strigoi8183 Let me try to explain it so you fully understand and then don't confuse the rest of the world when you inadvertently use it incorrectly.

When using a resistor to pull an input pin, it is used to pull in a direction that is defined by the type of logic you are using. Pulling "up" will pull the pin towards logic High, and pulling "down" will pull the pin towards logic Low. Most of the official Arduinos (the exception is the Due as of this writing) use 5V logic. In the 5V logic world, near 5V is logic High, and near 0V(GND) is logic Low. (There are some details as to where in the middle the change from high and low is, but that isn't important for this lesson.) So, a "pull up" resistor will pull the input towards 5V, and a "pull down" resistor will pull the input towards GND. The Arduino exception is the Due. It uses 3.3V logic, but the rules work the same. Pull up resistors pull towards 3.3V and pull down resistors pull towards GND.

From this example, one sees that "pull up" seems to indicate pulling towards a DC voltage, and "pull down" pulls towards ground. But it is important to think of pull up/down as it relates to logic high/low, not voltage levels. I've worked with an odd-ball logic called Emitter-Coupled Logic (ECL). (I'm not sure why they used ECL. It may have been to integrate to legacy custom equipment designed back when ECL was the only logic family fast enough.) With ECL logic all the voltage levels are [u]negative[/u] VDC. Low is from -5.2(Vee) to -1.4, and high is from -1.2 to 0(GND), usually implemented with only a 0.8V swing between high and low. So, while logic high is technically more positive (thus higher) than low, it is also closer to ground than low is because all the logic levels are "below" ground. To save sanity it is just best to think of pull up/down in logic levels. (But, yeah... this is one very rare, and not related to Arduinos at all, instance where the phrase "pull up to ground" actually makes sense...)

To make things worse, there are some even more exotic logic levels that are defined by differing current levels instead of voltage levels. I've only heard of them anecdotally and have no idea how one would steer an otherwise floating input in those logic familes. But, the convention of pulling up/down relating to logic high/low would still work.

We now return you to your regularly scheduled code discussion...

Sembazuru. Thank u for clearing that up for me… let me make sure I have tis correct… in my circuit the buttons are connected to for example pin 5 ant then to ground … so then the logic would be held in a high state untill said button gets pressed pulling it low … am I correct in this assumption?
If so then I have been going about this backwards and need to change my code to reflect… Sorry if I have confused anyone… n thank you for all the help that has bern given

In your example pin 5 will be held high until the button is pressed if you have a resistor connected between pin 5 and 5V or you enable the internal resistor by using INPUT_PULLUP in the pinMode command. You then detect a button press because a digitalRead of pin 5 returns a LOW.