JSON file not being read correctly

Hello,

I am trying to read data from a JSON file, but I am having trouble. I would like the background to change based on the level type indicated (deep, light, wake, rem). I followed Dan Shiffman’s Coding train video and have worked with JSON in a different project before, but for some reason it is not working this time. The error message I receive is “sleep[L].data is undefined”

Here is sample JSON file:

[{
  "logId" : 27405388351,
  "dateOfSleep" : "2020-05-30",
  "startTime" : "2020-05-29T23:43:30.000",
  "endTime" : "2020-05-30T07:53:00.000",
  "duration" : 29340000,
  "minutesToFallAsleep" : 0,
  "minutesAsleep" : 434,
  "minutesAwake" : 55,
  "minutesAfterWakeup" : 1,
  "timeInBed" : 489,
  "efficiency" : 94,
  "type" : "stages",
  "infoCode" : 0,
  "levels" : {
    "summary" : {
      "deep" : {
        "count" : 3,
        "minutes" : 37,
        "thirtyDayAvgMinutes" : 57
      },
      "wake" : {
        "count" : 33,
        "minutes" : 55,
        "thirtyDayAvgMinutes" : 68
      },
      "light" : {
        "count" : 31,
        "minutes" : 293,
        "thirtyDayAvgMinutes" : 266
      },
      "rem" : {
        "count" : 9,
        "minutes" : 104,
        "thirtyDayAvgMinutes" : 99
      }
    },
    "data" : [{
      "dateTime" : "2020-05-29T23:43:30.000",
      "level" : "wake",
      "seconds" : 30
    },{
      "dateTime" : "2020-05-29T23:44:00.000",
      "level" : "light",
      "seconds" : 2250
    },{
      "dateTime" : "2020-05-30T00:21:30.000",
      "level" : "deep",
      "seconds" : 960
    },{
      "dateTime" : "2020-05-30T00:37:30.000",
      "level" : "light",
      "seconds" : 1560
    },{
      "dateTime" : "2020-05-30T01:03:30.000",
      "level" : "rem",
      "seconds" : 1500
    },{
      "dateTime" : "2020-05-30T01:28:30.000",
      "level" : "light",
      "seconds" : 1770
    },{
      "dateTime" : "2020-05-30T01:58:00.000",
      "level" : "wake",
      "seconds" : 360
    },{
      "dateTime" : "2020-05-30T02:04:00.000",
      "level" : "light",
      "seconds" : 3660
    },{
      "dateTime" : "2020-05-30T03:05:00.000",
      "level" : "deep",
      "seconds" : 840
    },{
      "dateTime" : "2020-05-30T03:19:00.000",
      "level" : "light",
      "seconds" : 180
    },{
      "dateTime" : "2020-05-30T03:22:00.000",
      "level" : "rem",
      "seconds" : 1740
    },{
      "dateTime" : "2020-05-30T03:51:00.000",
      "level" : "light",
      "seconds" : 360
    },{
      "dateTime" : "2020-05-30T03:57:00.000",
      "level" : "rem",
      "seconds" : 360
    },{
      "dateTime" : "2020-05-30T04:03:00.000",
      "level" : "light",
      "seconds" : 5310
    },{
      "dateTime" : "2020-05-30T05:31:30.000",
      "level" : "rem",
      "seconds" : 1320
    },{
      "dateTime" : "2020-05-30T05:53:30.000",
      "level" : "wake",
      "seconds" : 210
    },{
      "dateTime" : "2020-05-30T05:57:00.000",
      "level" : "light",
      "seconds" : 930
    },{
      "dateTime" : "2020-05-30T06:12:30.000",
      "level" : "rem",
      "seconds" : 330
    },{
      "dateTime" : "2020-05-30T06:18:00.000",
      "level" : "wake",
      "seconds" : 900
    },{
      "dateTime" : "2020-05-30T06:33:00.000",
      "level" : "light",
      "seconds" : 2490
    },{
      "dateTime" : "2020-05-30T07:14:30.000",
      "level" : "deep",
      "seconds" : 450
    },{
      "dateTime" : "2020-05-30T07:22:00.000",
      "level" : "light",
      "seconds" : 480
    },{
      "dateTime" : "2020-05-30T07:30:00.000",
      "level" : "rem",
      "seconds" : 1170
    },{
      "dateTime" : "2020-05-30T07:49:30.000",
      "level" : "wake",
      "seconds" : 210
    }],
    "shortData" : [{
      "dateTime" : "2020-05-29T23:43:30.000",
      "level" : "wake",
      "seconds" : 90
    },{
      "dateTime" : "2020-05-29T23:48:30.000",
      "level" : "wake",
      "seconds" : 30
    },{
      "dateTime" : "2020-05-29T23:50:30.000",
      "level" : "wake",
      "seconds" : 30
    },{
      "dateTime" : "2020-05-30T01:00:30.000",
      "level" : "wake",
      "seconds" : 180
    },{
      "dateTime" : "2020-05-30T01:10:00.000",
      "level" : "wake",
      "seconds" : 30
    },{
      "dateTime" : "2020-05-30T01:32:00.000",
      "level" : "wake",
      "seconds" : 30
    },{
      "dateTime" : "2020-05-30T01:51:00.000",
      "level" : "wake",
      "seconds" : 30
    },{
      "dateTime" : "2020-05-30T02:08:00.000",
      "level" : "wake",
      "seconds" : 60
    },{
      "dateTime" : "2020-05-30T02:20:00.000",
      "level" : "wake",
      "seconds" : 60
    },{
      "dateTime" : "2020-05-30T02:22:30.000",
      "level" : "wake",
      "seconds" : 150
    },{
      "dateTime" : "2020-05-30T02:28:30.000",
      "level" : "wake",
      "seconds" : 30
    },{
      "dateTime" : "2020-05-30T02:43:30.000",
      "level" : "wake",
      "seconds" : 120
    },{
      "dateTime" : "2020-05-30T03:18:00.000",
      "level" : "wake",
      "seconds" : 60
    },{
      "dateTime" : "2020-05-30T03:22:00.000",
      "level" : "wake",
      "seconds" : 30
    },{
      "dateTime" : "2020-05-30T03:38:00.000",
      "level" : "wake",
      "seconds" : 30
    },{
      "dateTime" : "2020-05-30T03:50:30.000",
      "level" : "wake",
      "seconds" : 30
    },{
      "dateTime" : "2020-05-30T04:04:00.000",
      "level" : "wake",
      "seconds" : 30
    },{
      "dateTime" : "2020-05-30T04:24:00.000",
      "level" : "wake",
      "seconds" : 30
    },{
      "dateTime" : "2020-05-30T04:26:30.000",
      "level" : "wake",
      "seconds" : 30
    },{
      "dateTime" : "2020-05-30T04:30:00.000",
      "level" : "wake",
      "seconds" : 60
    },{
      "dateTime" : "2020-05-30T04:34:00.000",
      "level" : "wake",
      "seconds" : 30
    },{
      "dateTime" : "2020-05-30T04:45:30.000",
      "level" : "wake",
      "seconds" : 60
    },{
      "dateTime" : "2020-05-30T04:53:00.000",
      "level" : "wake",
      "seconds" : 60
    },{
      "dateTime" : "2020-05-30T05:09:30.000",
      "level" : "wake",
      "seconds" : 30
    },{
      "dateTime" : "2020-05-30T05:29:00.000",
      "level" : "wake",
      "seconds" : 120
    },{
      "dateTime" : "2020-05-30T05:42:30.000",
      "level" : "wake",
      "seconds" : 30
    },{
      "dateTime" : "2020-05-30T05:59:00.000",
      "level" : "wake",
      "seconds" : 120
    },{
      "dateTime" : "2020-05-30T06:52:00.000",
      "level" : "wake",
      "seconds" : 30
    },{
      "dateTime" : "2020-05-30T07:27:30.000",
      "level" : "wake",
      "seconds" : 30
    }]
  },
  "mainSleep" : true
}

Here is my code:

let L = 0;
let secs = 0;
// let list1 = [];
let sleep = [];

function preload(){
  //Load list of json file names
  //list1 = loadStrings('dataList.txt');
  sleep = loadJSON("data/sleep-2020-04-30.json");
}

function setup() {
  createCanvas(windowWidth, windowHeight);
  colorMode(HSB, 360, 100, 100, 100);
}

function draw() {
    let sleepLevel = sleep[L]['data']['level'];
    print(sleepLevel)
    
    if (sleepLevel == ["wake"]){
      //frameCount = 0;
      background(0, 100, 100);
    }
    if (sleepLevel == ["deep"]){
      background(100, 100, 100);
    }
    if (sleepLevel == ["light"]){
      background(200, 100, 100);
    }
    if (sleepLevel == ["rem"]){
      background(300, 100, 100);
    }

    L += 1;
  }
``
1 Like

Hi @fleshcircuit,

Your data/sleep-2020-04-30.json file needs a ] at the end.

