Code working in tinkercad but not Arduino

Hello!
I am trying to move my code which i made in thinkercad to recreate it in arduino, however i have reached some problems. These are some of the errors "warning: narrowing conversion of '128' from 'int' to 'char' inside { } [-Wnarrowing]
char BinaryD[] = {0x10,0x20,0x30,0x40,0x50,0x60,0x70,0x80,0x90,0xa0,0xb0,0xc0,0xd0,0xe0,0xf0};"
All the other errors are about the same narrowing conversion warning.
My code is shown below and the tinkercad link is this
The first 2 patters work fine on my arduino but the 3rd and the 4th are completely messed up.
Any help? Thanks in advance

static volatile int cnt = 1;
int speed;
int num = 0;


ISR(INT0_vect)
{
 cnt ++;
  if (cnt == 5)
    cnt = 1;
}

void setup() {
   Serial.begin(9600);
  DDRD = 0xf0;
  DDRB = 0x0f;
  PIND = 0x04;
  EICRA = 0x03;
  EIMSK = 0x01;
  DDRC = 0x00;
}

void loop(){
  if (cnt == 1)
    Bounce();
  if (cnt == 2)
    Music();
  if (cnt == 3)
    Complicated();
  if (cnt == 4)
  {
    PORTB = 0x00;
    PORTD = 0x00;
    Binary();
  }
}
void Bounce()
{
  char Pat_BounceD[] = {0x00,0x10,0x30,0x60,0xC0,0x80,0xC0,0x60,0x30,0x10,0x00};
  char Pat_BounceB[] = {0x00,0x08,0x0C,0x06,0x03,0x01,0x03,0x06,0x0C,0x08,0x00};
  for(int i=0; i<=10 ; i++)
  {
    PORTD = Pat_BounceD[i];
    PORTB = Pat_BounceB[i];
    delay(100);
    if (cnt != 1)
      break;
  }
}
void Music()
{
  char Pat_MusicD[] = {0x00,0x10,0x30,0x70,0xf0,0xf0,0xf0,0xf0,0xf0};
  char Pat_MusicB[] = {0x00,0x00,0x00,0x00,0x00,0x01,0x03,0x07,0x0f};
    for(int i=0; i<=8 ; i++)
  {
    PORTD = Pat_MusicD[i];
    PORTB = Pat_MusicB[i];
    delay(100);
    if (cnt != 2)
      break;
  }
  for(int j=8; j>=0 ; j--)
  {
    PORTD = Pat_MusicD[j];
    PORTB = Pat_MusicB[j];
    delay(100);
    if (cnt != 2)
      break;
  }
  
}
void Complicated()
{
  char Pat_CountEOB[] = {0x08,0x04,0x02,0x01,0x00};
  char Pat_CountEOD[] = {0x80,0x40,0x20,0x10};
  char Pat_CountNTD[] = {0x90,0x50,0x30};
  char Pat_CountBSD[] = {0xb0,0x70};
  char Pat_countLB[] = {0x09,0x05,0x03,0x0b,0x07,0x0f};
  char x, y, e, n, b;
  char Pat_ComplicatedB[] = {0x00,x,x,x,x,x,y};
  char Pat_ComplicatedD[] = {0x00,e,n,b,0xf0,0xf0,0xf0};
     for(int i=0; i<=6 ; i++)
  {
    if (Pat_ComplicatedB[i] == x)
    {
      for (int k=0; k<=4 ; k++)
      {
        PORTB = Pat_CountEOB[k];
        delay(100);
        if (cnt != 3)
      break;
        
      }
      
    }
    if(Pat_ComplicatedB[i] == y)
    {
      for (int k=0; k<=6 ; k++)
      {
        PORTB = Pat_countLB[k];
        delay(100);
        if (cnt != 3)
      break;
      }
    }
    if ((Pat_ComplicatedB[i] != x) && Pat_ComplicatedB[i] != y)
    PORTB = Pat_ComplicatedB[i];
       //Done with portb
       
    if(i == 1)
    {
      for (int k=0; k<=3 ; k++)
      {
        PORTD = Pat_CountEOD[k];
        delay(100);
        if (cnt != 3)
      break;
      }
    }
       else if(i == 2)
    {
      for (int k=0; k<=2 ; k++)
      {
        PORTD = Pat_CountNTD[k];
        delay(100);
        if (cnt != 3)
      break;
      }
    }
  else if(i == 3)
    {
      for (int k=0; k<=1 ; k++)
      {
        PORTD = Pat_CountBSD[k];
        delay(100);
        if (cnt != 3)
      break;
      }
    }
  else 
    PORTD = Pat_ComplicatedD[i];
       if (cnt != 3)
      break;
  }
}
void Binary()
{
  char BinaryB[] = {0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f};
  char BinaryD[] = {0x10,0x20,0x30,0x40,0x50,0x60,0x70,0x80,0x90,0xa0,0xb0,0xc0,0xd0,0xe0,0xf0};
  char x;
  char Pat_BinaryD[] = {0x00,x,x,x,x,x,x,x,x,x,x,x,x,x,x};
  char Pat_BinaryB[] = {0x00,x,x,x,x,x,x,x,x,x,x,x,x,x,x};
  
  for (int i=0; i<=15 ; i++)
  {
  for (int k=0; k<=15 ; k++)
  {
    PORTD = BinaryD[k];
    delay(speed);
    if (cnt != 4)
      break;
  }
    PORTB = BinaryB[i];
    delay(speed);
    if (cnt != 4)
      break;
  }
  
  
  
}