To get things working, try this, then you can modify and build upon it as you wish, in order to complete your script:


let secs = 0;
// let list1 = [];
let sleep, data;

function preload(){
  //Load list of json file names
  //list1 = loadStrings('dataList.txt');
  sleep = loadJSON("data/sleep-2020-04-30.json");
  
}

function setup() {
  createCanvas(windowWidth, windowHeight);
  colorMode(HSB, 360, 100, 100, 100);
  data = sleep[0]["levels"]["data"];
  frameRate(1);
}

function draw() {
    let sleepLevel = data[frameCount % data.length]["level"];
    print(sleepLevel)
    
    if (sleepLevel == ["wake"]){
      //frameCount = 0;
      background(0, 100, 100);
    }
    if (sleepLevel == ["deep"]){
      background(100, 100, 100);
    }
    if (sleepLevel == ["light"]){
      background(200, 100, 100);
    }
    if (sleepLevel == ["rem"]){
      background(300, 100, 100);
    }

  }

EDITED on August 19, 2021 to add the following:

Essentially, the main problem was not properly drilling down into the information from the .json file. This, in the setup() function, helps get to what you need:

  data = sleep[0]["levels"]["data"];

Using [0] gets to the extensive object inside the opening square bracket. From there we need to get into the "levels" and "data" attributes, respectively.

1 Like

That works! Thank you.

1 Like

I have come across a minor issue when running the sketch (I believe this counts as additional details, so I am continuing the thread though I can start a new thread if needed).

The JSON file I am using has multiple sets of data (days of a month). At the moment, the sketch reads only the first day and loops. I am trying to figure out how I can get it to move to subsequent days.

First three days from JSON file:

[{
  "logId" : 27405388351,
  "dateOfSleep" : "2020-05-30",
  "startTime" : "2020-05-29T23:43:30.000",
  "endTime" : "2020-05-30T07:53:00.000",
  "duration" : 29340000,
  "minutesToFallAsleep" : 0,
  "minutesAsleep" : 434,
  "minutesAwake" : 55,
  "minutesAfterWakeup" : 1,
  "timeInBed" : 489,
  "efficiency" : 94,
  "type" : "stages",
  "infoCode" : 0,
  "levels" : {
    "summary" : {
      "deep" : {
        "count" : 3,
        "minutes" : 37,
        "thirtyDayAvgMinutes" : 57
      },
      "wake" : {
        "count" : 33,
        "minutes" : 55,
        "thirtyDayAvgMinutes" : 68
      },
      "light" : {
        "count" : 31,
        "minutes" : 293,
        "thirtyDayAvgMinutes" : 266
      },
      "rem" : {
        "count" : 9,
        "minutes" : 104,
        "thirtyDayAvgMinutes" : 99
      }
    },
    "data" : [{
      "dateTime" : "2020-05-29T23:43:30.000",
      "level" : "wake",
      "seconds" : 30
    },{
      "dateTime" : "2020-05-29T23:44:00.000",
      "level" : "light",
      "seconds" : 2250
    },{
      "dateTime" : "2020-05-30T00:21:30.000",
      "level" : "deep",
      "seconds" : 960
    },{
      "dateTime" : "2020-05-30T00:37:30.000",
      "level" : "light",
      "seconds" : 1560
    },{
      "dateTime" : "2020-05-30T01:03:30.000",
      "level" : "rem",
      "seconds" : 1500
    },{
      "dateTime" : "2020-05-30T01:28:30.000",
      "level" : "light",
      "seconds" : 1770
    },{
      "dateTime" : "2020-05-30T01:58:00.000",
      "level" : "wake",
      "seconds" : 360
    },{
      "dateTime" : "2020-05-30T02:04:00.000",
      "level" : "light",
      "seconds" : 3660
    },{
      "dateTime" : "2020-05-30T03:05:00.000",
      "level" : "deep",
      "seconds" : 840
    },{
      "dateTime" : "2020-05-30T03:19:00.000",
      "level" : "light",
      "seconds" : 180
    },{
      "dateTime" : "2020-05-30T03:22:00.000",
      "level" : "rem",
      "seconds" : 1740
    },{
      "dateTime" : "2020-05-30T03:51:00.000",
      "level" : "light",
      "seconds" : 360
    },{
      "dateTime" : "2020-05-30T03:57:00.000",
      "level" : "rem",
      "seconds" : 360
    },{
      "dateTime" : "2020-05-30T04:03:00.000",
      "level" : "light",
      "seconds" : 5310
    },{
      "dateTime" : "2020-05-30T05:31:30.000",
      "level" : "rem",
      "seconds" : 1320
    },{
      "dateTime" : "2020-05-30T05:53:30.000",
      "level" : "wake",
      "seconds" : 210
    },{
      "dateTime" : "2020-05-30T05:57:00.000",
      "level" : "light",
      "seconds" : 930
    },{
      "dateTime" : "2020-05-30T06:12:30.000",
      "level" : "rem",
      "seconds" : 330
    },{
      "dateTime" : "2020-05-30T06:18:00.000",
      "level" : "wake",
      "seconds" : 900
    },{
      "dateTime" : "2020-05-30T06:33:00.000",
      "level" : "light",
      "seconds" : 2490
    },{
      "dateTime" : "2020-05-30T07:14:30.000",
      "level" : "deep",
      "seconds" : 450
    },{
      "dateTime" : "2020-05-30T07:22:00.000",
      "level" : "light",
      "seconds" : 480
    },{
      "dateTime" : "2020-05-30T07:30:00.000",
      "level" : "rem",
      "seconds" : 1170
    },{
      "dateTime" : "2020-05-30T07:49:30.000",
      "level" : "wake",
      "seconds" : 210
    }],
    "shortData" : [{
      "dateTime" : "2020-05-29T23:43:30.000",
      "level" : "wake",
      "seconds" : 90
    },{
      "dateTime" : "2020-05-29T23:48:30.000",
      "level" : "wake",
      "seconds" : 30
    },{
      "dateTime" : "2020-05-29T23:50:30.000",
      "level" : "wake",
      "seconds" : 30
    },{
      "dateTime" : "2020-05-30T01:00:30.000",
      "level" : "wake",
      "seconds" : 180
    },{
      "dateTime" : "2020-05-30T01:10:00.000",
      "level" : "wake",
      "seconds" : 30
    },{
      "dateTime" : "2020-05-30T01:32:00.000",
      "level" : "wake",
      "seconds" : 30
    },{
      "dateTime" : "2020-05-30T01:51:00.000",
      "level" : "wake",
      "seconds" : 30
    },{
      "dateTime" : "2020-05-30T02:08:00.000",
      "level" : "wake",
      "seconds" : 60
    },{
      "dateTime" : "2020-05-30T02:20:00.000",
      "level" : "wake",
      "seconds" : 60
    },{
      "dateTime" : "2020-05-30T02:22:30.000",
      "level" : "wake",
      "seconds" : 150
    },{
      "dateTime" : "2020-05-30T02:28:30.000",
      "level" : "wake",
      "seconds" : 30
    },{
      "dateTime" : "2020-05-30T02:43:30.000",
      "level" : "wake",
      "seconds" : 120
    },{
      "dateTime" : "2020-05-30T03:18:00.000",
      "level" : "wake",
      "seconds" : 60
    },{
      "dateTime" : "2020-05-30T03:22:00.000",
      "level" : "wake",
      "seconds" : 30
    },{
      "dateTime" : "2020-05-30T03:38:00.000",
      "level" : "wake",
      "seconds" : 30
    },{
      "dateTime" : "2020-05-30T03:50:30.000",
      "level" : "wake",
      "seconds" : 30
    },{
      "dateTime" : "2020-05-30T04:04:00.000",
      "level" : "wake",
      "seconds" : 30
    },{
      "dateTime" : "2020-05-30T04:24:00.000",
      "level" : "wake",
      "seconds" : 30
    },{
      "dateTime" : "2020-05-30T04:26:30.000",
      "level" : "wake",
      "seconds" : 30
    },{
      "dateTime" : "2020-05-30T04:30:00.000",
      "level" : "wake",
      "seconds" : 60
    },{
      "dateTime" : "2020-05-30T04:34:00.000",
      "level" : "wake",
      "seconds" : 30
    },{
      "dateTime" : "2020-05-30T04:45:30.000",
      "level" : "wake",
      "seconds" : 60
    },{
      "dateTime" : "2020-05-30T04:53:00.000",
      "level" : "wake",
      "seconds" : 60
    },{
      "dateTime" : "2020-05-30T05:09:30.000",
      "level" : "wake",
      "seconds" : 30
    },{
      "dateTime" : "2020-05-30T05:29:00.000",
      "level" : "wake",
      "seconds" : 120
    },{
      "dateTime" : "2020-05-30T05:42:30.000",
      "level" : "wake",
      "seconds" : 30
    },{
      "dateTime" : "2020-05-30T05:59:00.000",
      "level" : "wake",
      "seconds" : 120
    },{
      "dateTime" : "2020-05-30T06:52:00.000",
      "level" : "wake",
      "seconds" : 30
    },{
      "dateTime" : "2020-05-30T07:27:30.000",
      "level" : "wake",
      "seconds" : 30
    }]
  },
  "mainSleep" : true
},{
  "logId" : 27394706444,
  "dateOfSleep" : "2020-05-29",
  "startTime" : "2020-05-28T23:36:30.000",
  "endTime" : "2020-05-29T08:21:30.000",
  "duration" : 31500000,
  "minutesToFallAsleep" : 0,
  "minutesAsleep" : 434,
  "minutesAwake" : 91,
  "minutesAfterWakeup" : 0,
  "timeInBed" : 525,
  "efficiency" : 94,
  "type" : "stages",
  "infoCode" : 0,
  "levels" : {
    "summary" : {
      "deep" : {
        "count" : 2,
        "minutes" : 58,
        "thirtyDayAvgMinutes" : 57
      },
      "wake" : {
        "count" : 20,
        "minutes" : 91,
        "thirtyDayAvgMinutes" : 67
      },
      "light" : {
        "count" : 22,
        "minutes" : 278,
        "thirtyDayAvgMinutes" : 266
      },
      "rem" : {
        "count" : 6,
        "minutes" : 98,
        "thirtyDayAvgMinutes" : 99
      }
    },
    "data" : [{
      "dateTime" : "2020-05-28T23:36:30.000",
      "level" : "wake",
      "seconds" : 240
    },{
      "dateTime" : "2020-05-28T23:40:30.000",
      "level" : "light",
      "seconds" : 1380
    },{
      "dateTime" : "2020-05-29T00:03:30.000",
      "level" : "deep",
      "seconds" : 3030
    },{
      "dateTime" : "2020-05-29T00:54:00.000",
      "level" : "light",
      "seconds" : 1890
    },{
      "dateTime" : "2020-05-29T01:25:30.000",
      "level" : "rem",
      "seconds" : 390
    },{
      "dateTime" : "2020-05-29T01:32:00.000",
      "level" : "light",
      "seconds" : 30
    },{
      "dateTime" : "2020-05-29T01:32:30.000",
      "level" : "wake",
      "seconds" : 330
    },{
      "dateTime" : "2020-05-29T01:38:00.000",
      "level" : "light",
      "seconds" : 2370
    },{
      "dateTime" : "2020-05-29T02:17:30.000",
      "level" : "rem",
      "seconds" : 300
    },{
      "dateTime" : "2020-05-29T02:22:30.000",
      "level" : "light",
      "seconds" : 2160
    },{
      "dateTime" : "2020-05-29T02:58:30.000",
      "level" : "rem",
      "seconds" : 1290
    },{
      "dateTime" : "2020-05-29T03:20:00.000",
      "level" : "light",
      "seconds" : 2040
    },{
      "dateTime" : "2020-05-29T03:54:00.000",
      "level" : "wake",
      "seconds" : 2760
    },{
      "dateTime" : "2020-05-29T04:40:00.000",
      "level" : "light",
      "seconds" : 930
    },{
      "dateTime" : "2020-05-29T04:55:30.000",
      "level" : "wake",
      "seconds" : 210
    },{
      "dateTime" : "2020-05-29T04:59:00.000",
      "level" : "light",
      "seconds" : 570
    },{
      "dateTime" : "2020-05-29T05:08:30.000",
      "level" : "deep",
      "seconds" : 450
    },{
      "dateTime" : "2020-05-29T05:16:00.000",
      "level" : "light",
      "seconds" : 1230
    },{
      "dateTime" : "2020-05-29T05:36:30.000",
      "level" : "rem",
      "seconds" : 3960
    },{
      "dateTime" : "2020-05-29T06:42:30.000",
      "level" : "wake",
      "seconds" : 510
    },{
      "dateTime" : "2020-05-29T06:51:00.000",
      "level" : "light",
      "seconds" : 4440
    },{
      "dateTime" : "2020-05-29T08:05:00.000",
      "level" : "wake",
      "seconds" : 360
    },{
      "dateTime" : "2020-05-29T08:11:00.000",
      "level" : "light",
      "seconds" : 240
    },{
      "dateTime" : "2020-05-29T08:15:00.000",
      "level" : "wake",
      "seconds" : 390
    }],
    "shortData" : [{
      "dateTime" : "2020-05-29T01:13:30.000",
      "level" : "wake",
      "seconds" : 90
    },{
      "dateTime" : "2020-05-29T01:41:00.000",
      "level" : "wake",
      "seconds" : 120
    },{
      "dateTime" : "2020-05-29T01:44:30.000",
      "level" : "wake",
      "seconds" : 30
    },{
      "dateTime" : "2020-05-29T02:23:00.000",
      "level" : "wake",
      "seconds" : 30
    },{
      "dateTime" : "2020-05-29T02:52:30.000",
      "level" : "wake",
      "seconds" : 30
    },{
      "dateTime" : "2020-05-29T03:11:30.000",
      "level" : "wake",
      "seconds" : 30
    },{
      "dateTime" : "2020-05-29T03:20:30.000",
      "level" : "wake",
      "seconds" : 30
    },{
      "dateTime" : "2020-05-29T05:21:00.000",
      "level" : "wake",
      "seconds" : 60
    },{
      "dateTime" : "2020-05-29T05:25:30.000",
      "level" : "wake",
      "seconds" : 60
    },{
      "dateTime" : "2020-05-29T05:50:00.000",
      "level" : "wake",
      "seconds" : 30
    },{
      "dateTime" : "2020-05-29T07:12:30.000",
      "level" : "wake",
      "seconds" : 60
    },{
      "dateTime" : "2020-05-29T07:36:00.000",
      "level" : "wake",
      "seconds" : 60
    },{
      "dateTime" : "2020-05-29T07:47:30.000",
      "level" : "wake",
      "seconds" : 30
    }]
  },
  "mainSleep" : true
}

Code for reading data from JSON


function sleepMapping(){
  data = sleep[L]["levels"]["data"];

  // sleep level mapping
  sleepLevel = data[frameCount % data.length]["level"];
  let duration = data[frameCount % data.length]["seconds"];
  
  sat = map(duration, 0, 6000, 1, 100);
  alp = sat;
  print(sleepLevel, duration);
}
2 Likes

The JSON file that you posted contains two nights of sleep data. How many nights of data do you have in total?

Recall this line from this post:

data = sleep[0]["levels"]["data"];

The first subscript, which is 0 in the above line, represents the index of the night for which you are processing data. To get past the first night, you can create a variable, as follows, to count up through the nights:

let night = 0;

Then you can use this:

data = sleep[night]["levels"]["data"];

Whenever you finish off the last piece of data for a given night, increment the night variable by 1 to get to the data for the next night.

EDIT (August 27, 2021):

In order to process data for multiple nights, when it is possible for nights to differ in the number of readings, you will also need to depart from the technique of using frameCount to calculate an index to the data.

Try out the code below, and feel free to refine it to suit your needs. Note that the variable name night_index is used instead of the name night that was suggested above.

let sleep, night_data;
let num_nights; // number of nights of data
let new_night = true; // starting data for a new night
let night_index = 0; // index for each night
let night_data_length; // number of data points for a night
let night_data_index; // index for data points for a night
function preload(){
  sleep = loadJSON("data/sleep-2020-04-30.json");
}

function setup() {
  createCanvas(windowWidth, windowHeight);
  num_nights = Object.keys(sleep).length; 
  colorMode(HSB, 360, 100, 100, 100);

  frameRate(1);
  textSize(24);
}

function draw() {

  if (new_night) {
      // starting a new night of data
      night_data = sleep[night_index]["levels"]["data"];
      night_data_length = night_data.length;
      new_night = false;
      night_data_index = 0;
  }
    let sleepLevel = night_data[night_data_index]["level"];
    let dateTime = night_data[night_data_index]["dateTime"];
    
    if (sleepLevel == ["wake"]){
      //frameCount = 0;
      background(0, 100, 100);
    }
    if (sleepLevel == ["deep"]){
      background(100, 100, 100);
    }
    if (sleepLevel == ["light"]){
      background(200, 100, 100);
    }
    if (sleepLevel == ["rem"]){
      background(300, 100, 100);
    }
    text(dateTime, 40, 40);
    text("Night: " + night_index + " Reading: " + night_data_index, 40, 80);
    text(sleepLevel, 40, 120);
    night_data_index += 1;
    if (night_data_index === night_data_length) {
      // have processed all data for a night
      new_night = true;
      night_index += 1;
      // are there any more nights?
      if (night_index === num_nights) {
        noLoop();
      }
    }
  }
1 Like

Thanks! I will try this out.

1 Like

One of the challenges with this project is that it achieves a changing display via automatic repetitive calling of the draw() function as a proxy for an explicit nested loop. We have a succession of nights, and within each night there is a sequence of data recordings, hence the rationale for a nested loop.

If we had an actual explicit nested loop, it might be situated entirely within the draw() function. However, instead, there are two counters, namely night_index and night_data_index to manage the virtual outer and inner loops, respectively. Operationally, though not structurally within the code, these virtual loops straddle the draw() function, of which each automatic call functions as if it were an individual iteration of our virtual inner loop.

@fleshcircuit, we look forward to learning of how this project works out.

EDITED for clarification on August 27, 2021

Thansk for this – it’s mostly working now. I’ve been using this to create a digital art piece/poem: https://editor.p5js.org/fleshcircuit/sketches/UivbitD6Z

Only issue that comes up is that I am trying to get the data to loop through the month again when it gets to the end. I tried this:

night_data_index += 1;
  if (night_data_index == night_data_length) {
  // have processed all data for a night
  new_night = true;
  night_index += 1;
  // are there any more nights?
  if (night_index === num_nights) {
    night_index = 0; //swapping out noLoop()
        }
      }

But that didn’t work. It seems that most of the months have 32 nights of data, so I changed to a static number.

  if (night_index === 32) {
    night_index = 0; 
        }

However, when I ran it through, at least one of the months have less than that number, so in those instances, it freezes when it runs out of data.

I will keep playing around with it. I am happy with how it is coming together. Thanks so much for helping me figure this out.

2 Likes

To resolve that issue, begin with the script from this post. Then, simply replace this …

      if (night_index === num_nights) {
        noLoop();
      }

… with this, in order to reset the variables that initiate the iteration through the data …

      if (night_index === num_nights) {
        night_index = 0;
        new_night = true;
        night_data_index = 0;
      }
1 Like

That works – thank you!

1 Like