Why do you want to use the char type instead of byte for binary values?

oh yes that works! i guess it worked in tinkercad so i thought i could use char for all, but the code for the 3rd and 4th pattern are still not at all the same as tinkercad

Simulation is a dream but hardware is real life :wink:

You're saying 0x10 and 0x20 are OK, but 0x30 and 0x40 aren't?

... utterly pointless if one doesn't have a TinkerCAD account.

The codes "Bounce()" and "Music()" works but not "Complicated" and "Binary"

As for the tinkercad i'm so sorry i didn't know that it was not accessible without an account

You may want to consider what the values of those variables might be, before putting those values in an array.

I use the wokwi simulator at wokwi.com.

It runs on an emulated microprocessor. It uses the same too chain as the IDE.

The errors I have seen there are almost exclusively in the implementation of the bits of hardware in the components available.

Once an error was ultimately found to be an error in the AVR emulation.

Are y'all saying here that the blame for the OP's excitement can be placed on an error in Tinkercad?

Seems more likely that there is something like undefined behaviour playing out differently, for whatever reason, between the two.

I want to believe Tinkercad isn't junk.

a7

I don't know what the code shall do and what's going wrong. I only know that it is either a bug or know restriction in Tinkercad or an error in the description of the user hardware.

In addition the IDE seems to use a different compiler or different compiler switches.

Different results from invoking undefined behaviour…

a7

i tried recreating my circuit in wokwi and the errors are exactly the same as my arduino! thanks! i'll try solving it there since thinkercad just shows that everything's fine

You can share your Wokwi project link without problem.

I made your sketch and your circuit in Wokwi as well:

You have shared a link to your own project at Tinkercad. That means everyone can alter your Tinkercad project. You can still edit your post and remove that link. I also don't understand how I can make a copy of your Tinkercad project to my own Tinkercad projects.

If you want to continue with the circuit that I made in Wokwi, then you have to log in, and use "Save a copy", and then check your projects.

Wokwi can not simulate a analog circuit and can simulate only one Arduino board. But new things are developed almost every day while Tinkercad has bugs that linger on for years.

OK @Koepel spill - how you making those diagrams so precise and neat?

Please tell me you aren’t editing the *.json by hand…

a7

There is a round button with three dots. Enable the grid, and that makes it easier. Moving wires is not implemented yet. So I remove a wire and re-route it.

The resistor value, copying 1 resistor to 8 resistors, copying 1 wire to 8 wires, coloring the wires, and so on, all of that has to be done in the diagram.json file. I also use a text editor with a macro recorder.

I did not really "learn" how to edit the diagram.json, I just pick something up once in a while.

If you don't want to edit the diagram.json file, you have to wait until that is implemented :wink:

THX @Koepel. Grid, nice, never saw that. Poor curiousity factor.

I have edited the *.json, but it isn't really friendly to edit.

The one thing that is handy is this:

       "attrs": { "color": "Purple", "bounce": "0" }

to add the non-bounce feature to the simulating pushbuttons. This makes the OP's code run a bit smoother, that is to say it smoothly increments cnt once for each press.

The function Complicated() seems to use uninitialized char variables, and in an odd way at that.

The only reason I am thinking about this anymore at all is I do not like "works here, doesn't work there" things that magically are "fixed" by something that mightn't. Have even been a problem.

So @p4pompeii, what actually fixed anything? Does your pushbutton work well?

Can you post the current final version that works?

TIA

a7

:+1: :+1: :+1: :+1: :+1: :+1: :+1: :+1:

Hello!! So sorry for the delay i had too many projects to do.
This is what i did to fix my code.

static volatile int cnt = 1;
int speed;
int num = 0;


ISR(INT0_vect)
{
  
}

void setup() {
   Serial.begin(9600);
  DDRD = 0xf0;
  DDRB = 0x0f;
  PIND = 0x04;
  EICRA = 0x03;
  EIMSK = 0x01;
  DDRC = 0x00;
}

void loop(){

  if ((PINC & 0x20) == 0x20)
  {
    cnt++;
    delay(100);
    if(cnt == 5)
    cnt = 1;
  }
  Serial.println(cnt);
  if (cnt == 1)
    Bounce();
  if (cnt == 2)
    Music();
  if (cnt == 3)
    Complicated();
  if (cnt == 4)
  {
    PORTB = 0x00;
    PORTD = 0x00;
    Binary();
  }
}
void Bounce()
{
  byte Pat_BounceD[] = {0x00,0x10,0x30,0x60,0xC0,0x80,0xC0,0x60,0x30,0x10,0x00};
  byte Pat_BounceB[] = {0x00,0x08,0x0C,0x06,0x03,0x01,0x03,0x06,0x0C,0x08,0x00};
  for(int i=0; i<=10 ; i++)
  {
    PORTD = Pat_BounceD[i];
    PORTB = Pat_BounceB[i];
    delay(100);
    if (cnt != 1)
      break;
  }
}
void Music()
{
  byte Pat_MusicD[] = {0x00,0x10,0x30,0x70,0xf0,0xf0,0xf0,0xf0,0xf0};
  byte Pat_MusicB[] = {0x00,0x00,0x00,0x00,0x00,0x01,0x03,0x07,0x0f};
    for(int i=0; i<=8 ; i++)
  {
    PORTD = Pat_MusicD[i];
    PORTB = Pat_MusicB[i];
    delay(200);
    if (cnt != 2)
      break;
  }
  for(int j=8; j>=0 ; j--)
  {
    PORTD = Pat_MusicD[j];
    PORTB = Pat_MusicB[j];
    delay(200);
    if (cnt != 2)
      break;
  }
  
}
void Complicated()
{
  byte Pat_ComplicatedB[] = {0x00,0x08,0x04,0x02,0x01,0x00,0x08,0x04,0x02,0x01,0x00,0x08,0x04,0x02,0x01,0x00,0x08,0x04,0x02,0x01,0x00,0x08,0x04,0x02,0x01,0x09,0x05,0x03,0x0b,0x07,0x0f};
  byte Pat_ComplicatedD[] = {0x00,0x00,0x00,0x00,0x00,0x80,0x40,0x20,0x10,0x10,0x90,0x50,0x30,0x30,0x30,0xb0,0x70,0x70,0x70,0x70,0xf0,0xf0,0xf0,0xf0,0xf0,0xf0,0xf0,0xf0,0xf0,0xf0,0xf0};
     for(int i=0; i<=30 ; i++)
  {
    PORTB = Pat_ComplicatedB[i];
    PORTD = Pat_ComplicatedD[i];
    delay(200);
    if (cnt != 3)
      break;
  }
}
void Binary()
{
  byte BinaryB[] = {0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f};
  byte BinaryD[] = {0x10,0x20,0x30,0x40,0x50,0x60,0x70,0x80,0x90,0xa0,0xb0,0xc0,0xd0,0xe0,0xf0};
  
  for (int k=0; k<=14 ; k++)
  {
    PORTD = BinaryD[k];
    PORTB = BinaryB[k];
    delay(200);
    if (cnt != 4)
      break;

  }
}

the main changes i did was to use polling for the counter as my isr causes debouncing, which i'm not too sure how to fix, and changed my pattern to not include the x in the arrays. You can access the simulation here , mind you the pushbutton is slightly wonky as it waits for the loop of patterns to carry on before changing the counter

If you meant something changed because you switched from char to byte, I wonder if all that helped was to eliminate a compiler warning you did not like or understand.

Have you placed the working code back into your Tinkercod simulation? I wonder if it will perform flawlessly. I hate to think it has a problem that has gone unnoticed and/or unfixed, a problem you happen to discover.

Switch debouncing can be done in many ways, and as you are observing can be important. When you feel like it, do even a shallow dive into the subject, find some technique and code you like and use it always for your buttons.

Since real life doesn’t come with buttons that have an attribute like the wokwi simukated buttons have that can make them perfectly bounce free.

I note also that the wokwi you link does not have your latest code, but it is easy enough for anyone to cut and paste it if they wanna see and experiment.

a7

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.